📄 entitybinder.java
字号:
String constraints, Table denormalizedSuperclassTable ) { String logicalName = StringHelper.isNotEmpty( tableName ) ? tableName : StringHelper.unqualify( name ); Table table = TableBinder.fillTable( schema, catalog, getClassTableName( tableName ), logicalName, persistentClass.isAbstract(), uniqueConstraints, constraints, denormalizedSuperclassTable, mappings ); if ( persistentClass instanceof TableOwner ) { if ( log.isInfoEnabled() ) { log.info( "Bind entity " + persistentClass.getEntityName() + " on table " + table.getName() ); } ( (TableOwner) persistentClass ).setTable( table ); } else { throw new AssertionFailure( "binding a table for a subclass" ); } } public void finalSecondaryTableBinding(PropertyHolder propertyHolder) { /* * Those operations has to be done after the id definition of the persistence class. * ie after the properties parsing */ Iterator joins = secondaryTables.values().iterator(); Iterator joinColumns = secondaryTableJoins.values().iterator(); while ( joins.hasNext() ) { Object uncastedColumn = joinColumns.next(); Join join = (Join) joins.next(); createPrimaryColumnsToSecondaryTable( uncastedColumn, propertyHolder, join ); } mappings.addJoins( persistentClass, secondaryTables ); } private void createPrimaryColumnsToSecondaryTable(Object uncastedColumn, PropertyHolder propertyHolder, Join join) { Ejb3JoinColumn[] ejb3JoinColumns; PrimaryKeyJoinColumn[] pkColumnsAnn = null; JoinColumn[] joinColumnsAnn = null; if ( uncastedColumn instanceof PrimaryKeyJoinColumn[] ) { pkColumnsAnn = (PrimaryKeyJoinColumn[]) uncastedColumn; } if ( uncastedColumn instanceof JoinColumn[] ) { joinColumnsAnn = (JoinColumn[]) uncastedColumn; } if ( pkColumnsAnn == null && joinColumnsAnn == null ) { ejb3JoinColumns = new Ejb3JoinColumn[1]; ejb3JoinColumns[0] = Ejb3JoinColumn.buildJoinColumn( null, null, persistentClass.getIdentifier(), secondaryTables, propertyHolder, mappings ); } else { int nbrOfJoinColumns = pkColumnsAnn != null ? pkColumnsAnn.length : joinColumnsAnn.length; if ( nbrOfJoinColumns == 0 ) { ejb3JoinColumns = new Ejb3JoinColumn[1]; ejb3JoinColumns[0] = Ejb3JoinColumn.buildJoinColumn( null, null, persistentClass.getIdentifier(), secondaryTables, propertyHolder, mappings ); } else { ejb3JoinColumns = new Ejb3JoinColumn[nbrOfJoinColumns]; if ( pkColumnsAnn != null ) { for (int colIndex = 0; colIndex < nbrOfJoinColumns; colIndex++) { ejb3JoinColumns[colIndex] = Ejb3JoinColumn.buildJoinColumn( pkColumnsAnn[colIndex], null, persistentClass.getIdentifier(), secondaryTables, propertyHolder, mappings ); } } else { for (int colIndex = 0; colIndex < nbrOfJoinColumns; colIndex++) { ejb3JoinColumns[colIndex] = Ejb3JoinColumn.buildJoinColumn( null, joinColumnsAnn[colIndex], persistentClass.getIdentifier(), secondaryTables, propertyHolder, mappings ); } } } } for (Ejb3JoinColumn joinColumn : ejb3JoinColumns) { joinColumn.forceNotNull(); } bindJoinToPersistentClass( join, ejb3JoinColumns ); } private void bindJoinToPersistentClass( Join join, Ejb3JoinColumn[] ejb3JoinColumns ) { SimpleValue key = new DependantValue( join.getTable(), persistentClass.getIdentifier() ); join.setKey( key ); setFKNameIfDefined( join ); key.setCascadeDeleteEnabled( false ); TableBinder.bindFk( persistentClass, null, ejb3JoinColumns, key, false, mappings ); join.createPrimaryKey(); join.createForeignKey(); persistentClass.addJoin( join ); } private void setFKNameIfDefined(Join join) { org.hibernate.annotations.Table matchingTable = findMatchingComplimentTableAnnotation( join ); if ( matchingTable != null && !BinderHelper.isDefault( matchingTable.foreignKey().name() ) ) { ( (SimpleValue) join.getKey() ).setForeignKeyName( matchingTable.foreignKey().name() ); } } private org.hibernate.annotations.Table findMatchingComplimentTableAnnotation(Join join) { String tableName = join.getTable().getQuotedName(); org.hibernate.annotations.Table table = annotatedClass.getAnnotation( org.hibernate.annotations.Table.class ); org.hibernate.annotations.Table matchingTable = null; if ( table != null && tableName.equals( table.appliesTo() ) ) { matchingTable = table; } else { Tables tables = annotatedClass.getAnnotation( Tables.class ); if ( tables != null ) { for (org.hibernate.annotations.Table current : tables.value()) { if ( tableName.equals( current.appliesTo() ) ) { matchingTable = current; break; } } } } return matchingTable; } public void firstLevelSecondaryTablesBinding( SecondaryTable secTable, SecondaryTables secTables ) { if ( secTables != null ) { //loop through it for (SecondaryTable tab : secTables.value()) { addJoin( tab, null, null, false ); } } else { if ( secTable != null ) addJoin( secTable, null, null, false ); } } //Used for @*ToMany @JoinTable public Join addJoin(JoinTable joinTable, PropertyHolder holder, boolean noDelayInPkColumnCreation) { return addJoin( null, joinTable, holder, noDelayInPkColumnCreation ); } /** * A non null propertyHolder means than we process the Pk creation without delay */ private Join addJoin( SecondaryTable secondaryTable, JoinTable joinTable, PropertyHolder propertyHolder, boolean noDelayInPkColumnCreation ) { Join join = new Join(); join.setPersistentClass( persistentClass ); String schema; String catalog; String table; String realTable; UniqueConstraint[] uniqueConstraintsAnn; if ( secondaryTable != null ) { schema = secondaryTable.schema(); catalog = secondaryTable.catalog(); table = secondaryTable.name(); realTable = mappings.getNamingStrategy().tableName( table ); //always an explicit table name uniqueConstraintsAnn = secondaryTable.uniqueConstraints(); } else if ( joinTable != null ) { schema = joinTable.schema(); catalog = joinTable.catalog(); table = joinTable.name(); realTable = mappings.getNamingStrategy().tableName( table ); //always an explicit table name uniqueConstraintsAnn = joinTable.uniqueConstraints(); } else { throw new AssertionFailure( "Both JoinTable and SecondaryTable are null" ); } List uniqueConstraints = new ArrayList( uniqueConstraintsAnn == null ? 0 : uniqueConstraintsAnn.length ); if ( uniqueConstraintsAnn != null && uniqueConstraintsAnn.length != 0 ) { for (UniqueConstraint uc : uniqueConstraintsAnn) { uniqueConstraints.add( uc.columnNames() ); } } Table tableMapping = TableBinder.fillTable( schema, catalog, realTable, table, false, uniqueConstraints, null, null, mappings ); //no check constraints available on joins join.setTable( tableMapping ); //somehow keep joins() for later. //Has to do the work later because it needs persistentClass id! Object joinColumns = null; //get the appropriate pk columns if ( secondaryTable != null ) { joinColumns = secondaryTable.pkJoinColumns(); } else if ( joinTable != null ) { joinColumns = joinTable.joinColumns(); } if ( log.isInfoEnabled() ) { log.info( "Adding secondary table to entity " + persistentClass.getEntityName() + " -> " + join.getTable() .getName() ); } org.hibernate.annotations.Table matchingTable = findMatchingComplimentTableAnnotation( join ); if ( matchingTable != null ) { join.setSequentialSelect( FetchMode.JOIN != matchingTable.fetch() ); join.setInverse( matchingTable.inverse() ); join.setOptional( matchingTable.optional() ); if ( !BinderHelper.isDefault( matchingTable.sqlInsert().sql() ) ) { join.setCustomSQLInsert( matchingTable.sqlInsert().sql().trim(), matchingTable.sqlInsert().callable(), ExecuteUpdateResultCheckStyle.parse( matchingTable.sqlInsert().check().toString().toLowerCase() ) ); } if ( !BinderHelper.isDefault( matchingTable.sqlUpdate().sql() ) ) { join.setCustomSQLUpdate( matchingTable.sqlUpdate().sql().trim(), matchingTable.sqlUpdate().callable(), ExecuteUpdateResultCheckStyle.parse( matchingTable.sqlUpdate().check().toString().toLowerCase() ) ); } if ( !BinderHelper.isDefault( matchingTable.sqlDelete().sql() ) ) { join.setCustomSQLDelete( matchingTable.sqlDelete().sql().trim(), matchingTable.sqlDelete().callable(), ExecuteUpdateResultCheckStyle.parse( matchingTable.sqlDelete().check().toString().toLowerCase() ) ); } } else { //default join.setSequentialSelect( false ); join.setInverse( false ); join.setOptional( true ); //perhaps not quite per-spec, but a Good Thing anyway } if ( noDelayInPkColumnCreation ) { createPrimaryColumnsToSecondaryTable( joinColumns, propertyHolder, join ); } else { secondaryTables.put( table, join ); secondaryTableJoins.put( table, joinColumns ); } return join; } public java.util.Map<String, Join> getSecondaryTables() { return secondaryTables; } public void setCache(Cache cacheAnn) { if ( cacheAnn != null ) { cacheRegion = BinderHelper.isDefault( cacheAnn.region() ) ? null : cacheAnn.region(); cacheConcurrentStrategy = getCacheConcurrencyStrategy( cacheAnn.usage() ); if ( "all".equalsIgnoreCase( cacheAnn.include() ) ) { cacheLazyProperty = true; } else if ( "non-lazy".equalsIgnoreCase( cacheAnn.include() ) ) { cacheLazyProperty = false; } else { throw new AnnotationException( "Unknown lazy property annotations: " + cacheAnn.include() ); } } else { cacheConcurrentStrategy = null; cacheRegion = null; cacheLazyProperty = true; } } public static String getCacheConcurrencyStrategy(CacheConcurrencyStrategy strategy) { switch ( strategy ) { case NONE: return null; case READ_ONLY: return CacheFactory.READ_ONLY; case READ_WRITE: return CacheFactory.READ_WRITE; case NONSTRICT_READ_WRITE: return CacheFactory.NONSTRICT_READ_WRITE; case TRANSACTIONAL: return CacheFactory.TRANSACTIONAL; default: throw new AssertionFailure( "CacheConcurrencyStrategy unknown: " + strategy ); } } public void addFilter(String name, String condition) { filters.put( name, condition ); } public void setInheritanceState(InheritanceState inheritanceState) { this.inheritanceState = inheritanceState; } public boolean isIgnoreIdAnnotations() { return ignoreIdAnnotations; } public void setIgnoreIdAnnotations(boolean ignoreIdAnnotations) { this.ignoreIdAnnotations = ignoreIdAnnotations; } public void processComplementaryTableDefinitions(org.hibernate.annotations.Table table) { //comment and index are processed here if ( table == null ) return; String appliedTable = table.appliesTo(); Iterator tables = persistentClass.getTableClosureIterator(); Table hibTable = null; while ( tables.hasNext() ) { Table pcTable = (Table) tables.next(); if ( pcTable.getQuotedName().equals( appliedTable ) ) { //we are in the correct table to find columns hibTable = pcTable; break; } hibTable = null; } if ( hibTable == null ) { //maybe a join/secondary table for ( Join join : secondaryTables.values() ) { if ( join.getTable().getQuotedName().equals( appliedTable ) ) { hibTable = join.getTable(); break; } } } if ( hibTable == null ) { throw new AnnotationException( "@org.hibernate.annotations.Table references an unknown table: " + appliedTable ); } if ( !BinderHelper.isDefault( table.comment() ) ) hibTable.setComment( table.comment() ); TableBinder.addIndexes( hibTable, table.indexes(), mappings ); } public void processComplementaryTableDefinitions(Tables tables) { if ( tables == null ) return; for (org.hibernate.annotations.Table table : tables.value()) { processComplementaryTableDefinitions( table ); } } public void setPropertyAnnotated(boolean propertyAnnotated) { this.isPropertyAnnotated = propertyAnnotated; } public String getPropertyAccessor() { return propertyAccessor; } public void setPropertyAccessor(String propertyAccessor) { this.propertyAccessor = propertyAccessor; } public boolean isPropertyAnnotated(XAnnotatedElement element) { AccessType access = element.getAnnotation( AccessType.class ); if ( access == null ) return isPropertyAnnotated; String propertyAccessor = access.value(); if ( "property".equals( propertyAccessor ) ) { return Boolean.TRUE; } else if ( "field".equals( propertyAccessor ) ) { return Boolean.FALSE; } else { return isPropertyAnnotated; } } public String getPropertyAccessor(XAnnotatedElement element) { AccessType access = element.getAnnotation( AccessType.class ); if ( access == null ) return propertyAccessor; return access.value(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -