📄 collectionbinder.java
字号:
else { throw new AnnotationException( "Illegal use of @FilterJoinTable on an association without join table:" + StringHelper.qualify( propertyHolder.getPath(), propertyName ) ); } } } Where where = property.getAnnotation( Where.class ); String whereClause = where == null ? null : where.clause(); if ( StringHelper.isNotEmpty( whereClause ) ) { if ( hasAssociationTable ) { collection.setManyToManyWhere( whereClause ); } else { collection.setWhere( whereClause ); } } WhereJoinTable whereJoinTable = property.getAnnotation( WhereJoinTable.class ); String whereJoinTableClause = whereJoinTable == null ? null : whereJoinTable.clause(); if ( StringHelper.isNotEmpty( whereJoinTableClause ) ) { if ( hasAssociationTable ) { collection.setWhere( whereJoinTableClause ); } else { throw new AnnotationException( "Illegal use of @WhereJoinTable on an association without join table:" + StringHelper.qualify( propertyHolder.getPath(), propertyName ) ); } }// This cannot happen in annotations since the second fetch is hardcoded to join// if ( ( ! collection.getManyToManyFilterMap().isEmpty() || collection.getManyToManyWhere() != null ) &&// collection.getFetchMode() == FetchMode.JOIN &&// collection.getElement().getFetchMode() != FetchMode.JOIN ) {// throw new MappingException(// "association with join table defining filter or where without join fetching " +// "not valid within collection using join fetching [" + collection.getRole() + "]"// );// } } private String getCondition(FilterJoinTable filter) { //set filtering String name = filter.name(); String cond = filter.condition(); return getCondition( cond, name ); } private String getCondition(Filter filter) { //set filtering String name = filter.name(); String cond = filter.condition(); return getCondition( cond, name ); } private String getCondition(String cond, String name) { if ( BinderHelper.isDefault( cond ) ) { cond = mappings.getFilterDefinition( name ).getDefaultFilterCondition(); if ( StringHelper.isEmpty( cond ) ) { throw new AnnotationException( "no filter condition found for filter " + name + " in " + StringHelper.qualify( propertyHolder.getPath(), propertyName ) ); } } return cond; } public void setCache(Cache cacheAnn) { if ( cacheAnn != null ) { cacheRegionName = BinderHelper.isDefault( cacheAnn.region() ) ? null : cacheAnn.region(); cacheConcurrencyStrategy = EntityBinder.getCacheConcurrencyStrategy( cacheAnn.usage() ); } else { cacheConcurrencyStrategy = null; cacheRegionName = null; } } public void setOneToMany(boolean oneToMany) { this.oneToMany = oneToMany; } public void setIndexColumn(IndexColumn indexColumn) { this.indexColumn = indexColumn; } public void setMapKey(MapKey key) { if ( key != null ) { mapKeyPropertyName = key.name(); } } private static String buildOrderByClauseFromHql(String hqlOrderBy, PersistentClass associatedClass, String role) { String orderByString = null; if ( hqlOrderBy != null ) { List<String> properties = new ArrayList<String>(); List<String> ordering = new ArrayList<String>(); StringBuilder orderByBuffer = new StringBuilder(); if ( hqlOrderBy.length() == 0 ) { //order by id Iterator it = associatedClass.getIdentifier().getColumnIterator(); while ( it.hasNext() ) { Selectable col = (Selectable) it.next(); orderByBuffer.append( col.getText() ).append( " asc" ).append( ", " ); } } else { StringTokenizer st = new StringTokenizer( hqlOrderBy, " ,", false ); String currentOrdering = null; //FIXME make this code decent while ( st.hasMoreTokens() ) { String token = st.nextToken(); if ( isNonPropertyToken( token ) ) { if ( currentOrdering != null ) { throw new AnnotationException( "Error while parsing HQL orderBy clause: " + hqlOrderBy + " (" + role + ")" ); } currentOrdering = token; } else { //Add ordering of the previous if ( currentOrdering == null ) { //default ordering ordering.add( "asc" ); } else { ordering.add( currentOrdering ); currentOrdering = null; } properties.add( token ); } } ordering.remove( 0 ); //first one is the algorithm starter // add last one ordering if ( currentOrdering == null ) { //default ordering ordering.add( "asc" ); } else { ordering.add( currentOrdering ); currentOrdering = null; } int index = 0; for (String property : properties) { Property p = BinderHelper.findPropertyByName( associatedClass, property ); if ( p == null ) { throw new AnnotationException( "property from @OrderBy clause not found: " + associatedClass.getEntityName() + "." + property ); } PersistentClass pc = p.getPersistentClass(); String table; if ( pc == null ) { //we are touching a @IdClass property, the pc is not set //this means pc == associatedClass //TODO check whether @ManyToOne @JoinTable in @IdClass used for @OrderBy works: doh! table = ""; } else if ( pc != associatedClass ) { table = pc.getTable().getQuotedName() + "."; } else { table = ""; } Iterator propertyColumns = p.getColumnIterator(); while ( propertyColumns.hasNext() ) { Selectable column = (Selectable) propertyColumns.next(); orderByBuffer.append( table ) .append( column.getText() ) .append( " " ) .append( ordering.get( index ) ) .append( ", " ); } index++; } } orderByString = orderByBuffer.substring( 0, orderByBuffer.length() - 2 ); } return orderByString; } private static String buildOrderByClauseFromHql(String hqlOrderBy, Component component, String role) { String orderByString = null; if ( hqlOrderBy != null ) { List<String> properties = new ArrayList<String>(); List<String> ordering = new ArrayList<String>(); StringBuilder orderByBuffer = new StringBuilder(); if ( hqlOrderBy.length() == 0 ) { //TODO : Check that. Maybe order by key for maps } else { StringTokenizer st = new StringTokenizer( hqlOrderBy, " ,", false ); String currentOrdering = null; //FIXME make this code decent while ( st.hasMoreTokens() ) { String token = st.nextToken(); if ( isNonPropertyToken( token ) ) { if ( currentOrdering != null ) { throw new AnnotationException( "Error while parsing HQL orderBy clause: " + hqlOrderBy + " (" + role + ")" ); } currentOrdering = token; } else { //Add ordering of the previous if ( currentOrdering == null ) { //default ordering ordering.add( "asc" ); } else { ordering.add( currentOrdering ); currentOrdering = null; } properties.add( token ); } } ordering.remove( 0 ); //first one is the algorithm starter // add last one ordering if ( currentOrdering == null ) { //default ordering ordering.add( "asc" ); } else { ordering.add( currentOrdering ); currentOrdering = null; } int index = 0; for (String property : properties) { Property p = component.getProperty( property ); if ( p == null ) { throw new AnnotationException( "property from @OrderBy clause not found: " + role + "." + property ); } Iterator propertyColumns = p.getColumnIterator(); while ( propertyColumns.hasNext() ) { Selectable column = (Selectable) propertyColumns.next(); orderByBuffer.append( column.getText() ) .append( " " ) .append( ordering.get( index ) ) .append( ", " ); } index++; } if ( orderByBuffer.length() >= 2 ) { orderByString = orderByBuffer.substring( 0, orderByBuffer.length() - 2 ); } } } return orderByString; } private static boolean isNonPropertyToken(String token) { if ( " ".equals( token ) ) return true; if ( ",".equals( token ) ) return true; if ( token.equalsIgnoreCase( "desc" ) ) return true; if ( token.equalsIgnoreCase( "asc" ) ) return true; return false; } private static SimpleValue buildCollectionKey( Collection collValue, Ejb3JoinColumn[] joinColumns, boolean cascadeDeleteEnabled, XProperty property, ExtendedMappings mappings ) { //binding key reference using column KeyValue keyVal; //give a chance to override the referenced property name //has to do that here because the referencedProperty creation happens in a FKSecondPass for Many to one yuk! if ( joinColumns.length > 0 && StringHelper.isNotEmpty( joinColumns[0].getMappedBy() ) ) { String entityName = joinColumns[0].getManyToManyOwnerSideEntityName() != null ? "inverse__" + joinColumns[0].getManyToManyOwnerSideEntityName() : joinColumns[0].getPropertyHolder().getEntityName(); String propRef = mappings.getPropertyReferencedAssociation( entityName, joinColumns[0].getMappedBy() ); if ( propRef != null ) { collValue.setReferencedPropertyName( propRef ); mappings.addPropertyReference( collValue.getOwnerEntityName(), propRef ); } } String propRef = collValue.getReferencedPropertyName(); if ( propRef == null ) { keyVal = collValue.getOwner().getIdentifier(); } else { keyVal = (KeyValue) collValue.getOwner() .getRecursiveProperty( propRef ) .getValue(); } DependantValue key = new DependantValue( collValue.getCollectionTable(), keyVal ); key.setTypeName( null ); Ejb3Column.checkPropertyConsistency( joinColumns, collValue.getOwnerEntityName() ); key.setNullable( joinColumns.length == 0 || joinColumns[0].isNullable() ); key.setUpdateable( joinColumns.length == 0 || joinColumns[0].isUpdatable() ); key.setCascadeDeleteEnabled( cascadeDeleteEnabled ); collValue.setKey( key ); ForeignKey fk = property != null ? property.getAnnotation( ForeignKey.class ) : null; String fkName = fk != null ? fk.name() : ""; if ( !BinderHelper.isDefault( fkName ) ) key.setForeignKeyName( fkName ); return key; } protected void bindManyToManySecondPass( Collection collValue, Map persistentClasses, Ejb3JoinColumn[] joinColumns, Ejb3JoinColumn[] inverseJoinColumns, Ejb3Column[] elementColumns, boolean isEmbedded, XClass collType, boolean ignoreNotFound, boolean unique, boolean cascadeDeleteEnabled, TableBinder associationTableBinder, XProperty property, PropertyHolder parentPropertyHolder, String hqlOrderBy, ExtendedMappings mappings ) throws MappingException { PersistentClass collectionEntity = (PersistentClass) persistentClasses.get( collType.getName() ); boolean isCollectionOfEntities = collectionEntity != null; ManyToAny anyAnn = property.getAnnotation( ManyToAny.class ); if ( log.isDebugEnabled() ) { String path = collValue.getOwnerEntityName() + "." + joinColumns[0].getPropertyName(); if ( isCollectionOfEntities && unique ) { log.debug( "Binding a OneToMany: " + path + " through an association table" ); } else if ( isCollectionOfEntities ) { log.debug( "Binding as ManyToMany: " + path ); } else if ( anyAnn != null ) { log.debug( "Binding a ManyToAny: " + path ); } else { log.debug( "Binding a collection of element: " + path ); } } //check for user error if ( !isCollectionOfEntities ) { if ( property.isAnnotationPresent( ManyToMany.class ) || property.isAnnotationPresent( OneToMany.class ) ) { String path = collValue.getOwnerEntityName() + "." + joinColumns[0].getPropertyName(); throw new AnnotationException( "Use of @OneToMany or @ManyToMany targeting an unmapped class: " + path + "[" + collType + "]" ); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -