📄 annotationbinder.java
字号:
//proper join column joinCol.linkWithValue(value); match = true; break; } } if (match == false) { throw new AnnotationException("Column name " + col.getName() + " of " + referencedEntity.getEntityName() + " not found in JoinColumns.referencedColumnName"); } } } } value.createForeignKey(); } private static void bindOneToOne(String cascadeStrategy, Ejb3JoinColumn[] columns, boolean optional, FetchMode fetchMode, String propertyName, String returnedClassName, String targetEntity, String propertyAccessorName, PropertyHolder propertyHolder, ExtendedMappings mappings) { //column.getTable() => persistentClass.getTable() log.debug("Fetching " + propertyName + " with " + fetchMode); Iterator idColumns = propertyHolder.getIdentifier().getColumnIterator(); List<String> idColumnNames = new ArrayList<String>(); org.hibernate.mapping.Column currentColumn; while( idColumns.hasNext() ) { currentColumn = (org.hibernate.mapping.Column) idColumns.next(); idColumnNames.add( currentColumn.getName() ); } boolean trueOneToOne = true; for (Ejb3JoinColumn col : columns) { if ( ! idColumnNames.contains( col.getMappingColumn().getName() ) ) { trueOneToOne = false; break; }// if (StringHelper.isEmpty( col.getReferencedColumn() ) ) {// // this is not an id to id// referenced = false;// } } if (trueOneToOne) { //is a true one-to-one //FIXME referencedColumnName ignored => ordering may fail. org.hibernate.mapping.OneToOne value = new org.hibernate.mapping.OneToOne( propertyHolder.getTable(), propertyHolder.getIdentifier() ); if ( isDefault(targetEntity) ) { value.setReferencedEntityName(returnedClassName); } else { value.setReferencedEntityName(targetEntity); } value.setFetchMode(fetchMode); //TODO: manytoOne(optional=false) if (!optional) log.warn( new NotYetImplementedException("OneToOne.optional()") ); //FIXME Handle bidir OneToOne boolean constrained = false; value.setForeignKeyType( constrained ? ForeignKeyDirection.FOREIGN_KEY_FROM_PARENT : ForeignKeyDirection.FOREIGN_KEY_TO_PARENT ); String propertyRef = value.getReferencedPropertyName(); if (propertyRef!=null) mappings.addUniquePropertyReference( value.getReferencedEntityName(), propertyRef ); //value.createForeignKey(); Property prop = makeProperty(propertyName, value, true, true, false, cascadeStrategy, propertyAccessorName); prop.setCascade(cascadeStrategy); //no column associated since its a one to one propertyHolder.addProperty(prop); } else { //has a FK on the table //TODO add the unique constrains bindManyToOne(cascadeStrategy, columns, optional, fetchMode, propertyName, returnedClassName, targetEntity, propertyAccessorName, propertyHolder, mappings); } } private static void bindOneToMany(Class collectionType, String targetEntity, String propertyName, Class returnedClass, FetchMode fetchMode, Ejb3JoinColumn[] columns, String cascadeStrategy, String propertyAccessorName, PropertyHolder propertyHolder, int batchSize, ExtendedMappings mappings, String where) { for (Ejb3JoinColumn column : columns) { if ( column.isSecondary() ) { throw new NotYetImplementedException("Collections having FK in secondary table"); } } Collection collection = fillCollection( propertyName, returnedClass, fetchMode, propertyHolder, batchSize, where); collection.setInverse(true); //If I understand well EJB3 6.1.6 org.hibernate.mapping.OneToMany oneToMany = new org.hibernate.mapping.OneToMany( collection.getOwner() ); collection.setElement(oneToMany); oneToMany.setReferencedEntityName( getCollectionType(collectionType, targetEntity) ); mappings.addSecondPass( new CollectionSecondPass(mappings, collection, columns) ); mappings.addCollection(collection); Property prop = makeProperty(propertyName, collection, true, true, false, cascadeStrategy, propertyAccessorName); //we don't care about the join stuffs because the column is on the other side. propertyHolder.addProperty(prop); } private static void bindManyToMany(Class collectionType, String targetEntity, String propertyName, Class returnedClass, FetchMode fetchMode, Table table, String cascadeStrategy, String propertyAccessorName, PropertyHolder propertyHolder, boolean isInverse, Ejb3JoinColumn[] joinColumns, Ejb3JoinColumn[] inverseJoinColumns, int batchSize, String where, ExtendedMappings mappings) { log.debug("Binding ManyToMany: " + propertyName); //log.debug( "Joincolumn: " + joinColumn.getMappingColumn().getName() ); //log.debug( "InverseJoincolumn: " + inverseJoinColumn.getMappingColumn().getName() ); Collection collection = fillCollection( propertyName, returnedClass, fetchMode, propertyHolder, batchSize, where); collection.setInverse(isInverse); collection.setCollectionTable(table); String collType = getCollectionType(collectionType, targetEntity); //NEXT: think of second mapp and JoinColumns mappings.addSecondPass( new CollectionSecondPass( mappings, collection, joinColumns, inverseJoinColumns, collType, fetchMode ) ); mappings.addCollection(collection); Property prop = makeProperty(propertyName, collection, true, true, false, cascadeStrategy, propertyAccessorName); //we don't care about the join stuffs because the column is on the association table. propertyHolder.addProperty(prop); } private static Collection fillCollection(String propertyName, Class returnedClass, FetchMode fetchMode, PropertyHolder propertyHolder, int batchSize, String where) { Collection collection; if ( returnedClass.equals(Set.class) ) { collection = new org.hibernate.mapping.Set( propertyHolder.getPersistentClass() ); } else if ( returnedClass.equals(java.util.Collection.class) ) { collection = new Bag( propertyHolder.getPersistentClass() ); } else { throw new AnnotationException(returnedClass.getName() + " collection not yet supported"); } log.debug("Collection role: " + StringHelper.qualify(propertyHolder.getPath(), propertyName) ); collection.setRole( StringHelper.qualify(propertyHolder.getPath(), propertyName) ); //set laziness collection.setFetchMode(fetchMode); collection.setLazy(fetchMode == FetchMode.SELECT); collection.setBatchSize(batchSize); if ( StringHelper.isNotEmpty(where) ) collection.setWhere(where); return collection; } private static String getCollectionType(Class collectionType, String targetEntity) { if ( isDefault(targetEntity) ) { if (collectionType != null) { return collectionType.getName(); } else { throw new AnnotationException("Collection has neither generic type or OneToMany.targetEntity() defined"); } } else { return targetEntity; } } public static void bindCollectionSecondPass(Collection collValue, java.util.Map persistentClasses, Ejb3JoinColumn[] columns, ExtendedMappings mappings) throws MappingException { if ( collValue.isOneToMany() ) { org.hibernate.mapping.OneToMany oneToMany = (org.hibernate.mapping.OneToMany) collValue.getElement(); String assocClass = oneToMany.getReferencedEntityName(); PersistentClass persistentClass = (PersistentClass) persistentClasses.get(assocClass); if (mappings == null) { throw new AssertionFailure("CollectionSecondPass for oneToMany should not be called with null mappings"); } Map<String, Join> joins = mappings.getJoins(assocClass); if (persistentClass==null) throw new MappingException( "Association references unmapped class: " + assocClass ); oneToMany.setAssociatedClass(persistentClass); //FIXME: find the appropriate table between second and primary for (Ejb3JoinColumn column : columns) { column.setPersistentClass(persistentClass); column.setJoins(joins); collValue.setCollectionTable( column.getTable() ); } log.info("Mapping collection: " + collValue.getRole() + " -> " + collValue.getCollectionTable().getName() ); } //binding key reference using column KeyValue keyVal; String propRef = collValue.getReferencedPropertyName(); if (propRef==null) { keyVal = collValue.getOwner().getIdentifier(); } else { keyVal = (KeyValue) collValue.getOwner() .getProperty(propRef) .getValue(); } SimpleValue key = new DependantValue( collValue.getCollectionTable(), keyVal ); //fillSimpleValue(key, null, null, column, null); key.setTypeName(null); bindFk(collValue.getOwner(), columns, key); collValue.setKey(key); } public static void bindManyToManySecondPass( Collection collValue, java.util.Map persistentClasses, Ejb3JoinColumn[] joinColumns, Ejb3JoinColumn[] inverseJoinColumns, String collType, FetchMode fetchMode) throws MappingException { bindCollectionSecondPass(collValue, persistentClasses, joinColumns, null); org.hibernate.mapping.ManyToOne element = new org.hibernate.mapping.ManyToOne( collValue.getCollectionTable() ); collValue.setElement(element); element.setReferencedEntityName(collType); element.setFetchMode(fetchMode); //FIXME: do optional = false// value.setTypeName(returnedClassName);// value.setTypeUsingReflection(persistentClass.getClassName() , propertyName); PersistentClass persistentClass = (PersistentClass) persistentClasses.get(collType); if (persistentClass==null) throw new MappingException( "Association references unmapped class: " + collType ); bindFk(persistentClass, inverseJoinColumns, element); //inverseJoinColumn.getMappingColumn().setValue(element); //element.getTable().addColumn( inverseJoinColumn.getMappingColumn() ); //element.addColumn( inverseJoinColumn.getMappingColumn() );// String referencedColumn = ( (Ejb3JoinColumn) inverseJoinColumn ).getReferencedColumn();// if ( referencedColumn != null && !referencedColumn.equals("") ) throw new NotYetImplementedException("JoinColumn.referencedColumnName");// value.createForeignKey(); //bindManyToOne(subnode, element, Collection.DEFAULT_ELEMENT_COLUMN_NAME, false, mappings); //FIXME: Watch for fetch of the element in the collection } private static String generatorType(GeneratorType generatorEnum) { switch ( generatorEnum ) { case NONE : return "assigned"; case IDENTITY:return "identity"; case AUTO:return "native"; case TABLE:return MultipleHiLoPerTableGenerator.class.getName(); case SEQUENCE:return "sequence"; } throw new AssertionFailure("Unknown GeneratorType: " + generatorEnum); } private static String getClassTableName(PersistentClass model, String tableName, ExtendedMappings mappings) { if ( StringHelper.isEmpty(tableName) ) { return mappings.getNamingStrategy().classToTableName( model.getEntityName() ); } else { return mappings.getNamingStrategy().tableName(tableName); } } private static String getCascadeStrategy(CascadeType[] cascades) { if (cascades.length == 0) { return "none"; } boolean all = false; boolean create = false; boolean merge = false; boolean remove = false; for(CascadeType cascade: cascades) { switch (cascade) { case ALL: all = true; break; case CREATE: create = true; break; case MERGE: merge = true; break; case REMOVE: remove = true; break; } } if (!all) all = create && merge && remove; if (all) return "all"; if (create && merge) return "persist,merge,save-update"; //HBX-47 if (create && remove) return "persist,delete,save-update"; //HBX-47 if (merge && remove) return "merge,delete,save-update"; //HBX-47 if (remove) return "delete"; if (create) return "persist,save-update"; //HBX-47 if (merge) return "merge,save-update"; //HBX-47 throw new AssertionFailure("Error in cascade strategies mapper"); } private static FetchMode getFetchMode(FetchType fetch) { if (fetch == FetchType.EAGER) { return FetchMode.JOIN; } else { return FetchMode.SELECT; } } private static HashMap<String,IdGenerator> buildLocalGenerators(AnnotatedElement annElt) { HashMap<String,IdGenerator> generators = new HashMap<String, IdGenerator>(); TableGenerator tabGen = (TableGenerator) annElt.getAnnotation(TableGenerator.class); SequenceGenerator seqGen = (SequenceGenerator) annElt.getAnnotation(SequenceGenerator.class); if (tabGen != null) { IdGenerator idGen = buildIdGenerator(tabGen); generators.put(idGen.getName(), idGen); } if (seqGen != null) { IdGenerator idGen = buildIdGenerator(seqGen); generators.put(idGen.getName(), idGen); } return generators; } private static void prepareForSecondaryTable(SecondaryTable tabAnn, PersistentClass persistentClass, Map<String, Join> secondaryTables, Map<String, Object> secondaryTableJoins, ExtendedMappings mappings) { Join join = new Join(); join.setPersistentClass(persistentClass); List uniqueConstraints = new ArrayList(); if (tabAnn.uniqueConstraints().length != 0) { for( UniqueConstraint uc : tabAnn.uniqueConstraints() ) { if (! uc.primary() ) { uniqueConstraints.add( uc.columnNames() ); } else { log.warn("@UniqueConstraint(primaryKey=true) not yet supported"); } } } org.hibernate.mapping.Table tableMapping = fillTable( tabAnn.schema(), tabAnn.catalog(), tabAnn.name(), false, uniqueConstraints, 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! secondaryTableJoins.put(tabAnn.name(), tabAnn.join() ); log.info("Mapping class join: " + persistentClass.getEntityName() + " -> " + join.getTable().getName() ); secondaryTables.put(tabAnn.name(), join); } public static boolean isDefault(String annotationString) { return ANNOTATION_STRING_DEFAULT.equals(annotationString); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -