📄 binderhelper.java
字号:
else { Iterator columnIt = property.getColumnIterator(); while ( columnIt.hasNext() ) { Object column = columnIt.next(); //can be a Formula so we don't cast //noinspection SuspiciousMethodCalls if ( columnsToProperty.containsKey( column ) ) { columnsToProperty.get( column ).add( property ); } } } } /** * Retrieve the property by path in a recursive way, including IndetifierProperty in the loop * If propertyName is null or empty, the IdentifierProperty is returned */ public static Property findPropertyByName(PersistentClass associatedClass, String propertyName) { Property property = null; Property idProperty = associatedClass.getIdentifierProperty(); String idName = idProperty != null ? idProperty.getName() : null; try { if ( propertyName == null || propertyName.length() == 0 || propertyName.equals( idName ) ) { //default to id property = idProperty; } else { if ( propertyName.indexOf( idName + "." ) == 0 ) { property = idProperty; propertyName = propertyName.substring( idName.length() + 1 ); } StringTokenizer st = new StringTokenizer( propertyName, ".", false ); while ( st.hasMoreElements() ) { String element = (String) st.nextElement(); if ( property == null ) { property = associatedClass.getProperty( element ); } else { if ( !property.isComposite() ) return null; property = ( (Component) property.getValue() ).getProperty( element ); } } } } catch (MappingException e) { try { //if we do not find it try to check the identifier mapper if ( associatedClass.getIdentifierMapper() == null ) return null; StringTokenizer st = new StringTokenizer( propertyName, ".", false ); while ( st.hasMoreElements() ) { String element = (String) st.nextElement(); if ( property == null ) { property = associatedClass.getIdentifierMapper().getProperty( element ); } else { if ( !property.isComposite() ) return null; property = ( (Component) property.getValue() ).getProperty( element ); } } } catch (MappingException ee) { return null; } } return property; } public static String getRelativePath(PropertyHolder propertyHolder, String propertyName) { if ( propertyHolder == null ) return propertyName; String path = propertyHolder.getPath(); String entityName = propertyHolder.getPersistentClass().getEntityName(); if ( path.length() == entityName.length() ) { return propertyName; } else { return StringHelper.qualify( path.substring( entityName.length() + 1 ), propertyName ); } } /** * Find the column owner (ie PersistentClass or Join) of columnName. * If columnName is null or empty, persistentClass is returned */ public static Object findColumnOwner( PersistentClass persistentClass, String columnName, ExtendedMappings mappings ) { if ( StringHelper.isEmpty( columnName ) ) { return persistentClass; //shortcut for implicit referenced column names } PersistentClass current = persistentClass; Object result = null; boolean found = false; do { result = current; Table currentTable = current.getTable(); try { mappings.getPhysicalColumnName( columnName, currentTable ); found = true; } catch (MappingException me) { //swallow it } Iterator joins = current.getJoinIterator(); while ( !found && joins.hasNext() ) { result = joins.next(); currentTable = ( (Join) result ).getTable(); try { mappings.getPhysicalColumnName( columnName, currentTable ); found = true; } catch (MappingException me) { //swallow it } } current = current.getSuperclass(); } while ( !found && current != null ); return found ? result : null; } /** * apply an id generator to a SimpleValue */ public static void makeIdGenerator( SimpleValue id, String generatorType, String generatorName, ExtendedMappings mappings, Map<String, IdGenerator> localGenerators ) { Table table = id.getTable(); table.setIdentifierValue( id ); //generator settings id.setIdentifierGeneratorStrategy( generatorType ); Properties params = new Properties(); //always settable params.setProperty( PersistentIdentifierGenerator.TABLE, table.getName() ); if ( id.getColumnSpan() == 1 ) { 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 ); } //This is quite vague in the spec but a generator could override the generate choice String identifierGeneratorStrategy = gen.getIdentifierGeneratorStrategy(); //yuk! this is a hack not to override 'AUTO' even if generator is set final boolean avoidOverriding = identifierGeneratorStrategy.equals( "identity" ) || identifierGeneratorStrategy.equals( "seqhilo" ) || identifierGeneratorStrategy.equals( MultipleHiLoPerTableGenerator.class.getName() ); if ( generatorType == null || !avoidOverriding ) { id.setIdentifierGeneratorStrategy( identifierGeneratorStrategy ); } //checkIfMatchingGenerator(gen, 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 ( "assigned".equals( generatorType ) ) id.setNullValue( "undefined" ); id.setIdentifierGeneratorProperties( params ); } public static boolean isDefault(String annotationString) { return annotationString != null && annotationString.length() == 0; //equivalent to (but faster) ANNOTATION_STRING_DEFAULT.equals( annotationString ); } public static Any buildAnyValue(String anyMetaDefName, Ejb3JoinColumn[] columns, javax.persistence.Column metaColumn, PropertyData inferredData, boolean cascadeOnDelete, Nullability nullability, PropertyHolder propertyHolder, EntityBinder entityBinder, boolean optional, ExtendedMappings mappings) { //All FK columns should be in the same table Any value = new Any( columns[0].getTable() ); AnyMetaDef metaAnnDef = inferredData.getProperty().getAnnotation( AnyMetaDef.class ); if ( metaAnnDef != null ) { //local has precedence over general and can be mapped for future reference if named bindAnyMetaDefs( inferredData.getProperty(), mappings ); } else { metaAnnDef = mappings.getAnyMetaDef( anyMetaDefName ); } if ( metaAnnDef != null ) { value.setIdentifierType( metaAnnDef.idType() ); value.setMetaType( metaAnnDef.metaType() ); HashMap values = new HashMap(); org.hibernate.type.Type metaType = TypeFactory.heuristicType( value.getMetaType() ); for (MetaValue metaValue : metaAnnDef.metaValues()) { try { Object discrim = ( (org.hibernate.type.DiscriminatorType) metaType ).stringToObject( metaValue .value() ); String entityName = metaValue.targetEntity().getName(); values.put( discrim, entityName ); } catch (ClassCastException cce) { throw new MappingException( "metaType was not a DiscriminatorType: " + metaType.getName() ); } catch (Exception e) { throw new MappingException( "could not interpret metaValue", e ); } } if ( !values.isEmpty() ) value.setMetaValues( values ); } else { throw new AnnotationException( "Unable to find @AnyMetaDef for an @(ManyTo)Any mapping: " + StringHelper.qualify( propertyHolder.getPath(), inferredData.getPropertyName() ) ); } value.setCascadeDeleteEnabled( cascadeOnDelete ); if ( !optional ) { for (Ejb3JoinColumn column : columns) { column.setNullable( false ); } } Ejb3Column[] metaColumns = Ejb3Column.buildColumnFromAnnotation( new javax.persistence.Column[] { metaColumn }, null, nullability, propertyHolder, inferredData, entityBinder.getSecondaryTables(), mappings ); //set metaColumn to the right table for (Ejb3Column column : metaColumns) { column.setTable( value.getTable() ); } //meta column for (Ejb3Column column : metaColumns) { column.linkWithValue( value ); } //id columns final String propertyName = inferredData.getPropertyName(); Ejb3Column.checkPropertyConsistency( columns, propertyHolder.getEntityName() + propertyName ); for (Ejb3JoinColumn column : columns) { column.linkWithValue( value ); } return value; } public static void bindAnyMetaDefs(XAnnotatedElement annotatedElement, ExtendedMappings mappings) { AnyMetaDef defAnn = annotatedElement.getAnnotation( AnyMetaDef.class ); AnyMetaDefs defsAnn = annotatedElement.getAnnotation( AnyMetaDefs.class ); boolean mustHaveName = XClass.class.isAssignableFrom( annotatedElement.getClass() ) || XPackage.class.isAssignableFrom( annotatedElement.getClass() ); if ( defAnn != null ) { checkAnyMetaDefValidity( mustHaveName, defAnn, annotatedElement ); bindAnyMetaDef( defAnn, mappings ); } if ( defsAnn != null ) { for (AnyMetaDef def : defsAnn.value()) { checkAnyMetaDefValidity( mustHaveName, def, annotatedElement ); bindAnyMetaDef( def, mappings ); } } } private static void checkAnyMetaDefValidity(boolean mustHaveName, AnyMetaDef defAnn, XAnnotatedElement annotatedElement) { if ( mustHaveName && isDefault( defAnn.name() ) ) { String name = XClass.class.isAssignableFrom( annotatedElement.getClass() ) ? ( (XClass) annotatedElement ).getName() : ( (XPackage) annotatedElement ).getName(); throw new AnnotationException( "@AnyMetaDef.name cannot be null on an entity or a package: " + name ); } } private static void bindAnyMetaDef(AnyMetaDef defAnn, ExtendedMappings mappings) { if ( isDefault( defAnn.name() ) ) return; //don't map not named definitions if ( log.isInfoEnabled() ) log.info( "Binding Any Meta definition: " + defAnn.name() ); mappings.addAnyMetaDef( defAnn ); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -