private PersistentClass getReferenceCollectionClass(Collection collectionValue) { PersistentClass referencedClass = null; if ( collectionValue.getElement() instanceof OneToMany ) { final OneToMany oneToManyValue = (OneToMany) collectionValue.getElement(); referencedClass = oneToManyValue.getAssociatedClass(); } else if ( collectionValue.getElement() instanceof ManyToOne ) { // Case for bi-directional relation with @JoinTable on the owning @ManyToOne side. final ManyToOne manyToOneValue = (ManyToOne) collectionValue.getElement(); referencedClass = manyToOneValue.getMetadata().getEntityBinding( manyToOneValue.getReferencedEntityName() ); } return referencedClass; }
private void makeOneToManyMapKeyColumnNullableIfNotInProperty( final XProperty property) { final org.hibernate.mapping.Map map = (org.hibernate.mapping.Map) this.collection; if ( map.isOneToMany() && property.isAnnotationPresent( MapKeyColumn.class ) ) { final Value indexValue = map.getIndex(); if ( indexValue.getColumnSpan() != 1 ) { throw new AssertionFailure( "Map key mapped by @MapKeyColumn does not have 1 column" ); } final Selectable selectable = indexValue.getColumnIterator().next(); if ( selectable.isFormula() ) { throw new AssertionFailure( "Map key mapped by @MapKeyColumn is a Formula" ); } Column column = (Column) map.getIndex().getColumnIterator().next(); if ( !column.isNullable() ) { final PersistentClass persistentClass = ( ( OneToMany ) map.getElement() ).getAssociatedClass(); // check if the index column has been mapped by the associated entity to a property; // @MapKeyColumn only maps a column to the primary table for the one-to-many, so we only // need to check "un-joined" properties. if ( !propertyIteratorContainsColumn( persistentClass.getUnjoinedPropertyIterator(), column ) ) { // The index column is not mapped to an associated entity property so we can // safely make the index column nullable. column.setNullable( true ); } } } }
public String getClassName(OneToMany om) { return om.getAssociatedClass().getClassName(); }
public Object accept(OneToMany value) { return value.getAssociatedClass().getEntityName(); }
public Object accept(OneToMany value) { return value.getAssociatedClass().getEntityName(); }
public Object accept(OneToMany value) { return value.getAssociatedClass().getClassName(); }
public String getClassName(OneToMany om) { return om.getAssociatedClass().getClassName(); }
public Object accept(OneToMany value) { return value.getAssociatedClass().getClassName(); }
@SuppressWarnings("unchecked") private String findMappedByProperty(Collection collection) { String ownerEntityName = collection.getOwnerEntityName(); Iterator<Column> columnIterator = collection.getKey().getColumnIterator(); List<String> columnNames = new ArrayList<>(); while (columnIterator.hasNext()) { Column column = columnIterator.next(); columnNames.add(column.getName()); } OneToMany oneToMany = (OneToMany) collection.getElement(); return findMappedByProperty(collection, ownerEntityName, columnNames, oneToMany.getAssociatedClass().getPropertyIterator()); }
@SuppressWarnings("unchecked") private String findMappedByProperty(Collection collection) { String ownerEntityName = collection.getOwnerEntityName(); Iterator<Column> columnIterator = collection.getKey().getColumnIterator(); List<String> columnNames = new ArrayList<>(); while (columnIterator.hasNext()) { Column column = columnIterator.next(); columnNames.add(column.getName()); } OneToMany oneToMany = (OneToMany) collection.getElement(); Iterator propertyIterator = oneToMany.getAssociatedClass().getPropertyIterator(); while (propertyIterator.hasNext()) { Property property = (Property) propertyIterator.next(); if (property.getValue() instanceof ToOne) { ToOne toOne = (ToOne) property.getValue(); if (ownerEntityName.equals(toOne.getReferencedEntityName()) && matches(columnNames, collection.getKey().getColumnIterator())) { return property.getName(); } } } return null; }
private PersistentClass getReferenceCollectionClass(Collection collectionValue) { PersistentClass referencedClass = null; if ( collectionValue.getElement() instanceof OneToMany ) { final OneToMany oneToManyValue = (OneToMany) collectionValue.getElement(); referencedClass = oneToManyValue.getAssociatedClass(); } else if ( collectionValue.getElement() instanceof ManyToOne ) { // Case for bi-directional relation with @JoinTable on the owning @ManyToOne side. final ManyToOne manyToOneValue = (ManyToOne) collectionValue.getElement(); referencedClass = manyToOneValue.getMetadataBuildingContext().getMetadataCollector().getEntityBinding( manyToOneValue.getReferencedEntityName() ); } return referencedClass; }
private void makeOneToManyMapKeyColumnNullableIfNotInProperty( final XProperty property) { final org.hibernate.mapping.Map map = (org.hibernate.mapping.Map) this.collection; if ( map.isOneToMany() && property.isAnnotationPresent( MapKeyColumn.class ) ) { final Value<?> indexValue = map.getIndex(); if ( indexValue.getColumnSpan() != 1 ) { throw new AssertionFailure( "Map key mapped by @MapKeyColumn does not have 1 column" ); } final MappedColumn mappedColumn = indexValue.getMappedColumns().get( 0 ); if ( mappedColumn.isFormula() ) { throw new AssertionFailure( "Map key mapped by @MapKeyColumn is a Formula" ); } final Column column = (Column) map.getIndex().getMappedColumns().get( 0 ); if ( !column.isNullable() ) { final PersistentClass persistentClass = ( ( OneToMany ) map.getElement() ).getAssociatedClass(); // check if the index column has been mapped by the associated entity to a property; // @MapKeyColumn only maps a column to the primary table for the one-to-many, so we only // need to check "un-joined" properties. if ( !propertyIteratorContainsColumn( persistentClass.getUnjoinedPropertyIterator(), column ) ) { // The index column is not mapped to an associated entity property so we can // safely make the index column nullable. column.setNullable( true ); } } } }
@Test public void testCreateOneToMany() { PersistentClass orders = metadata.getEntityBinding(PACKAGE_NAME + ".Orders"); Property itemset = orders.getProperty("itemsForRelatedOrderId"); Collection col = (Collection) itemset.getValue(); OneToMany otm = (OneToMany) col.getElement(); Assert.assertEquals(otm.getReferencedEntityName(), PACKAGE_NAME + ".Item"); Assert.assertEquals(otm.getAssociatedClass().getClassName(), PACKAGE_NAME + ".Item"); Assert.assertEquals(otm.getTable().getName(), "ORDERS"); Assert.assertNotNull(itemset); Assert.assertTrue(itemset.getValue() instanceof Set); }