public void testMetamodelCriteriaUpdateEmbeddedField() {
    if ((getPersistenceUnitServerSession()).getPlatform().isSymfoware()) {
      getPersistenceUnitServerSession()
          .logMessage(
              "Test updateEmbeddedFieldTest skipped for this platform, "
                  + "Symfoware doesn't support UpdateAll/DeleteAll on multi-table objects (see rfe 298193).");
      return;
    }

    EntityManager em = createEntityManager();
    int nrOfEmps =
        ((Number)
                em.createQuery("SELECT COUNT(e) FROM Employee e where e.firstName is not null")
                    .getSingleResult())
            .intValue();

    Metamodel metamodel = em.getMetamodel();
    EntityType<Employee> entityEmp_ = metamodel.entity(Employee.class);
    javax.persistence.metamodel.EmbeddableType<EmploymentPeriod> embedEmpPeriod_ =
        metamodel.embeddable(EmploymentPeriod.class);

    Calendar startCalendar = Calendar.getInstance();
    startCalendar.set(1905, 11, 31, 0, 0, 0);
    java.sql.Date startDate = new java.sql.Date(startCalendar.getTime().getTime());

    // em.createQuery("UPDATE Employee e SET e.period.startDate=
    // :startDate").setParameter("startDate", startDate).executeUpdate();
    CriteriaBuilder qb = em.getCriteriaBuilder();
    CriteriaUpdate<Employee> cq = qb.createCriteriaUpdate(Employee.class);
    Root<Employee> root = cq.from(entityEmp_);
    cq.set(
        root.get(entityEmp_.getSingularAttribute("period", EmploymentPeriod.class))
            .get(embedEmpPeriod_.getSingularAttribute("startDate", java.sql.Date.class)),
        startDate);

    beginTransaction(em);
    try {
      clearCache();

      int updated = em.createQuery(cq).executeUpdate();
      assertEquals(
          "testCriteriaUpdateEmbeddedField: wrong number of updated instances", nrOfEmps, updated);

      // check database changes
      int nr =
          ((Number)
                  em.createQuery(
                          "SELECT COUNT(e) FROM Employee e WHERE e.period.startDate = :startDate")
                      .setParameter("startDate", startDate)
                      .getSingleResult())
              .intValue();
      assertEquals(
          "testCriteriaUpdateEmbeddedField: unexpected number of changed values in the database",
          nrOfEmps,
          nr);
    } finally {
      if (isTransactionActive(em)) {
        rollbackTransaction(em);
      }
      closeEntityManager(em);
    }
  }
Ejemplo n.º 2
0
  @Test
  public void testInjections() throws Exception {
    // Address (embeddable)
    assertNotNull(Address_.address1);
    assertNotNull(Address_.address2);
    assertNotNull(Address_.city);
    final EmbeddableType<Address> addressType = (EmbeddableType<Address>) House_.address.getType();
    assertEquals(addressType.getDeclaredSingularAttribute("address1"), Address_.address1);
    assertEquals(addressType.getDeclaredSingularAttribute("address2"), Address_.address2);
    assertTrue(Address_.address1.isOptional());
    assertFalse(Address_.address2.isOptional());

    // Animal (mapped superclass)
    assertNotNull(Animal_.id);
    assertTrue(Animal_.id.isId());
    assertEquals(Long.class, Animal_.id.getJavaType());
    assertNotNull(Animal_.legNbr);
    //		assertEquals( Integer.class, Animal_.legNbr.getJavaType() );
    assertEquals(int.class, Animal_.legNbr.getJavaType());

    // Cat (hierarchy)
    assertNotNull(Cat_.id);
    assertNotNull(Cat_.id.isId());
    assertEquals(Animal.class, Cat_.id.getJavaMember().getDeclaringClass());
    assertNotNull(Cat_.nickname);

    // FoodItem
    assertNotNull(FoodItem_.version);
    assertTrue(FoodItem_.version.isVersion());

    // Fridge
    assertNotNull(Fridge_.id);
    assertTrue(Fridge_.id.isId());
    assertEquals(Long.class, Fridge_.id.getJavaType());
    assertNotNull(Fridge_.temperature);
    assertEquals("temperature", Fridge_.temperature.getName());
    assertEquals(Fridge.class, Fridge_.temperature.getDeclaringType().getJavaType());
    //		assertEquals( Integer.class, Fridge_.temperature.getJavaType() );
    //		assertEquals( Integer.class, Fridge_.temperature.getBindableJavaType() );
    //		assertEquals( Integer.class, Fridge_.temperature.getType().getJavaType() );
    assertEquals(int.class, Fridge_.temperature.getJavaType());
    assertEquals(int.class, Fridge_.temperature.getBindableJavaType());
    assertEquals(int.class, Fridge_.temperature.getType().getJavaType());
    assertEquals(Bindable.BindableType.SINGULAR_ATTRIBUTE, Fridge_.temperature.getBindableType());
    assertEquals(Type.PersistenceType.BASIC, Fridge_.temperature.getType().getPersistenceType());
    assertEquals(
        Attribute.PersistentAttributeType.BASIC, Fridge_.temperature.getPersistentAttributeType());
    assertFalse(Fridge_.temperature.isId());
    assertFalse(Fridge_.temperature.isOptional());
    assertFalse(Fridge_.temperature.isAssociation());
    assertFalse(Fridge_.temperature.isCollection());
    assertFalse(Fridge_.brand.isOptional());

    // House (embedded id)
    assertNotNull(House_.key);
    assertTrue(House_.key.isId());
    assertEquals(
        Attribute.PersistentAttributeType.EMBEDDED, House_.key.getPersistentAttributeType());
    assertNotNull(House_.address);
    assertEquals(
        Attribute.PersistentAttributeType.EMBEDDED, House_.address.getPersistentAttributeType());
    assertFalse(House_.address.isCollection());
    assertFalse(House_.address.isAssociation());
    assertNotNull(House_.rooms);
    assertTrue(House_.rooms.isAssociation());
    assertTrue(House_.rooms.isCollection());
    assertEquals(
        Attribute.PersistentAttributeType.ELEMENT_COLLECTION,
        House_.rooms.getPersistentAttributeType());
    assertEquals(Room.class, House_.rooms.getBindableJavaType());
    assertEquals(Bindable.BindableType.PLURAL_ATTRIBUTE, House_.rooms.getBindableType());
    assertEquals(Set.class, House_.rooms.getJavaType());
    assertEquals(PluralAttribute.CollectionType.SET, House_.rooms.getCollectionType());
    assertEquals(
        Type.PersistenceType.EMBEDDABLE, House_.rooms.getElementType().getPersistenceType());
    assertNotNull(House_.roomsByName);
    assertEquals(String.class, House_.roomsByName.getKeyJavaType());
    assertEquals(Type.PersistenceType.BASIC, House_.roomsByName.getKeyType().getPersistenceType());
    assertEquals(PluralAttribute.CollectionType.MAP, House_.roomsByName.getCollectionType());
    assertNotNull(House_.roomsBySize);
    assertEquals(
        Type.PersistenceType.EMBEDDABLE, House_.roomsBySize.getElementType().getPersistenceType());
    assertEquals(PluralAttribute.CollectionType.LIST, House_.roomsBySize.getCollectionType());

    // Person (mapped id)
    assertNotNull(Person_.firstName);
    assertNotNull(Person_.lastName);
    assertTrue(Person_.firstName.isId());
    assertTrue(Person_.lastName.isId());
    assertTrue(Person_.lastName.isId());

    // Garden List as bag
    assertNotNull(Garden_.flowers);
  }