public static BytecodeEnhancementMetadata from(PersistentClass persistentClass) { final Class mappedClass = persistentClass.getMappedClass(); final boolean enhancedForLazyLoading = PersistentAttributeInterceptable.class.isAssignableFrom( mappedClass ); final LazyAttributesMetadata lazyAttributesMetadata = enhancedForLazyLoading ? LazyAttributesMetadata.from( persistentClass ) : LazyAttributesMetadata.nonEnhanced( persistentClass.getEntityName() ); return new BytecodeEnhancementMetadataPojoImpl( persistentClass.getEntityName(), mappedClass, enhancedForLazyLoading, lazyAttributesMetadata ); }
public void createPrimaryKey() { //Primary key constraint final Table table = getTable(); PrimaryKey pk = new PrimaryKey( table ); pk.setName( PK_ALIAS.toAliasString( table.getName() ) ); table.setPrimaryKey( pk ); pk.addColumns( getKey().getColumnIterator() ); }
final String element = (String) st.nextElement(); if ( property == null ) { Property identifierProperty = getIdentifierProperty(); if ( identifierProperty != null && identifierProperty.getName().equals( element ) ) { else if ( identifierProperty == null && getIdentifierMapper() != null ) { identifierProperty = getProperty( element, getIdentifierMapper().getPropertyIterator() ); if ( identifierProperty != null ) { property = getProperty( element, iter ); property = ( (Component) property.getValue() ).getProperty( element ); throw new MappingException( "property [" + propertyPath + "] not found on entity [" + getEntityName() + "]" );
public OneToOne(MetadataBuildingContext buildingContext, Table table, PersistentClass owner) throws MappingException { super( buildingContext, table ); this.identifier = owner.getKey(); this.entityName = owner.getEntityName(); }
public EntityNamingSourceImpl(PersistentClass entityBinding) { this( entityBinding.getEntityName(), entityBinding.getClassName(), entityBinding.getJpaEntityName() ); }
private PersistentClass getCommonPersistentClass(PersistentClass clazz1, PersistentClass clazz2) { while ( clazz2 != null && clazz2.getMappedClass() != null && clazz1.getMappedClass() != null && !clazz2.getMappedClass() .isAssignableFrom( clazz1.getMappedClass() ) ) { clazz2 = clazz2.getSuperclass(); } return clazz2; }
protected void validateCustomer(Metadata metadata) { final PersistentClass customerBinding = metadata.getEntityBinding( Customer.class.getName() ); assertNotNull( customerBinding ); validateCustomerPrimaryTableName( customerBinding.getTable().getQuotedName() ); assertEquals( 1, customerBinding.getIdentifier().getColumnSpan() ); validateCustomerPrimaryKeyColumn( (Column) customerBinding.getIdentifier().getColumnIterator().next() ); assertNotNull( customerBinding.getVersion() ); assertEquals( 1, customerBinding.getVersion().getColumnSpan() ); validateCustomerVersionColumn( (Column) customerBinding.getVersion().getColumnIterator().next() ); final Property nameBinding = customerBinding.getProperty( "name" ); assertNotNull( nameBinding ); assertEquals( 1, nameBinding.getColumnSpan() ); validateCustomerNameColumn( (Column) nameBinding.getColumnIterator().next() ); final Property hqAddressBinding = customerBinding.getProperty( "hqAddress" ); assertNotNull( hqAddressBinding ); assertEquals( 3, hqAddressBinding.getColumnSpan() ); validateCustomerHqAddressComponent( assertTyping( Component.class, hqAddressBinding.getValue() ) ); }
name = persistentClass.getEntityName(); rootName = persistentClass.getRootClass().getEntityName(); versioned = persistentClass.isVersioned(); if ( persistentClass.hasPojoRepresentation() ) { bytecodeEnhancementMetadata = BytecodeEnhancementMetadataPojoImpl.from( persistentClass ); bytecodeEnhancementMetadata = new BytecodeEnhancementMetadataNonPojoImpl( persistentClass.getEntityName() ); propertySpan = persistentClass.getPropertyClosureSpan(); properties = new NonIdentifierAttribute[propertySpan]; List<Integer> naturalIdNumbers = new ArrayList<>(); Iterator iter = persistentClass.getPropertyClosureIterator(); int i = 0; int tempVersionProperty = NO_VERSION_INDX; if ( prop == persistentClass.getVersion() ) { tempVersionProperty = i; properties[i] = PropertyFactory.buildVersionProperty( naturalIdPropertyNumbers = ArrayHelper.toIntArray(naturalIdNumbers); hasImmutableNaturalId = !foundUpdateableNaturalIdProperty; hasCacheableNaturalId = persistentClass.getNaturalIdCacheRegionName() != null; lazy = persistentClass.isLazy() && ( !persistentClass.hasPojoRepresentation() ||
); 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( for ( Iterator it=ownerCollection.getCollectionTable().getForeignKeyIterator(); it.hasNext(); ) { final ForeignKey fk = (ForeignKey) it.next(); assertSame( ownerCollection.getCollectionTable(), fk.getTable() ); assertSame( ownerCollection.getOwner().getTable(), fk.getReferencedTable() ); hasOwnerFK = true; assertSame( associatedPersistentClass.getTable(), fk.getReferencedTable() ); hasInverseFK = true;
Class mappedClass = persistentClass.getMappedClass(); Class proxyInterface = persistentClass.getProxyInterface(); Iterator<Subclass> subclasses = persistentClass.getSubclassIterator(); while ( subclasses.hasNext() ) { final Subclass subclass = subclasses.next(); Iterator properties = persistentClass.getPropertyIterator(); Class clazz = persistentClass.getMappedClass(); while ( properties.hasNext() ) { Property property = (Property) properties.next(); Method method = property.getGetter( clazz ).getMethod(); if ( method != null && Modifier.isFinal( method.getModifiers() ) ) { LOG.gettersOfLazyClassesCannotBeFinal( persistentClass.getEntityName(), property.getName() ); method = property.getSetter( clazz ).getMethod(); if ( method != null && Modifier.isFinal( method.getModifiers() ) ) { LOG.settersOfLazyClassesCannotBeFinal( persistentClass.getEntityName(), property.getName() ); proxyGetIdentifierMethod, proxySetIdentifierMethod, persistentClass.hasEmbeddedIdentifier() ? (CompositeType) persistentClass.getIdentifier().getType() : null );
public void validate(Mapping mapping) throws MappingException { Iterator iter = getPropertyIterator(); while ( iter.hasNext() ) { Property prop = (Property) iter.next(); if ( !prop.isValid( mapping ) ) { throw new MappingException( "property mapping has wrong number of columns: " + StringHelper.qualify( getEntityName(), prop.getName() ) + " type: " + prop.getType().getName() ); } } checkPropertyDuplication(); checkColumnDuplication(); }
@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 ); } } }
@Override public void doTestWork(StandardServiceRegistry ssr) { MetadataImplementor metadata = (MetadataImplementor) new MetadataSources( ssr ) .addAnnotatedClass( E1.class ) .buildMetadata(); metadata.validate(); PersistentClass entityBinding = metadata.getEntityBinding( E1.class.getName() ); org.hibernate.mapping.Column idColumn = extractColumn( entityBinding.getIdentifier().getColumnIterator() ); assertTrue( isQuoted( idColumn.getSqlType(), ssr ) ); org.hibernate.mapping.Column otherColumn = extractColumn( entityBinding.getProperty( "other" ).getColumnIterator() ); assertTrue( isQuoted( otherColumn.getSqlType(), ssr ) ); } }
protected void checkDefaultJoinTableAndAllColumnNames( Metadata metadata, Class<?> ownerEntityClass, String ownerCollectionPropertyName, String expectedCollectionTableName, String ownerForeignKeyNameExpected, String[] columnNames) { final org.hibernate.mapping.Collection collection = metadata.getCollectionBinding( ownerEntityClass.getName() + '.' + ownerCollectionPropertyName ); final org.hibernate.mapping.Table table = collection.getCollectionTable(); assertEquals( expectedCollectionTableName, table.getName() ); // The default owner and inverse join columns can only be computed if they have PK with 1 column. assertEquals( 1, collection.getOwner().getKey().getColumnSpan() ); assertEquals( ownerForeignKeyNameExpected, collection.getKey().getColumnIterator().next().getText() ); int columnNumber = table.getColumnSpan(); for ( int i = 0; i < columnNumber; i++ ) { assertEquals( columnNames[i], table.getColumn( i + 1 ).getName()); } }
protected void createBackReferences() { if ( collectionBinding.isOneToMany() && !collectionBinding.isInverse() && !collectionBinding.getKey().isNullable() ) { // for non-inverse one-to-many, with a not-null fk, add a backref! String entityName = ( (OneToMany) collectionBinding.getElement() ).getReferencedEntityName(); PersistentClass referenced = mappingDocument.getMetadataCollector().getEntityBinding( entityName ); Backref prop = new Backref(); prop.setName( '_' + collectionBinding.getOwnerEntityName() + "." + pluralAttributeSource.getName() + "Backref" ); prop.setUpdateable( false ); prop.setSelectable( false ); prop.setCollectionRole( collectionBinding.getRole() ); prop.setEntityName( collectionBinding.getOwner().getEntityName() ); prop.setValue( collectionBinding.getKey() ); referenced.addProperty( prop ); log.debugf( "Added virtual backref property [%s] : %s", prop.getName(), pluralAttributeSource.getAttributeRole().getFullPath() ); } }
final String mappedBy = columns[0].getMappedBy(); if ( StringHelper.isNotEmpty( mappedBy ) ) { final Property property = referencedEntity.getRecursiveProperty( mappedBy ); Iterator mappedByColumns; if ( property.getValue() instanceof Collection ) { mappedByColumns = ( (Collection) property.getValue() ).getKey().getColumnIterator(); Iterator joinsIt = referencedEntity.getJoinIterator(); KeyValue key = null; while ( joinsIt.hasNext() ) { if ( key == null ) key = property.getPersistentClass().getIdentifier(); mappedByColumns = key.getColumnIterator(); "inverse__" + referencedEntity.getEntityName(), mappedBy ); if ( referencedPropertyName != null ) { referencedEntity.getEntityName(), referencedPropertyName );
@SuppressWarnings({"unchecked"}) private String searchMappedBy(PersistentClass referencedClass, Collection collectionValue) { final Iterator<Property> assocClassProps = referencedClass.getPropertyIterator(); while ( assocClassProps.hasNext() ) { final Property property = assocClassProps.next(); if ( Tools.iteratorsContentEqual( property.getValue().getColumnIterator(), collectionValue.getKey().getColumnIterator() ) ) { return property.getName(); } } // HHH-7625 // Support ToOne relations with mappedBy that point to an @IdClass key property. return searchMappedByKey( referencedClass, collectionValue ); }
private Iterator getSubPropertyIterator(PersistentClass pc, String reducedName) { Value value = pc.getRecursiveProperty( reducedName ).getValue(); Iterator parentPropIter; if ( value instanceof Component ) { Component comp = (Component) value; parentPropIter = comp.getPropertyIterator(); if ( toOne.getReferencedPropertyName() != null ) { try { parentPropIter = ( (Component) referencedPc.getRecursiveProperty( toOne.getReferencedPropertyName() ).getValue() ).getPropertyIterator(); throw new MappingException( "dotted notation reference neither a component nor a many/one to one", e ); if ( referencedPc.getIdentifierMapper() == null ) { parentPropIter = ( (Component) referencedPc.getIdentifierProperty() .getValue() ).getPropertyIterator(); parentPropIter = referencedPc.getIdentifierMapper().getPropertyIterator(); throw new MappingException( "dotted notation reference neither a component nor a many/one to one", e ); throw new MappingException( "dotted notation reference neither a component nor a many/one to one" );
private void checkPropertyDuplication() throws MappingException { HashSet<String> names = new HashSet<>(); Iterator iter = getPropertyIterator(); while ( iter.hasNext() ) { Property prop = (Property) iter.next(); if ( !names.add( prop.getName() ) ) { throw new MappingException( "Duplicate property mapping of " + prop.getName() + " found in " + getEntityName() ); } } }
protected void checkDefaultJoinColumnName( Class<?> ownerEntityClass, String ownerCollectionPropertyName, String ownerForeignKeyNameExpected) { final org.hibernate.mapping.Collection ownerCollection = metadata().getCollectionBinding( ownerEntityClass.getName() + '.' + ownerCollectionPropertyName ); // The default owner join column can only be computed if it has a PK with 1 column. assertEquals ( 1, ownerCollection.getOwner().getKey().getColumnSpan() ); assertEquals( ownerForeignKeyNameExpected, ownerCollection.getKey().getColumnIterator().next().getText() ); boolean hasOwnerFK = 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; } } assertTrue( hasOwnerFK ); }