Search in sources :

Example 1 with EntityMetamodel

use of org.hibernate.tuple.entity.EntityMetamodel in project midpoint by Evolveum.

the class RUtil method fixCompositeIdentifierInMetaModel.

private static void fixCompositeIdentifierInMetaModel(SessionFactory sessionFactory, Class clazz) {
    ClassMetadata classMetadata = sessionFactory.getClassMetadata(clazz);
    if (classMetadata instanceof AbstractEntityPersister) {
        AbstractEntityPersister persister = (AbstractEntityPersister) classMetadata;
        EntityMetamodel model = persister.getEntityMetamodel();
        IdentifierProperty identifier = model.getIdentifierProperty();
        try {
            Field field = IdentifierProperty.class.getDeclaredField("hasIdentifierMapper");
            field.setAccessible(true);
            field.set(identifier, true);
            field.setAccessible(false);
        } catch (Exception ex) {
            throw new SystemException("Attempt to fix entity meta model with hack failed, reason: " + ex.getMessage(), ex);
        }
    }
}
Also used : ClassMetadata(org.hibernate.metadata.ClassMetadata) Field(java.lang.reflect.Field) SystemException(com.evolveum.midpoint.util.exception.SystemException) IdentifierProperty(org.hibernate.tuple.IdentifierProperty) EntityMetamodel(org.hibernate.tuple.entity.EntityMetamodel) AbstractEntityPersister(org.hibernate.persister.entity.AbstractEntityPersister) SchemaException(com.evolveum.midpoint.util.exception.SchemaException) SystemException(com.evolveum.midpoint.util.exception.SystemException)

Example 2 with EntityMetamodel

use of org.hibernate.tuple.entity.EntityMetamodel in project hibernate-orm by hibernate.

the class LazyBasicFieldNotInitializedTestTask method execute.

public void execute() {
    Session s = getFactory().openSession();
    s.beginTransaction();
    Entity entity = s.get(Entity.class, entityId);
    Assert.assertFalse(Hibernate.isPropertyInitialized(entity, "description"));
    final EntityMetamodel entityMetamodel = ((SessionFactoryImplementor) getFactory()).getEntityPersister(Entity.class.getName()).getEntityMetamodel();
    final boolean[] propertyLaziness = entityMetamodel.getPropertyLaziness();
    assertEquals(1, propertyLaziness.length);
    assertTrue(propertyLaziness[0]);
    // Make sure NonIdentifierAttribute#isLazy is consistent (HHH-10551)
    final NonIdentifierAttribute[] properties = entityMetamodel.getProperties();
    assertEquals(1, properties.length);
    assertTrue(properties[0].isLazy());
    s.getTransaction().commit();
    s.close();
}
Also used : NonIdentifierAttribute(org.hibernate.tuple.NonIdentifierAttribute) EntityMetamodel(org.hibernate.tuple.entity.EntityMetamodel) Session(org.hibernate.Session)

Example 3 with EntityMetamodel

use of org.hibernate.tuple.entity.EntityMetamodel in project hibernate-orm by hibernate.

the class NullableNaturalIdTest method testNaturalIdNullability.

@Test
@TestForIssue(jiraKey = "HHH-10360")
public void testNaturalIdNullability() {
    // A, B, C, and D are mapped using annotations;
    // none are mapped to be non-nullable, so all are nullable by annotations-specific default,
    // except primitives
    EntityPersister persister = sessionFactory().getEntityPersister(A.class.getName());
    EntityMetamodel entityMetamodel = persister.getEntityMetamodel();
    assertTrue(persister.getPropertyNullability()[entityMetamodel.getPropertyIndex("assC")]);
    assertTrue(persister.getPropertyNullability()[entityMetamodel.getPropertyIndex("myname")]);
    persister = sessionFactory().getEntityPersister(B.class.getName());
    entityMetamodel = persister.getEntityMetamodel();
    assertTrue(persister.getPropertyNullability()[entityMetamodel.getPropertyIndex("assA")]);
    // naturalid is a primitive, so it is non-nullable
    assertFalse(persister.getPropertyNullability()[entityMetamodel.getPropertyIndex("naturalid")]);
    persister = sessionFactory().getEntityPersister(C.class.getName());
    entityMetamodel = persister.getEntityMetamodel();
    assertTrue(persister.getPropertyNullability()[entityMetamodel.getPropertyIndex("name")]);
    persister = sessionFactory().getEntityPersister(D.class.getName());
    entityMetamodel = persister.getEntityMetamodel();
    assertTrue(persister.getPropertyNullability()[entityMetamodel.getPropertyIndex("name")]);
    assertTrue(persister.getPropertyNullability()[entityMetamodel.getPropertyIndex("associatedC")]);
    // User is mapped using hbm.xml; properties are explicitly mapped to be nullable
    persister = sessionFactory().getEntityPersister(User.class.getName());
    entityMetamodel = persister.getEntityMetamodel();
    assertTrue(persister.getPropertyNullability()[entityMetamodel.getPropertyIndex("name")]);
    assertTrue(persister.getPropertyNullability()[entityMetamodel.getPropertyIndex("org")]);
    // intVal is a primitive; hbm.xml apparently allows primitive to be nullable
    assertTrue(persister.getPropertyNullability()[entityMetamodel.getPropertyIndex("intVal")]);
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) EntityMetamodel(org.hibernate.tuple.entity.EntityMetamodel) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 4 with EntityMetamodel

use of org.hibernate.tuple.entity.EntityMetamodel in project hibernate-orm by hibernate.

the class ImmutableEntityNaturalIdTest method testNaturalIdNullability.

@Test
@TestForIssue(jiraKey = "HHH-10360")
public void testNaturalIdNullability() {
    final EntityPersister persister = sessionFactory().getEntityPersister(Child.class.getName());
    // nullability is not specified for either properties making up
    // the natural ID, so they should be non-nullable by hbm-specific default
    final EntityMetamodel entityMetamodel = persister.getEntityMetamodel();
    assertFalse(persister.getPropertyNullability()[entityMetamodel.getPropertyIndex("parent")]);
    assertFalse(persister.getPropertyNullability()[entityMetamodel.getPropertyIndex("name")]);
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) EntityMetamodel(org.hibernate.tuple.entity.EntityMetamodel) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 5 with EntityMetamodel

use of org.hibernate.tuple.entity.EntityMetamodel in project hibernate-orm by hibernate.

the class ImmutableEntityNaturalIdTest method testNaturalIdNullability.

@Test
@TestForIssue(jiraKey = "HHH-10360")
public void testNaturalIdNullability() {
    final EntityPersister persister = sessionFactory().getEntityPersister(Building.class.getName());
    final EntityMetamodel entityMetamodel = persister.getEntityMetamodel();
    // nullability is not specified, so they should be nullable by annotations-specific default
    assertTrue(persister.getPropertyNullability()[entityMetamodel.getPropertyIndex("address")]);
    assertTrue(persister.getPropertyNullability()[entityMetamodel.getPropertyIndex("city")]);
    assertTrue(persister.getPropertyNullability()[entityMetamodel.getPropertyIndex("state")]);
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) EntityMetamodel(org.hibernate.tuple.entity.EntityMetamodel) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Aggregations

EntityMetamodel (org.hibernate.tuple.entity.EntityMetamodel)7 EntityPersister (org.hibernate.persister.entity.EntityPersister)5 TestForIssue (org.hibernate.testing.TestForIssue)5 Test (org.junit.Test)5 SchemaException (com.evolveum.midpoint.util.exception.SchemaException)1 SystemException (com.evolveum.midpoint.util.exception.SystemException)1 Field (java.lang.reflect.Field)1 Session (org.hibernate.Session)1 ClassMetadata (org.hibernate.metadata.ClassMetadata)1 AbstractEntityPersister (org.hibernate.persister.entity.AbstractEntityPersister)1 IdentifierProperty (org.hibernate.tuple.IdentifierProperty)1 NonIdentifierAttribute (org.hibernate.tuple.NonIdentifierAttribute)1