Search in sources :

Example 1 with PropertyHolder

use of org.hibernate.cfg.PropertyHolder in project hibernate-orm by hibernate.

the class ListBinder method bindIndex.

private void bindIndex(final MetadataBuildingContext buildingContext) {
    if (!indexColumn.isImplicit()) {
        PropertyHolder valueHolder = PropertyHolderBuilder.buildPropertyHolder(this.collection, StringHelper.qualify(this.collection.getRole(), "key"), null, null, propertyHolder, getBuildingContext());
        List list = (List) this.collection;
        if (!list.isOneToMany())
            indexColumn.forceNotNull();
        indexColumn.setPropertyHolder(valueHolder);
        SimpleValueBinder value = new SimpleValueBinder();
        value.setColumns(new Ejb3Column[] { indexColumn });
        value.setExplicitType("integer");
        value.setBuildingContext(getBuildingContext());
        SimpleValue indexValue = value.make();
        indexColumn.linkWithValue(indexValue);
        list.setIndex(indexValue);
        list.setBaseIndex(indexColumn.getBase());
        if (list.isOneToMany() && !list.getKey().isNullable() && !list.isInverse()) {
            String entityName = ((OneToMany) list.getElement()).getReferencedEntityName();
            PersistentClass referenced = buildingContext.getMetadataCollector().getEntityBinding(entityName);
            IndexBackref ib = new IndexBackref();
            ib.setName('_' + propertyName + "IndexBackref");
            ib.setUpdateable(false);
            ib.setSelectable(false);
            ib.setCollectionRole(list.getRole());
            ib.setEntityName(list.getOwner().getEntityName());
            ib.setValue(list.getIndex());
            referenced.addProperty(ib);
        }
    } else {
        Collection coll = this.collection;
        throw new AnnotationException("List/array has to be annotated with an @OrderColumn (or @IndexColumn): " + coll.getRole());
    }
}
Also used : PropertyHolder(org.hibernate.cfg.PropertyHolder) Collection(org.hibernate.mapping.Collection) AnnotationException(org.hibernate.AnnotationException) List(org.hibernate.mapping.List) IndexBackref(org.hibernate.mapping.IndexBackref) OneToMany(org.hibernate.mapping.OneToMany) SimpleValue(org.hibernate.mapping.SimpleValue) PersistentClass(org.hibernate.mapping.PersistentClass)

Example 2 with PropertyHolder

use of org.hibernate.cfg.PropertyHolder in project hibernate-orm by hibernate.

the class CollectionBinderTest method testAssociatedClassException.

@Test
@TestForIssue(jiraKey = "HHH-10106")
public void testAssociatedClassException() throws SQLException {
    final Collection collection = mock(Collection.class);
    final Map persistentClasses = mock(Map.class);
    final XClass collectionType = mock(XClass.class);
    final MetadataBuildingContext buildingContext = mock(MetadataBuildingContext.class);
    final InFlightMetadataCollector inFly = mock(InFlightMetadataCollector.class);
    final PersistentClass persistentClass = mock(PersistentClass.class);
    final Table table = mock(Table.class);
    when(buildingContext.getMetadataCollector()).thenReturn(inFly);
    when(persistentClasses.get(null)).thenReturn(null);
    when(collection.getOwner()).thenReturn(persistentClass);
    when(collectionType.getName()).thenReturn("List");
    when(persistentClass.getTable()).thenReturn(table);
    when(table.getName()).thenReturn("Hibernate");
    CollectionBinder collectionBinder = new CollectionBinder(false) {

        @Override
        protected Collection createCollection(PersistentClass persistentClass) {
            return null;
        }

        {
            final PropertyHolder propertyHolder = Mockito.mock(PropertyHolder.class);
            when(propertyHolder.getClassName()).thenReturn(CollectionBinderTest.class.getSimpleName());
            this.propertyName = "abc";
            this.propertyHolder = propertyHolder;
        }
    };
    String expectMessage = "Association [abc] for entity [CollectionBinderTest] references unmapped class [List]";
    try {
        collectionBinder.bindOneToManySecondPass(collection, persistentClasses, null, collectionType, false, false, buildingContext, null);
    } catch (MappingException e) {
        assertEquals(expectMessage, e.getMessage());
    }
}
Also used : InFlightMetadataCollector(org.hibernate.boot.spi.InFlightMetadataCollector) Table(org.hibernate.mapping.Table) PropertyHolder(org.hibernate.cfg.PropertyHolder) Collection(org.hibernate.mapping.Collection) MetadataBuildingContext(org.hibernate.boot.spi.MetadataBuildingContext) Map(java.util.Map) XClass(org.hibernate.annotations.common.reflection.XClass) PersistentClass(org.hibernate.mapping.PersistentClass) MappingException(org.hibernate.MappingException) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Aggregations

PropertyHolder (org.hibernate.cfg.PropertyHolder)2 Collection (org.hibernate.mapping.Collection)2 PersistentClass (org.hibernate.mapping.PersistentClass)2 Map (java.util.Map)1 AnnotationException (org.hibernate.AnnotationException)1 MappingException (org.hibernate.MappingException)1 XClass (org.hibernate.annotations.common.reflection.XClass)1 InFlightMetadataCollector (org.hibernate.boot.spi.InFlightMetadataCollector)1 MetadataBuildingContext (org.hibernate.boot.spi.MetadataBuildingContext)1 IndexBackref (org.hibernate.mapping.IndexBackref)1 List (org.hibernate.mapping.List)1 OneToMany (org.hibernate.mapping.OneToMany)1 SimpleValue (org.hibernate.mapping.SimpleValue)1 Table (org.hibernate.mapping.Table)1 TestForIssue (org.hibernate.testing.TestForIssue)1 Test (org.junit.Test)1