📄 collectionbinder.java
字号:
package org.hibernate.cfg.annotations;import java.util.ArrayList;import java.util.Comparator;import java.util.HashMap;import java.util.Iterator;import java.util.List;import java.util.Map;import java.util.StringTokenizer;import javax.persistence.AttributeOverride;import javax.persistence.AttributeOverrides;import javax.persistence.Embeddable;import javax.persistence.FetchType;import javax.persistence.JoinTable;import javax.persistence.ManyToMany;import javax.persistence.MapKey;import javax.persistence.OneToMany;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.hibernate.AnnotationException;import org.hibernate.AssertionFailure;import org.hibernate.FetchMode;import org.hibernate.MappingException;import org.hibernate.annotations.BatchSize;import org.hibernate.annotations.Cache;import org.hibernate.annotations.CollectionId;import org.hibernate.annotations.CollectionOfElements;import org.hibernate.annotations.Fetch;import org.hibernate.annotations.Filter;import org.hibernate.annotations.FilterJoinTable;import org.hibernate.annotations.FilterJoinTables;import org.hibernate.annotations.Filters;import org.hibernate.annotations.ForeignKey;import org.hibernate.annotations.Immutable;import org.hibernate.annotations.LazyCollection;import org.hibernate.annotations.LazyCollectionOption;import org.hibernate.annotations.Loader;import org.hibernate.annotations.ManyToAny;import org.hibernate.annotations.OptimisticLock;import org.hibernate.annotations.OrderBy;import org.hibernate.annotations.Persister;import org.hibernate.annotations.SQLDelete;import org.hibernate.annotations.SQLDeleteAll;import org.hibernate.annotations.SQLInsert;import org.hibernate.annotations.SQLUpdate;import org.hibernate.annotations.Sort;import org.hibernate.annotations.SortType;import org.hibernate.annotations.Where;import org.hibernate.annotations.WhereJoinTable;import org.hibernate.annotations.common.reflection.XClass;import org.hibernate.annotations.common.reflection.XProperty;import org.hibernate.cfg.AnnotatedClassType;import org.hibernate.cfg.AnnotationBinder;import org.hibernate.cfg.BinderHelper;import org.hibernate.cfg.CollectionSecondPass;import org.hibernate.cfg.Ejb3Column;import org.hibernate.cfg.Ejb3JoinColumn;import org.hibernate.cfg.ExtendedMappings;import org.hibernate.cfg.IndexColumn;import org.hibernate.cfg.PropertyData;import org.hibernate.cfg.PropertyHolder;import org.hibernate.cfg.PropertyHolderBuilder;import org.hibernate.cfg.PropertyInferredData;import org.hibernate.cfg.PropertyPreloadedData;import org.hibernate.cfg.SecondPass;import org.hibernate.engine.ExecuteUpdateResultCheckStyle;import org.hibernate.mapping.Any;import org.hibernate.mapping.Backref;import org.hibernate.mapping.Collection;import org.hibernate.mapping.Column;import org.hibernate.mapping.Component;import org.hibernate.mapping.DependantValue;import org.hibernate.mapping.IdGenerator;import org.hibernate.mapping.Join;import org.hibernate.mapping.KeyValue;import org.hibernate.mapping.ManyToOne;import org.hibernate.mapping.PersistentClass;import org.hibernate.mapping.Property;import org.hibernate.mapping.Selectable;import org.hibernate.mapping.SimpleValue;import org.hibernate.mapping.Table;import org.hibernate.util.StringHelper;/** * Collection binder * * @author inger * @author Emmanuel Bernard */public abstract class CollectionBinder { private static final Log log = LogFactory.getLog( CollectionBinder.class ); protected Collection collection; protected String propertyName; PropertyHolder propertyHolder; int batchSize; private String mappedBy; private XClass collectionType; private XClass targetEntity; private ExtendedMappings mappings; private Ejb3JoinColumn[] inverseJoinColumns; private String cascadeStrategy; String cacheConcurrencyStrategy; String cacheRegionName; private boolean oneToMany; protected IndexColumn indexColumn; private String orderBy; protected String hqlOrderBy; private boolean isSorted; private Class comparator; private boolean hasToBeSorted; protected boolean cascadeDeleteEnabled; protected String mapKeyPropertyName; private boolean insertable = true; private boolean updatable = true; private Ejb3JoinColumn[] fkJoinColumns; private boolean isExplicitAssociationTable; private Ejb3Column[] elementColumns; private boolean isEmbedded; private XProperty property; private boolean ignoreNotFound; private TableBinder tableBinder; private Ejb3Column[] mapKeyColumns; private Ejb3JoinColumn[] mapKeyManyToManyColumns; protected HashMap<String, IdGenerator> localGenerators; public void setUpdatable(boolean updatable) { this.updatable = updatable; } public void setInsertable(boolean insertable) { this.insertable = insertable; } public void setCascadeStrategy(String cascadeStrategy) { this.cascadeStrategy = cascadeStrategy; } public void setPropertyAccessorName(String propertyAccessorName) { this.propertyAccessorName = propertyAccessorName; } private String propertyAccessorName; public void setInverseJoinColumns(Ejb3JoinColumn[] inverseJoinColumns) { this.inverseJoinColumns = inverseJoinColumns; } public void setJoinColumns(Ejb3JoinColumn[] joinColumns) { this.joinColumns = joinColumns; } private Ejb3JoinColumn[] joinColumns; public void setPropertyHolder(PropertyHolder propertyHolder) { this.propertyHolder = propertyHolder; } public void setBatchSize(BatchSize batchSize) { this.batchSize = batchSize == null ? -1 : batchSize.size(); } public void setEjb3OrderBy(javax.persistence.OrderBy orderByAnn) { if ( orderByAnn != null ) { hqlOrderBy = orderByAnn.value(); } } public void setSqlOrderBy(OrderBy orderByAnn) { if ( orderByAnn != null ) { if ( !BinderHelper.isDefault( orderByAnn.clause() ) ) orderBy = orderByAnn.clause(); } } public void setSort(Sort sortAnn) { if ( sortAnn != null ) { isSorted = !SortType.UNSORTED.equals( sortAnn.type() ); if ( isSorted && SortType.COMPARATOR.equals( sortAnn.type() ) ) { comparator = sortAnn.comparator(); } } } /** * collection binder factory */ public static CollectionBinder getCollectionBinder( String entityName, XProperty property, boolean isIndexed ) { if ( property.isArray() ) { if ( property.getElementClass().isPrimitive() ) { return new PrimitiveArrayBinder(); } else { return new ArrayBinder(); } } else if ( property.isCollection() ) { //TODO consider using an XClass Class returnedClass = property.getCollectionClass(); if ( java.util.Set.class.equals( returnedClass ) ) { if ( property.isAnnotationPresent( CollectionId.class ) ) { throw new AnnotationException( "Set do not support @CollectionId: " + StringHelper.qualify( entityName, property.getName() ) ); } return new SetBinder(); } else if ( java.util.SortedSet.class.equals( returnedClass ) ) { if ( property.isAnnotationPresent( CollectionId.class ) ) { throw new AnnotationException( "Set do not support @CollectionId: " + StringHelper.qualify( entityName, property.getName() ) ); } return new SetBinder( true ); } else if ( java.util.Map.class.equals( returnedClass ) ) { if ( property.isAnnotationPresent( CollectionId.class ) ) { throw new AnnotationException( "Map do not support @CollectionId: " + StringHelper.qualify( entityName, property.getName() ) ); } return new MapBinder(); } else if ( java.util.SortedMap.class.equals( returnedClass ) ) { if ( property.isAnnotationPresent( CollectionId.class ) ) { throw new AnnotationException( "Map do not support @CollectionId: " + StringHelper.qualify( entityName, property.getName() ) ); } return new MapBinder( true ); } else if ( java.util.Collection.class.equals( returnedClass ) ) { if ( property.isAnnotationPresent( CollectionId.class ) ) { return new IdBagBinder(); } else { return new BagBinder(); } } else if ( java.util.List.class.equals( returnedClass ) ) { if ( isIndexed ) { if ( property.isAnnotationPresent( CollectionId.class ) ) { throw new AnnotationException( "List do not support @CollectionId and @IndexColumn at the same time: " + StringHelper.qualify( entityName, property.getName() ) ); } return new ListBinder(); } else if ( property.isAnnotationPresent( CollectionId.class ) ) { return new IdBagBinder(); } else { return new BagBinder(); } } else { throw new AnnotationException( returnedClass.getName() + " collection not yet supported: " + StringHelper.qualify( entityName, property.getName() ) ); } } else { throw new AnnotationException( "Illegal attempt to map a non collection as a @OneToMany, @ManyToMany or @CollectionOfElements: " + StringHelper.qualify( entityName, property.getName() ) ); } } protected CollectionBinder() { } protected CollectionBinder(boolean sorted) { this.hasToBeSorted = sorted; } public void setMappedBy(String mappedBy) { this.mappedBy = mappedBy; } public void setTableBinder(TableBinder tableBinder) { this.tableBinder = tableBinder; } public void setCollectionType(XClass collectionType) { this.collectionType = collectionType; } public void setTargetEntity(XClass targetEntity) { this.targetEntity = targetEntity; } public void setMappings(ExtendedMappings mappings) { this.mappings = mappings; } protected abstract Collection createCollection(PersistentClass persistentClass); public Collection getCollection() { return collection; } public void setPropertyName(String propertyName) { this.propertyName = propertyName; } public void bind() { this.collection = createCollection( propertyHolder.getPersistentClass() ); log.debug( "Collection role: " + StringHelper.qualify( propertyHolder.getPath(), propertyName ) ); collection.setRole( StringHelper.qualify( propertyHolder.getPath(), propertyName ) ); collection.setNodeName( propertyName ); if ( property.isAnnotationPresent( org.hibernate.annotations.MapKey.class ) && mapKeyPropertyName != null ) { throw new AnnotationException( "Cannot mix @javax.persistence.MapKey and @org.hibernate.annotations.MapKey " + "on the same collection: " + StringHelper.qualify( propertyHolder.getPath(), propertyName ) ); } //set laziness defineFetchingStrategy(); collection.setBatchSize( batchSize ); if ( orderBy != null && hqlOrderBy != null ) { throw new AnnotationException( "Cannot use sql order by clause in conjunction of EJB3 order by clause: " + safeCollectionRole() ); } collection.setMutable( !property.isAnnotationPresent( Immutable.class ) ); OptimisticLock lockAnn = property.getAnnotation( OptimisticLock.class ); if ( lockAnn != null ) collection.setOptimisticLocked( !lockAnn.excluded() ); Persister persisterAnn = property.getAnnotation( Persister.class ); if ( persisterAnn != null ) collection.setCollectionPersisterClass( persisterAnn.impl() ); // set ordering if ( orderBy != null ) collection.setOrderBy( orderBy ); if ( isSorted ) { collection.setSorted( true ); if ( comparator != null ) { try { collection.setComparator( (Comparator) comparator.newInstance() ); } catch (ClassCastException e) { throw new AnnotationException( "Comparator not implementing java.util.Comparator class: " + comparator.getName() + "(" + safeCollectionRole() + ")" ); } catch (Exception e) { throw new AnnotationException( "Could not instantiate comparator class: " + comparator.getName() + "(" + safeCollectionRole() + ")" ); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -