Search in sources :

Example 36 with Collection

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

the class FieldAccessedNestedEmbeddableMetadataTest method testEnumTypeInterpretation.

@Test
@FailureExpected(jiraKey = "HHH-9089")
public void testEnumTypeInterpretation() {
    StandardServiceRegistry ssr = new StandardServiceRegistryBuilder().build();
    try {
        final Metadata metadata = new MetadataSources(ssr).addAnnotatedClass(Customer.class).buildMetadata();
        PersistentClass classMetadata = metadata.getEntityBinding(Customer.class.getName());
        Property investmentsProperty = classMetadata.getProperty("investments");
        Collection investmentsValue = (Collection) investmentsProperty.getValue();
        Component investmentMetadata = (Component) investmentsValue.getElement();
        Value descriptionValue = investmentMetadata.getProperty("description").getValue();
        assertEquals(1, descriptionValue.getColumnSpan());
        Column selectable = (Column) descriptionValue.getColumnIterator().next();
        assertEquals(500, selectable.getLength());
        Component amountMetadata = (Component) investmentMetadata.getProperty("amount").getValue();
        SimpleValue currencyMetadata = (SimpleValue) amountMetadata.getProperty("currency").getValue();
        CustomType currencyType = (CustomType) currencyMetadata.getType();
        int[] currencySqlTypes = currencyType.sqlTypes(metadata);
        assertEquals(1, currencySqlTypes.length);
        assertJdbcTypeCode(Types.VARCHAR, currencySqlTypes[0]);
    } finally {
        StandardServiceRegistryBuilder.destroy(ssr);
    }
}
Also used : CustomType(org.hibernate.type.CustomType) StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) Metadata(org.hibernate.boot.Metadata) MetadataSources(org.hibernate.boot.MetadataSources) SimpleValue(org.hibernate.mapping.SimpleValue) Column(org.hibernate.mapping.Column) SimpleValue(org.hibernate.mapping.SimpleValue) Value(org.hibernate.mapping.Value) Collection(org.hibernate.mapping.Collection) Component(org.hibernate.mapping.Component) Property(org.hibernate.mapping.Property) StandardServiceRegistry(org.hibernate.boot.registry.StandardServiceRegistry) PersistentClass(org.hibernate.mapping.PersistentClass) Test(org.junit.Test) FailureExpected(org.hibernate.testing.FailureExpected)

Example 37 with Collection

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

the class FullyQualifiedEntityNameNamingStrategyTest method testManyToManyForeignKeys.

@Test
@TestForIssue(jiraKey = "HHH-9327")
public void testManyToManyForeignKeys() {
    final Collection ownerCollectionMapping = metadata.getCollectionBinding(Category.class.getName() + "." + "items");
    final String expectedOwnerFK = transformEntityName(Category.class.getName()) + "_id";
    final String expectedInverseFK = transformEntityName(Item.class.getName()) + "_items_id";
    boolean ownerFKFound = false;
    boolean inverseFKFound = false;
    for (Iterator it = ownerCollectionMapping.getCollectionTable().getForeignKeyIterator(); it.hasNext(); ) {
        final String fkColumnName = ((ForeignKey) it.next()).getColumn(0).getName();
        if (expectedOwnerFK.equals(fkColumnName)) {
            ownerFKFound = true;
        } else if (expectedInverseFK.equals(fkColumnName)) {
            inverseFKFound = true;
        }
    }
    assertTrue(ownerFKFound);
    assertTrue(inverseFKFound);
}
Also used : Iterator(java.util.Iterator) Collection(org.hibernate.mapping.Collection) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 38 with Collection

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

the class FullyQualifiedEntityNameNamingStrategyTest method testElementCollectionTable.

@Test
@TestForIssue(jiraKey = "HHH-9327")
public void testElementCollectionTable() {
    final Collection collectionMapping = metadata.getCollectionBinding(Workflow.class.getName() + ".localized");
    final String expectedTableName = transformEntityName(Workflow.class.getName()) + "_localized";
    assertEquals(expectedTableName, collectionMapping.getCollectionTable().getName());
}
Also used : Collection(org.hibernate.mapping.Collection) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 39 with Collection

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

the class CollectionJoinTableNamingTest method testCollectionJoinTableNamingLegacyHbmStrategy.

@Test
@TestForIssue(jiraKey = "HHH-9908")
public void testCollectionJoinTableNamingLegacyHbmStrategy() {
    final MetadataSources metadataSources = new MetadataSources();
    try {
        metadataSources.addAnnotatedClass(Input.class);
        metadataSources.addAnnotatedClass(Ptx.class);
        final Metadata metadata = metadataSources.getMetadataBuilder().applyImplicitNamingStrategy(ImplicitNamingStrategyLegacyHbmImpl.INSTANCE).build();
        Collection inputs1Mapping = metadata.getCollectionBinding(Ptx.class.getName() + ".inputs1");
        assertEquals("ptx_inputs1", inputs1Mapping.getCollectionTable().getName());
        Collection inputs2Mapping = metadata.getCollectionBinding(Ptx.class.getName() + ".inputs2");
        assertEquals("ptx_inputs2", inputs2Mapping.getCollectionTable().getName());
    } finally {
        ServiceRegistry metaServiceRegistry = metadataSources.getServiceRegistry();
        if (metaServiceRegistry instanceof BootstrapServiceRegistry) {
            BootstrapServiceRegistryBuilder.destroy(metaServiceRegistry);
        }
    }
}
Also used : MetadataSources(org.hibernate.boot.MetadataSources) Metadata(org.hibernate.boot.Metadata) Collection(org.hibernate.mapping.Collection) BootstrapServiceRegistry(org.hibernate.boot.registry.BootstrapServiceRegistry) ServiceRegistry(org.hibernate.service.ServiceRegistry) BootstrapServiceRegistry(org.hibernate.boot.registry.BootstrapServiceRegistry) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 40 with Collection

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

the class CollectionJoinTableNamingTest method assertSameTableUsed.

private void assertSameTableUsed(Metadata metadata) {
    Collection inputs1Mapping = metadata.getCollectionBinding(Ptx.class.getName() + ".inputs1");
    assertEquals("ptx_input", inputs1Mapping.getCollectionTable().getName());
    Collection inputs2Mapping = metadata.getCollectionBinding(Ptx.class.getName() + ".inputs2");
    assertEquals("ptx_input", inputs2Mapping.getCollectionTable().getName());
    assertSame(inputs1Mapping.getCollectionTable(), inputs2Mapping.getCollectionTable());
    // NOTE : here so that tester can more easily see the produced table. It is only dumped to stdout
    new SchemaExport().create(EnumSet.of(TargetType.STDOUT), metadata);
    for (int i = 0; i < inputs1Mapping.getCollectionTable().getColumnSpan(); i++) {
        final Column column = inputs1Mapping.getCollectionTable().getColumn(i);
        // this, coupled with JPA saying the 2 collections implicitly map to the same table,
        // is the crux of the problem: all columns are null, so we effectively can never
        // insert rows into it.
        assertFalse(column.isNullable());
    }
}
Also used : Column(org.hibernate.mapping.Column) OrderColumn(javax.persistence.OrderColumn) Collection(org.hibernate.mapping.Collection) SchemaExport(org.hibernate.tool.hbm2ddl.SchemaExport)

Aggregations

Collection (org.hibernate.mapping.Collection)42 PersistentClass (org.hibernate.mapping.PersistentClass)19 Test (org.junit.Test)15 Property (org.hibernate.mapping.Property)11 Metadata (org.hibernate.boot.Metadata)9 MetadataSources (org.hibernate.boot.MetadataSources)9 Column (org.hibernate.mapping.Column)9 Iterator (java.util.Iterator)7 Component (org.hibernate.mapping.Component)7 RootClass (org.hibernate.mapping.RootClass)6 AnnotationException (org.hibernate.AnnotationException)5 SimpleValue (org.hibernate.mapping.SimpleValue)5 TestForIssue (org.hibernate.testing.TestForIssue)5 List (java.util.List)4 Map (java.util.Map)4 ElementCollection (javax.persistence.ElementCollection)4 StandardServiceRegistry (org.hibernate.boot.registry.StandardServiceRegistry)4 StandardServiceRegistryBuilder (org.hibernate.boot.registry.StandardServiceRegistryBuilder)4 Value (org.hibernate.mapping.Value)4 ArrayList (java.util.ArrayList)3