Search in sources :

Example 76 with Collection

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

the class ElementCollectionTests method testSimpleConvertUsage.

@Test
public void testSimpleConvertUsage() throws MalformedURLException {
    // first some assertions of the metamodel
    PersistentClass entityBinding = metadata().getEntityBinding(TheEntity.class.getName());
    assertNotNull(entityBinding);
    Property setAttributeBinding = entityBinding.getProperty("set");
    Collection setBinding = (Collection) setAttributeBinding.getValue();
    assertTyping(AttributeConverterTypeAdapter.class, setBinding.getElement().getType());
    Property mapAttributeBinding = entityBinding.getProperty("map");
    IndexedCollection mapBinding = (IndexedCollection) mapAttributeBinding.getValue();
    assertTyping(AttributeConverterTypeAdapter.class, mapBinding.getIndex().getType());
    assertTyping(AttributeConverterTypeAdapter.class, mapBinding.getElement().getType());
    // now lets try to use the model, integration-testing-style!
    TheEntity entity = new TheEntity(1);
    Session s = openSession();
    s.beginTransaction();
    s.save(entity);
    s.getTransaction().commit();
    s.close();
    s = openSession();
    s.beginTransaction();
    TheEntity retrieved = (TheEntity) s.load(TheEntity.class, 1);
    assertEquals(1, retrieved.getSet().size());
    assertEquals(new ValueType("set_value"), retrieved.getSet().iterator().next());
    assertEquals(1, retrieved.getMap().size());
    assertEquals(new ValueType("map_value"), retrieved.getMap().get(new ValueType("map_key")));
    s.delete(retrieved);
    s.getTransaction().commit();
    s.close();
}
Also used : IndexedCollection(org.hibernate.mapping.IndexedCollection) Collection(org.hibernate.mapping.Collection) ElementCollection(javax.persistence.ElementCollection) Property(org.hibernate.mapping.Property) IndexedCollection(org.hibernate.mapping.IndexedCollection) PersistentClass(org.hibernate.mapping.PersistentClass) Session(org.hibernate.Session) Test(org.junit.Test)

Example 77 with Collection

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

the class NestedEmbeddableMetadataTest method testEnumTypeInterpretation.

@Test
public void testEnumTypeInterpretation() {
    final StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().enableAutoClose().applySetting(AvailableSettings.HBM2DDL_AUTO, "create-drop").build();
    try {
        final Metadata metadata = new MetadataSources(serviceRegistry).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(serviceRegistry);
    }
}
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)

Example 78 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 79 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 80 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)

Aggregations

Collection (org.hibernate.mapping.Collection)134 Test (org.junit.jupiter.api.Test)80 Bag (org.hibernate.mapping.Bag)61 IdentifierBag (org.hibernate.mapping.IdentifierBag)61 SimpleValue (org.hibernate.mapping.SimpleValue)28 Table (org.hibernate.mapping.Table)27 Set (org.hibernate.mapping.Set)25 ITable (org.jboss.tools.hibernate.runtime.spi.ITable)24 Test (org.junit.Test)23 PersistentClass (org.hibernate.mapping.PersistentClass)22 ManyToOne (org.hibernate.mapping.ManyToOne)14 KeyValue (org.hibernate.mapping.KeyValue)13 IValue (org.jboss.tools.hibernate.runtime.spi.IValue)12 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 AbstractValueFacade (org.jboss.tools.hibernate.runtime.common.AbstractValueFacade)7