📄 annotationbinder.java
字号:
OneToMany ann = annotedElt.getAnnotation(OneToMany.class); bindOneToMany(inferredData.getCollectionType(), ann.targetEntity(), inferredData.getPropertyName(), inferredData.getReturnedClass(), getFetchMode( ann.fetch() ), (Ejb3JoinColumn[]) columns, getCascadeStrategy( ann.cascade() ), inferredData.getDefaultAccess(), propertyHolder, bindBatchSize(annotedElt), mappings, bindWhere(annotedElt) ); } else if ( annotedElt.isAnnotationPresent(ManyToMany.class) ) { ManyToMany ann = annotedElt.getAnnotation(ManyToMany.class); Table assocTable; JoinColumn[] annJoins = null; JoinColumn[] annInverseJoins = null; if ( annotedElt.isAnnotationPresent(AssociationTable.class) ) { AssociationTable atAnn = annotedElt.getAnnotation(AssociationTable.class); if (! atAnn.table().specified() ) { throw new NotYetImplementedException("Implicit Table in AssociationTable"); } assocTable = fillTable(atAnn.table().schema(), atAnn.table().catalog(), mappings.getNamingStrategy().tableName( atAnn.table().name() ), false, new ArrayList(), null, mappings); //set check constaint in the second pass JoinColumn[] joins = atAnn.joinColumns(); if (joins.length == 0) { annJoins = null; } else { annJoins = joins; } JoinColumn[] inverseJoins = atAnn.inverseJoinColumns(); if (inverseJoins.length == 0) { annInverseJoins = null; } else { annInverseJoins = inverseJoins; } } else { throw new NotYetImplementedException("Implicit AssociationTable"); } Ejb3JoinColumn[] joinColumns = buildArrayOfEjb3JoinColumn(annJoins, secondaryTables, propertyHolder, mappings); Ejb3JoinColumn[] inverseJoinColumns = buildArrayOfEjb3JoinColumn(annInverseJoins, secondaryTables, propertyHolder, mappings); bindManyToMany(inferredData.getCollectionType(), ann.targetEntity(), inferredData.getPropertyName(), inferredData.getReturnedClass(), getFetchMode( ann.fetch() ), assocTable, getCascadeStrategy( ann.cascade() ), inferredData.getDefaultAccess(), propertyHolder, ann.isInverse(), joinColumns, inverseJoinColumns, bindBatchSize(annotedElt), bindWhere(annotedElt), mappings ); } else { if (mappings.getClassType( inferredData.getReturnedClass() ).equals(AnnotatedClassType.DEPENDENT) ) { //dependent object DependentObject ann = (DependentObject) inferredData.getReturnedClass().getAnnotation(DependentObject.class); final boolean propertyAccess = ann.access() == AccessType.PROPERTY; Dependent depAnn = (Dependent) annotedElt.getAnnotation(Dependent.class); Map<String, Column[]> columnOverride = new HashMap<String, Column[]>(); if (depAnn != null) { for ( DependentAttribute depAttr : depAnn.value() ) { columnOverride.put( depAttr.name(), depAttr.column() ); } } bindComponent(inferredData, propertyHolder, propertyAccess, secondaryTables, columnOverride, mappings); } else { //provide the basic property mapping log.debug(inferredData.getPropertyName() + " is a property"); boolean lazy = false; if ( annotedElt.isAnnotationPresent(Basic.class) ) { Basic ann = (Basic) annotedElt.getAnnotation(Basic.class); if ( ann.fetch() == FetchType.LAZY ) { lazy = true; log.debug(inferredData.getPropertyName() + " is lazy"); } } String type = ANNOTATION_STRING_DEFAULT; Type annType = (Type) annotedElt.getAnnotation(Type.class); if (annType != null) { type = annType.type(); } bindProperty( inferredData.getPropertyName(), inferredData.getReturnedClassName(), lazy, inferredData.getDefaultAccess(), columns[0], propertyHolder, type); } } } private static String bindWhere(AnnotatedElement annotedElt) { Where ann = (Where) annotedElt.getAnnotation(Where.class); return ann == null ? null : ann.clause(); } private static int bindBatchSize(AnnotatedElement annotedElt) { BatchSize batchAnn = annotedElt.getAnnotation(BatchSize.class); return batchAnn == null ? -1 : batchAnn.size(); } private static Ejb3JoinColumn[] buildArrayOfEjb3JoinColumn(JoinColumn[] annJoins, Map<String, Join> secondaryTables, PropertyHolder propertyHolder, ExtendedMappings mappings) { Ejb3JoinColumn[] joinColumns; if (annJoins == null) { joinColumns = new Ejb3JoinColumn[] { Ejb3JoinColumn.buildJoinColumn( (JoinColumn) null, (String) null, secondaryTables, propertyHolder, mappings) }; } else { joinColumns = new Ejb3JoinColumn[annJoins.length]; JoinColumn annJoin; int length = annJoins.length; for (int index = 0 ; index < length ; index++) { annJoin = annJoins[index]; joinColumns[index] = Ejb3JoinColumn.buildJoinColumn( (JoinColumn) annJoin, (String) null, secondaryTables, propertyHolder, mappings); } } return joinColumns; } private static void bindComponent( AnnotedElementInferredData inferredData, PropertyHolder propertyHolder, boolean propertyAccess, Map<String, Join> secondaryTables, Map<String, Column[]> columnOverride, ExtendedMappings mappings) { Component comp = fillComponent(propertyHolder, inferredData, propertyAccess, true, secondaryTables, columnOverride, mappings); Property prop = makeProperty( inferredData.getPropertyName(), comp, true, true, false, null, inferredData.getDefaultAccess() ); propertyHolder.addProperty(prop); } private static Component fillComponent(PropertyHolder propertyHolder, AnnotedElementInferredData inferredData, boolean propertyAccess, boolean isNullable, Map<String, Join> secondaryTables, Map<String, Column[]> columnOverride, ExtendedMappings mappings) { Component comp = new Component( propertyHolder.getPersistentClass() ); comp.setComponentClassName( inferredData.getReturnedClassName() ); String subpath = StringHelper.qualify( propertyHolder.getPath(), inferredData.getPropertyName() ); log.debug("Binding component with path: " + subpath); PropertyHolder subHolder = PropertyHolderBuilder.buildPropertyHolder(comp, subpath, columnOverride); processElementsOfAClass( subHolder, propertyAccess, isNullable, inferredData.getReturnedClass(), new HashMap<String, IdGenerator>(), new HashMap<String, Properties>(), secondaryTables, mappings ); return comp; } private static void bindId(String generatorType, String generatorName, AnnotedElementInferredData inferredData, Ejb3Column column, RootClass rootClass, Map<String, IdGenerator> localGenerators, HashMap<String, Properties> localGeneratorTables, boolean isComposite, Map<String, Column[]> columnOverride, boolean isPropertyAccess, Map<String, Join> secondaryTables, ExtendedMappings mappings) { /* * Fill simple value and property since and Id is a property */ String persistentClassName = rootClass == null ? null : rootClass.getClassName(); SimpleValue id; if (isComposite) { id = fillComponent(PropertyHolderBuilder.buildPropertyHolder(rootClass), inferredData, isPropertyAccess, false, secondaryTables, columnOverride, mappings); } else { column.forceNotNull(); //this is an id id = makeSimpleValue(inferredData.getPropertyName(), inferredData.getReturnedClassName(), column, persistentClassName, ""); } rootClass.setIdentifier(id); Table table = id.getTable(); table.setIdentifierValue(id); //generator settings id.setIdentifierGeneratorStrategy(generatorType); if (generatorType == "assigned") id.setNullValue("undefined"); Properties params = new Properties(); //always settable params.setProperty( PersistentIdentifierGenerator.TABLE, table.getName() ); params.setProperty( PersistentIdentifierGenerator.PK, ( (org.hibernate.mapping.Column) id.getColumnIterator().next() ).getName() ); if (! isDefault(generatorName) ) { //we have a named generator IdGenerator gen = mappings.getGenerator(generatorName, localGenerators); if (gen == null) { throw new AnnotationException("Unknown Id.generator: " + generatorName); } if ( ! gen.getIdentifierGeneratorStrategy().equals(generatorType) ) { //named generator and id one should be compatible throw new AnnotationException( "Incompatible generator between Id.generate and its named generator: " + generatorType + "!=" + generatorName); } Iterator genParams = gen.getParams().entrySet().iterator(); while ( genParams.hasNext() ) { Map.Entry elt = (Map.Entry) genParams.next(); params.setProperty( (String) elt.getKey(), (String) elt.getValue() ); } if ( MultipleHiLoPerTableGenerator.class.getName().equals(generatorType) ) { //try and find the associated Generator Table fillGeneratorWithGeneratorTableParams(params, localGeneratorTables, generatorName, mappings); } } id.setIdentifierGeneratorProperties(params); Property prop = makeProperty(inferredData.getPropertyName(), id, true, true, false, null, inferredData.getDefaultAccess()); rootClass.setIdentifierProperty(prop); } private static void fillGeneratorWithGeneratorTableParams(Properties params, HashMap<String, Properties> localGeneratorTables, String generatorName, ExtendedMappings mappings) { final String generatorTableName = params.getProperty(GENERATOR_TABLE_NAME_PARAM); Properties props = mappings.getGeneratorTableProperties(generatorTableName, localGeneratorTables); if (props == null) { if ( MultipleHiLoPerTableGenerator.DEFAULT_TABLE.equals(generatorTableName) ) { //default value return; } else { throw new AnnotationException("Unable to find a @GeneratorTable for table name in " + generatorName + ": " + generatorTableName ); } } else { Iterator properties = props.entrySet().iterator(); java.util.Map.Entry<String, String> property; while ( properties.hasNext() ) { property = (java.util.Map.Entry<String, String>) properties.next(); params.setProperty( property.getKey(), property.getValue() ); } } } private static SimpleValue makeSimpleValue(String propertyName, String returnedClassName, Ejb3Column column, String persistentClassName, String explicitType) { log.debug("making simplevalue for " + propertyName); Table table = column.getTable(); SimpleValue simpleValue = new SimpleValue(table); return fillSimpleValue(simpleValue, propertyName, returnedClassName, column, persistentClassName, explicitType); } private static SimpleValue fillSimpleValue(SimpleValue simpleValue, String propertyName, String returnedClassName, Ejb3Column column, String persistentClassName, String explicitType) { String type = isDefault( explicitType ) ? returnedClassName : explicitType; simpleValue.setTypeName(type); if (persistentClassName != null) { simpleValue.setTypeUsingReflection( persistentClassName, propertyName ); } column.linkWithValue(simpleValue); return simpleValue; } private static Property bindProperty(String name, String returnedClassName, boolean lazy, String propertyAccessorName, Ejb3Column column, PropertyHolder propertyHolder, String explicitType) { log.debug("binding property " + name); String containerClassName = propertyHolder == null ? null : propertyHolder.getClassName(); SimpleValue propertyValue = makeSimpleValue(name, returnedClassName, column, containerClassName, explicitType); Property prop = makeProperty(name, propertyValue, column.isInsertable(), column.isUpdatable(), lazy, null, propertyAccessorName); column.addPropertyToMappingContainer(prop); return prop; } private static Property makeProperty(String propertyName, Value value, boolean insertable, boolean updatable, boolean lazy, String cascade, String propertyAccessorName) { log.debug("Building property " + propertyName); Property prop = new Property(); prop.setName(propertyName); prop.setValue(value); prop.setInsertable(insertable); prop.setUpdateable(updatable); prop.setLazy(lazy); prop.setCascade(cascade); prop.setPropertyAccessorName(propertyAccessorName); log.debug("Cascading " + propertyName + " with " + cascade); return prop; } private static void bindManyToOne(String cascadeStrategy, Ejb3JoinColumn[] columns, boolean optional, FetchMode fetchMode, String propertyName, String returnedClassName, String targetEntity, String propertyAccessorName, PropertyHolder propertyHolder, ExtendedMappings mappings) { //All FK columns should be in the same table org.hibernate.mapping.ManyToOne value = new org.hibernate.mapping.ManyToOne( columns[0].getTable() ); if ( isDefault(targetEntity) ) { value.setReferencedEntityName(returnedClassName); } else { value.setReferencedEntityName(targetEntity); } value.setFetchMode(fetchMode); //TODO: manytoOne(optional=false) if (!optional) log.warn( new NotYetImplementedException("ManyToOne.optional() == false") ); value.setTypeName(returnedClassName); value.setTypeUsingReflection(propertyHolder.getClassName() , propertyName); String propertyRef = value.getReferencedPropertyName(); if (propertyRef!=null) mappings.addUniquePropertyReference( value.getReferencedEntityName(), propertyRef ); //value.createForeignKey(); mappings.addSecondPass( new FkSecondPass(value, columns, mappings) ); Property prop = makeProperty(propertyName, value, true, true, false, cascadeStrategy, propertyAccessorName); prop.setCascade(cascadeStrategy); //composite FK columns are in the same table so its OK columns[0].addPropertyToMappingContainer(prop); } public static void bindFkSecondPass(org.hibernate.mapping.ManyToOne manyToOne, Ejb3JoinColumn[] columns, Map persistentClasses) { PersistentClass ref = (PersistentClass) persistentClasses.get( manyToOne.getReferencedEntityName() ); if (ref == null) throw new AnnotationException("Unable to find entity " + manyToOne.getReferencedEntityName() ); bindFk(ref, columns, manyToOne); } private static void bindFk(PersistentClass referencedEntity, Ejb3JoinColumn[] columns, SimpleValue value) { if (referencedEntity.getIdentifier().getColumnSpan() == 1) { if (columns.length != 1) { throw new AnnotationException("@JoinColumns with " + columns.length + " columns" + " refers to " + referencedEntity.getEntityName() + " which has a " + referencedEntity.getIdentifier().getColumnSpan() + " sized PK"); } else { //nothing has to be explicit columns[0].linkWithValue(value); } } else { //Check if we have to do it the implicit way //TODO: clarify the spec on it boolean noReferencedColumn = true; for (Ejb3JoinColumn joinCol : columns) { if ( StringHelper.isNotEmpty( joinCol.getReferencedColumn() ) ) { noReferencedColumn = false; break; } } if (noReferencedColumn) { //implicit case, we hope PK and FK columns are in the same order if (columns.length != referencedEntity.getIdentifier().getColumnSpan() ) { throw new AnnotationException("A Foreign key refering " + referencedEntity.getEntityName() + " has the wrong number of column. should be " + referencedEntity.getIdentifier().getColumnSpan()); } for (Ejb3JoinColumn joinCol : columns) { joinCol.linkWithValue(value); } } else { //explicit referencedColumnName Iterator idColItr = referencedEntity.getIdentifier().getColumnIterator(); org.hibernate.mapping.Column col; if (! idColItr.hasNext()) log.debug("No column in the identifier!"); while ( idColItr.hasNext() ) { boolean match = false; //for each PK column, find the associated FK column. col = (org.hibernate.mapping.Column) idColItr.next(); for (Ejb3JoinColumn joinCol : columns) { if ( joinCol.getReferencedColumn().equals( col.getName() ) ) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -