Search in sources :

Example 36 with PersistentClass

use of org.hibernate.mapping.PersistentClass in project hibernate-orm by hibernate.

the class DiscriminatorOptionsTest method testPropertyBasedDiscriminatorForcing.

@Test
public void testPropertyBasedDiscriminatorForcing() throws Exception {
    final StandardServiceRegistry ssr = new StandardServiceRegistryBuilder().build();
    try {
        Metadata metadata = new MetadataSources(ssr).addAnnotatedClass(BaseClass2.class).addAnnotatedClass(SubClass2.class).getMetadataBuilder().enableImplicitForcingOfDiscriminatorsInSelect(true).build();
        PersistentClass persistentClass = metadata.getEntityBinding(BaseClass2.class.getName());
        assertNotNull(persistentClass);
        assertTrue(persistentClass instanceof RootClass);
        RootClass root = (RootClass) persistentClass;
        assertTrue("Discriminator should be forced by property", root.isForceDiscriminator());
    } finally {
        StandardServiceRegistryBuilder.destroy(ssr);
    }
}
Also used : RootClass(org.hibernate.mapping.RootClass) StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) Metadata(org.hibernate.boot.Metadata) MetadataSources(org.hibernate.boot.MetadataSources) StandardServiceRegistry(org.hibernate.boot.registry.StandardServiceRegistry) PersistentClass(org.hibernate.mapping.PersistentClass) Test(org.junit.Test)

Example 37 with PersistentClass

use of org.hibernate.mapping.PersistentClass in project hibernate-orm by hibernate.

the class ManyToManyImplicitNamingTest method checkDefaultJoinTablAndJoinColumnNames.

protected void checkDefaultJoinTablAndJoinColumnNames(Class<?> ownerEntityClass, String ownerCollectionPropertyName, String inverseCollectionPropertyName, String expectedCollectionTableName, String ownerForeignKeyNameExpected, String inverseForeignKeyNameExpected) {
    final org.hibernate.mapping.Collection collection = metadata().getCollectionBinding(ownerEntityClass.getName() + '.' + ownerCollectionPropertyName);
    final org.hibernate.mapping.Table table = collection.getCollectionTable();
    assertEquals(expectedCollectionTableName, table.getName());
    final org.hibernate.mapping.Collection ownerCollection = metadata().getCollectionBinding(ownerEntityClass.getName() + '.' + ownerCollectionPropertyName);
    // The default owner and inverse join columns can only be computed if they have PK with 1 column.
    assertEquals(1, ownerCollection.getOwner().getKey().getColumnSpan());
    assertEquals(ownerForeignKeyNameExpected, ownerCollection.getKey().getColumnIterator().next().getText());
    final EntityType associatedEntityType = (EntityType) ownerCollection.getElement().getType();
    final PersistentClass associatedPersistentClass = metadata().getEntityBinding(associatedEntityType.getAssociatedEntityName());
    assertEquals(1, associatedPersistentClass.getKey().getColumnSpan());
    if (inverseCollectionPropertyName != null) {
        final org.hibernate.mapping.Collection inverseCollection = metadata().getCollectionBinding(associatedPersistentClass.getEntityName() + '.' + inverseCollectionPropertyName);
        assertEquals(inverseForeignKeyNameExpected, inverseCollection.getKey().getColumnIterator().next().getText());
    }
    boolean hasOwnerFK = false;
    boolean hasInverseFK = false;
    for (Iterator it = ownerCollection.getCollectionTable().getForeignKeyIterator(); it.hasNext(); ) {
        final ForeignKey fk = (ForeignKey) it.next();
        assertSame(ownerCollection.getCollectionTable(), fk.getTable());
        if (fk.getColumnSpan() > 1) {
            continue;
        }
        if (fk.getColumn(0).getText().equals(ownerForeignKeyNameExpected)) {
            assertSame(ownerCollection.getOwner().getTable(), fk.getReferencedTable());
            hasOwnerFK = true;
        } else if (fk.getColumn(0).getText().equals(inverseForeignKeyNameExpected)) {
            assertSame(associatedPersistentClass.getTable(), fk.getReferencedTable());
            hasInverseFK = true;
        }
    }
    assertTrue(hasOwnerFK);
    assertTrue(hasInverseFK);
}
Also used : EntityType(org.hibernate.type.EntityType) Iterator(java.util.Iterator) ForeignKey(org.hibernate.mapping.ForeignKey) PersistentClass(org.hibernate.mapping.PersistentClass)

Example 38 with PersistentClass

use of org.hibernate.mapping.PersistentClass in project hibernate-orm by hibernate.

the class AbstractJPAIndexTest method testSecondaryTableIndex.

@Test
public void testSecondaryTableIndex() {
    PersistentClass entity = metadata().getEntityBinding(Car.class.getName());
    Join join = (Join) entity.getJoinIterator().next();
    Iterator<Index> itr = join.getTable().getIndexIterator();
    assertTrue(itr.hasNext());
    Index index = itr.next();
    assertFalse(itr.hasNext());
    assertTrue("index name is not generated", StringHelper.isNotEmpty(index.getName()));
    assertEquals(2, index.getColumnSpan());
    Iterator<Column> columnIterator = index.getColumnIterator();
    Column column = columnIterator.next();
    assertEquals("dealer_name", column.getName());
    column = columnIterator.next();
    assertEquals("rate", column.getName());
    assertSame(join.getTable(), index.getTable());
}
Also used : Column(org.hibernate.mapping.Column) Join(org.hibernate.mapping.Join) Index(org.hibernate.mapping.Index) PersistentClass(org.hibernate.mapping.PersistentClass) Test(org.junit.Test)

Example 39 with PersistentClass

use of org.hibernate.mapping.PersistentClass in project hibernate-orm by hibernate.

the class PersisterTest method testEntityEntityPersisterAndPersisterSpecified.

@Test
public void testEntityEntityPersisterAndPersisterSpecified() throws Exception {
    //checks to see that the persister specified with the @Persister annotation takes precedence if a @Entity.persister() is also specified		
    PersistentClass persistentClass = metadata().getEntityBinding(Deck.class.getName());
    assertEquals("Incorrect Persister class for " + persistentClass.getMappedClass(), EntityPersister.class, persistentClass.getEntityPersisterClass());
}
Also used : PersistentClass(org.hibernate.mapping.PersistentClass) Test(org.junit.Test)

Example 40 with PersistentClass

use of org.hibernate.mapping.PersistentClass in project hibernate-orm by hibernate.

the class PersisterTest method testEntityEntityPersisterSpecified.

@Test
public void testEntityEntityPersisterSpecified() throws Exception {
    //tests the persister specified with an @Entity.persister()		
    PersistentClass persistentClass = metadata().getEntityBinding(Card.class.getName());
    assertEquals("Incorrect Persister class for " + persistentClass.getMappedClass(), SingleTableEntityPersister.class, persistentClass.getEntityPersisterClass());
}
Also used : PersistentClass(org.hibernate.mapping.PersistentClass) Test(org.junit.Test)

Aggregations

PersistentClass (org.hibernate.mapping.PersistentClass)140 Test (org.junit.Test)70 Property (org.hibernate.mapping.Property)52 MetadataSources (org.hibernate.boot.MetadataSources)36 Column (org.hibernate.mapping.Column)36 Metadata (org.hibernate.boot.Metadata)28 StandardServiceRegistryBuilder (org.hibernate.boot.registry.StandardServiceRegistryBuilder)26 Iterator (java.util.Iterator)25 StandardServiceRegistry (org.hibernate.boot.registry.StandardServiceRegistry)24 SimpleValue (org.hibernate.mapping.SimpleValue)19 MappingException (org.hibernate.MappingException)18 Collection (org.hibernate.mapping.Collection)18 TestForIssue (org.hibernate.testing.TestForIssue)18 Component (org.hibernate.mapping.Component)17 MetadataImplementor (org.hibernate.boot.spi.MetadataImplementor)15 AnnotationException (org.hibernate.AnnotationException)14 RootClass (org.hibernate.mapping.RootClass)14 Table (org.hibernate.mapping.Table)14 HashMap (java.util.HashMap)13 Type (org.hibernate.type.Type)13