📄 annotationbinder.java
字号:
//try to find class level generators HashMap<String, IdGenerator> classGenerators = buildLocalGenerators(annotatedClass); HashMap<String, Properties> classGeneratorTables = buildLocalGeneratorTable(annotatedClass); // check properties Class currentClassInHierarchy = annotatedClass; do { processElementsOfAClass(propertyHolder, entityBinder.isPropertyAccess(), true, currentClassInHierarchy, classGenerators, classGeneratorTables, secondaryTables, mappings); currentClassInHierarchy = currentClassInHierarchy.getSuperclass(); } while ( isRootClass && ! Object.class.equals( currentClassInHierarchy ) ); if (isRootClass) { ( (RootClass) persistentClass).createPrimaryKey(); } else { superEntity.addSubclass( (Subclass) persistentClass ); }// String entityName = isDefault( entityAnn.name() ) ? StringHelper.unqualify( annotatedClass.getName() ) : entityAnn.name();// log.debug("Import with entity name=" + entityName);// try {// mappings.addImport( persistentClass.getEntityName(), entityName );// }// catch (MappingException me) {// throw new AnnotationException("Use of the same entity name twice: " + entityName);// } mappings.addClass(persistentClass); mapSecondaryTables(secondaryTables, secondaryTableJoins, persistentClass, propertyHolder, mappings); } private static HashMap<String, Properties> buildLocalGeneratorTable(AnnotatedElement annotatedClass) { HashMap<String, Properties> result = new HashMap<String, Properties>(); GeneratorTable ann = (GeneratorTable) annotatedClass.getAnnotation(GeneratorTable.class); if (ann != null) { result.put( ann.name(), buildPropertiesFromGeneratorTable(ann) ); } return result; } private static void mapSecondaryTables(Map<String, Join> secondaryTables, Map<String, Object> secondaryTableJoins, PersistentClass persistentClass, PropertyHolder propertyHolder, ExtendedMappings mappings) { Iterator joins = secondaryTables.values().iterator(); Iterator joinColumns = secondaryTableJoins.values().iterator(); while ( joins.hasNext() ) { JoinColumn[] columns = (JoinColumn[]) joinColumns.next(); Join join = (Join) joins.next(); SimpleValue key = new DependantValue( join.getTable(), persistentClass.getIdentifier() ); join.setKey(key); join.setSequentialSelect(false); join.setInverse(false); join.setOptional(false); key.setCascadeDeleteEnabled(false); Ejb3JoinColumn[] ejb3JoinColumns; int nbrOfJoinColumns = columns.length; if (nbrOfJoinColumns == 0) { ejb3JoinColumns = new Ejb3JoinColumn[1]; ejb3JoinColumns[0] = Ejb3JoinColumn.buildJoinColumn( (JoinColumn) null, persistentClass.getIdentifier(), secondaryTables, propertyHolder, mappings ); } else { ejb3JoinColumns = new Ejb3JoinColumn[nbrOfJoinColumns]; for (int colIndex = 0 ; colIndex < nbrOfJoinColumns ; colIndex++) { ejb3JoinColumns[colIndex] = Ejb3JoinColumn.buildJoinColumn( columns[colIndex], persistentClass.getIdentifier(), secondaryTables, propertyHolder, mappings ); } } for (Ejb3JoinColumn joinColumn : ejb3JoinColumns) { joinColumn.forceNotNull(); } bindFk(persistentClass, ejb3JoinColumns, key); join.createPrimaryKey(); // join.createForeignKey(); // persistentClass.addJoin(join); // } mappings.addJoins(persistentClass, secondaryTables); } private static void bindDiscriminatorToPersistentClass(RootClass rootClass, Ejb3DiscriminatorColumn discriminatorColumn, Map<String, Join> secondaryTables, PropertyHolder propertyHolder, ExtendedMappings mappings) { if (rootClass.getDiscriminator() == null) { if (discriminatorColumn == null) { Class clazz; try { clazz = ReflectHelper.classForName( rootClass.getClassName() ); } catch (ClassNotFoundException e) { throw new AnnotationException( "Root Class not found in classpath: " + rootClass.getClassName() ); } Inheritance inhAnn = (Inheritance) clazz.getAnnotation(Inheritance.class); DiscriminatorType type; if (inhAnn == null) { type = null; } else { type = inhAnn.discriminatorType(); } discriminatorColumn = Ejb3DiscriminatorColumn.buildDiscriminatorColumn(type, null, mappings); } discriminatorColumn.setJoins(secondaryTables); discriminatorColumn.setPropertyHolder(propertyHolder); SimpleValue discrim = new SimpleValue( rootClass.getTable() ); rootClass.setDiscriminator(discrim); org.hibernate.mapping.Column col = discriminatorColumn.getMappingColumn(); col.setValue(discrim); discrim.getTable().addColumn(col); //not sure this is the right place discrim.addColumn(col); discrim.setTypeName(discriminatorColumn.getDiscriminatorTypeName()); rootClass.setPolymorphic(true); log.debug( "Setting discriminator for entity " + rootClass.getEntityName() ); } } /** * Process the elements of a particular class */ private static void processElementsOfAClass(PropertyHolder propertyHolder, boolean propertyAccess, boolean isNullable, final Class annotatedClass, HashMap<String, IdGenerator> classGenerators, HashMap<String, Properties> classGeneratorTables, Map<String, Join> secondaryTables, ExtendedMappings mappings) { ArrayList<AnnotatedElement> annElts = new ArrayList<AnnotatedElement>(); ArrayList<AnnotedElementInferredData> inferredDatas = new ArrayList<AnnotedElementInferredData>(); if (propertyAccess) { log.debug("Processing " + propertyHolder.getEntityName() + " per property access"); Method[] methods = annotatedClass.getDeclaredMethods(); AnnotatedElement currentElt; int index = 0; while( index < methods.length) { currentElt = (AnnotatedElement) methods[index]; addAnnotatedElement(currentElt, annElts, inferredDatas); index++; } } else { log.debug("Processing " + propertyHolder.getEntityName() + " per field access"); Field[] fields = annotatedClass.getDeclaredFields(); AnnotatedElement currentElt; int index = 0; while( index < fields.length) { currentElt = (AnnotatedElement) fields[index]; addAnnotatedElement(currentElt, annElts, inferredDatas); index++; } } //process them int size = annElts.size(); for (int index = 0 ; index < size ; index++) { processElementAnnotations(propertyHolder, isNullable, annElts.get(index), mappings, inferredDatas.get(index), classGenerators, classGeneratorTables, secondaryTables); } } private static void addAnnotatedElement(AnnotatedElement elt, ArrayList<AnnotatedElement> annElts, ArrayList<AnnotedElementInferredData> inferredDatas) { AnnotedElementInferredData inferredData = new AnnotedElementInferredData(elt); if ( inferredData.isAnnotable() ) { /* * put element annotated by @Id in front * since it has to be parsed before any assoctation by Hibernate */ if ( elt.isAnnotationPresent(Id.class) ) { annElts.add(0, elt); inferredDatas.add(0, inferredData); } else { annElts.add(elt); inferredDatas.add(inferredData); } } } public static void bindTable(PersistentClass persistentClass, String schema, String catalog, String tableName, List uniqueConstraints, String constraints, ExtendedMappings mappings) { Table table = fillTable( schema, catalog, getClassTableName(persistentClass, tableName, mappings), persistentClass.isAbstract(), uniqueConstraints, constraints, mappings ); if (persistentClass instanceof TableOwner) { ( (TableOwner) persistentClass).setTable(table); } else { throw new AssertionFailure("call of AnnBinder.bindTable for a SubClass"); } } public static Table fillTable(String schema, String catalog, String realTableName, boolean isAbstract, List uniqueConstraints, String constraints, ExtendedMappings mappings) { schema = isDefault(schema) ? schema = mappings.getSchemaName() : schema; catalog = isDefault(catalog) ? catalog = mappings.getCatalogName() : catalog; Table table = mappings.addTable( schema, catalog, realTableName, null, //subselect isAbstract ); if (uniqueConstraints.size() > 0) mappings.addUniqueConstraints(table, uniqueConstraints); if (constraints != null) table.addCheckConstraint(constraints); return table; } /** * Process annotation of a particular element */ private static void processElementAnnotations(PropertyHolder propertyHolder, boolean isNullable, AnnotatedElement annotedElt, ExtendedMappings mappings, AnnotedElementInferredData inferredData, HashMap<String, IdGenerator> classGenerators, HashMap<String, Properties> classGeneratorTables, Map<String, Join> secondaryTables) throws MappingException { Ejb3Column[] columns = null; if ( annotedElt.isAnnotationPresent(Transient.class) ) { return; } log.debug( "Processing " + inferredData.getPropertyName() ); if ( annotedElt.isAnnotationPresent(Column.class) ) { Column ann = (Column) annotedElt.getAnnotation(Column.class); columns = Ejb3Column.buildColumnFromAnnotation(ann, propertyHolder, inferredData, secondaryTables, mappings); } else if ( annotedElt.isAnnotationPresent(JoinColumn.class) ) { JoinColumn ann = (JoinColumn) annotedElt.getAnnotation(JoinColumn.class); columns = new Ejb3JoinColumn[1]; columns[0] = Ejb3JoinColumn.buildJoinColumn(ann, inferredData.getPropertyName(), secondaryTables, propertyHolder, mappings); } else if ( annotedElt.isAnnotationPresent(JoinColumns.class) ) { JoinColumns ann = annotedElt.getAnnotation(JoinColumns.class); JoinColumn[] annColumns = ann.value(); int length = annColumns.length; if (length == 0) { //FIXME not sure this is appropriate, I need to reread the spec later throw new AnnotationException("Cannot bind an empty @JoinColumns"); } columns = new Ejb3JoinColumn[length]; for (int index = 0 ; index < length ; index++) { columns[index] = Ejb3JoinColumn.buildJoinColumn(annColumns[index], inferredData.getPropertyName(), secondaryTables, propertyHolder, mappings); } } else { //Collection.DEFAULT_KEY_COLUMN_NAME for many-to-many if ( annotedElt.isAnnotationPresent(ManyToOne.class) ) { columns = new Ejb3JoinColumn[1]; columns[0] = Ejb3JoinColumn.buildJoinColumn( (JoinColumn) null, inferredData.getPropertyName(), secondaryTables, propertyHolder, mappings); } else if ( annotedElt.isAnnotationPresent(OneToMany.class) ) { //FIXME: guess the correct name by getting it from ManyToOne side columns = new Ejb3JoinColumn[1]; columns[0] = Ejb3JoinColumn.buildJoinColumn( (JoinColumn) null, inferredData.getPropertyName(), secondaryTables, propertyHolder, mappings); } else if ( annotedElt.isAnnotationPresent(OneToOne.class) ) { columns = null; } else { columns = Ejb3Column.buildColumnFromAnnotation(null, propertyHolder, inferredData, secondaryTables, mappings); } } if (isNullable == false) { //force columns to not null for (Ejb3Column col : columns) { col.forceNotNull(); } } if ( annotedElt.isAnnotationPresent(Id.class) ) { log.debug(inferredData.getPropertyName() + " is an id"); Id ann = (Id) annotedElt.getAnnotation(Id.class); //clone classGenerator and override with local values HashMap<String, IdGenerator> localGenerators = (HashMap<String, IdGenerator>) classGenerators.clone(); HashMap<String, Properties> localGeneratorTables = (HashMap<String, Properties>) classGeneratorTables.clone(); localGenerators.putAll( buildLocalGenerators(annotedElt) ); localGeneratorTables.putAll( buildLocalGeneratorTable(annotedElt) ); DependentObject depAnn = (DependentObject) inferredData.getReturnedClass().getAnnotation(DependentObject.class); Dependent overrideAnn = (Dependent) annotedElt.getAnnotation(Dependent.class); Map<String, Column[]> columnOverride = new HashMap<String, Column[]>(); if (overrideAnn != null) { for ( DependentAttribute depAttr : overrideAnn.value() ) { columnOverride.put( depAttr.name(), depAttr.column() ); } } final boolean isComponent = depAnn != null; final boolean propertyAccess = isComponent ? depAnn.access() == AccessType.PROPERTY : false; //for now columns.length > 1 is impossible for @Id bindId( generatorType( ann.generate() ), ann.generator(), inferredData, columns[0], (RootClass) propertyHolder.getPersistentClass(), localGenerators, localGeneratorTables, isComponent, columnOverride, propertyAccess, secondaryTables, mappings); log.debug("Bind @Id on " + inferredData.getPropertyName() ); } else if ( annotedElt.isAnnotationPresent(Version.class) ) { log.debug(inferredData.getPropertyName() + " is a version property"); RootClass rootClass = (RootClass) propertyHolder.getPersistentClass(); boolean lazy = false; //for now columns.length > 1 is impossible for @Version Property prop = bindProperty( inferredData.getPropertyName(), inferredData.getReturnedClassName(), lazy, inferredData.getDefaultAccess(), columns[0], PropertyHolderBuilder.buildPropertyHolder(rootClass), ""); rootClass.setVersion(prop); SimpleValue simpleValue = (SimpleValue) prop.getValue(); if ( !simpleValue.isTypeSpecified() ) simpleValue.setTypeName("integer"); simpleValue.setNullValue("undefined"); rootClass.setOptimisticLockMode(Versioning.OPTIMISTIC_LOCK_VERSION); log.debug( "Version name: " + rootClass.getVersion().getName() + ", unsavedValue: " + ( (SimpleValue) rootClass.getVersion().getValue() ).getNullValue() ); } else if ( annotedElt.isAnnotationPresent(ManyToOne.class) ) { ManyToOne ann = (ManyToOne) annotedElt.getAnnotation(ManyToOne.class); bindManyToOne( getCascadeStrategy( ann.cascade() ), (Ejb3JoinColumn[]) columns, ann.optional(), getFetchMode( ann.fetch() ), inferredData.getPropertyName(), inferredData.getReturnedClassName(), ann.targetEntity(), inferredData.getDefaultAccess(), propertyHolder, mappings ); } else if ( annotedElt.isAnnotationPresent(OneToOne.class) ) { OneToOne ann = annotedElt.getAnnotation(OneToOne.class); if (columns == null) { columns = new Ejb3JoinColumn[0]; } bindOneToOne( getCascadeStrategy( ann.cascade() ), (Ejb3JoinColumn[]) columns, ann.optional(), getFetchMode( ann.fetch() ), inferredData.getPropertyName(), inferredData.getReturnedClassName(), ann.targetEntity(), inferredData.getDefaultAccess(), propertyHolder, mappings ); } else if ( annotedElt.isAnnotationPresent(OneToMany.class) ) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -