📄 abstractentitypersister.java
字号:
public boolean isVersioned() { return versioned; } public boolean isIdentifierAssignedByInsert() { return useIdentityColumn; } public boolean isUnsaved(Object object) throws HibernateException { if ( isVersioned() ) { // let this take precedence if defined, since it works for // assigned identifiers Boolean result = unsavedVersionValue.isUnsaved( getVersion(object) ); if (result!=null) return result.booleanValue(); } if ( hasIdentifierPropertyOrEmbeddedCompositeIdentifier() ) { return unsavedIdentifierValue.isUnsaved( getIdentifier(object) ); } else { // we always assume a transient instance with no identifier // or version property is unsaved! return true; } } public String[] getPropertyNames() { return propertyNames; } public String getIdentifierPropertyName() { return identifierPropertyName; } public String getVersionColumnName() { return versionColumnName; } public boolean implementsLifecycle() { return implementsLifecycle; } public boolean implementsValidatable() { return implementsValidatable; } public boolean hasCollections() { return hasCollections; } public boolean isMutable() { return mutable; } public boolean hasCache() { return cache!=null; } public boolean hasSubclasses() { return hasSubclasses; } public boolean hasProxy() { return hasProxy; } /** * The query that returns the generated identifier for an identity column */ protected final String sqlIdentitySelect() { return identitySelectString; } public IdentifierGenerator getIdentifierGenerator() throws HibernateException { return identifierGenerator; } protected void check(int rows, Serializable id) throws HibernateException { if (rows<1) { throw new StaleObjectStateException( getMappedClass(), id ); } else if (rows>1) { throw new HibernateException( "Duplicate identifier in table for " + getClassName() + ": " + id ); } } protected abstract String[] getActualPropertyColumnNames(int i); protected abstract String getFormulaTemplate(int i); protected void initPropertyPaths(Mapping mapping) throws MappingException { for ( int i=0; i<propertyNames.length; i++ ) { initPropertyPaths( propertyNames[i], propertyTypes[i], getActualPropertyColumnNames(i), getFormulaTemplate(i), mapping ); } String idProp = getIdentifierPropertyName(); if (idProp!=null) initPropertyPaths( idProp, getIdentifierType(), getIdentifierColumnNames(), mapping ); if ( hasEmbeddedIdentifier() ) initPropertyPaths( null, getIdentifierType(), getIdentifierColumnNames(), mapping ); initPropertyPaths( ENTITY_ID, getIdentifierType(), getIdentifierColumnNames(), mapping ); if ( isPolymorphic() ) { addPropertyPath( ENTITY_CLASS, getDiscriminatorType(), new String[] { getDiscriminatorColumnName() } ); } } protected AbstractEntityPersister(PersistentClass model, SessionFactoryImplementor factory) throws HibernateException { this.dialect = factory.getDialect(); // CLASS className = model.getMappedClass().getName(); mappedClass = model.getMappedClass(); mutable = model.isMutable(); selectBeforeUpdate = model.hasSelectBeforeUpdate(); dynamicUpdate = model.useDynamicUpdate(); dynamicInsert = model.useDynamicInsert(); sqlWhereString = model.getWhere(); sqlWhereStringTemplate = sqlWhereString==null ? null : Template.renderWhereStringTemplate(sqlWhereString, dialect); polymorphic = model.isPolymorphic(); explicitPolymorphism = model.isExplicitPolymorphism(); inherited = model.isInherited(); superclass = inherited ? model.getSuperclass().getMappedClass() : null; hasSubclasses = model.hasSubclasses(); batchSize = model.getBatchSize(); constructor = ReflectHelper.getDefaultConstructor(mappedClass); abstractClass = ReflectHelper.isAbstractClass(mappedClass); entityType = Hibernate.entity(mappedClass); optimisticLockMode = model.getOptimisticLockMode(); if (optimisticLockMode > Versioning.OPTIMISTIC_LOCK_VERSION && !dynamicUpdate) { throw new MappingException("optimistic-lock setting requires dynamic-update=\"true\": " + className); } // IDENTIFIER hasEmbeddedIdentifier = model.hasEmbeddedIdentifier(); Value idValue = model.getIdentifier(); identifierType = idValue.getType(); //PropertyAccessor pa = null; if ( model.hasIdentifierProperty() ) { Property idProperty = model.getIdentifierProperty(); identifierPropertyName = idProperty.getName(); identifierSetter = idProperty.getSetter(mappedClass); identifierGetter = idProperty.getGetter(mappedClass); } else { identifierPropertyName = null; identifierGetter = null; identifierSetter = null; } Class prox = model.getProxyInterface(); Method proxyGetter = null; Method proxySetter = null; if ( model.hasIdentifierProperty() && prox!=null ) { Property idProperty = model.getIdentifierProperty(); try { proxyGetter = idProperty.getGetter(prox).getMethod(); } catch (PropertyNotFoundException pnfe) {} try { proxySetter = idProperty.getSetter(prox).getMethod(); } catch (PropertyNotFoundException pnfe) {} } proxyGetIdentifierMethod = proxyGetter; proxySetIdentifierMethod = proxySetter; // HYDRATE SPAN int m=0; Iterator iter = model.getPropertyClosureIterator(); while ( iter.hasNext() ) { m++; iter.next(); } hydrateSpan=m; // IDENTIFIER int idColumnSpan = model.getIdentifier().getColumnSpan(); identifierColumnNames = new String[idColumnSpan]; identifierAliases = new String[idColumnSpan]; iter = idValue.getColumnIterator(); int i=0; while ( iter.hasNext() ) { Column col = (Column) iter.next(); identifierColumnNames[i] = col.getQuotedName(dialect); identifierAliases[i] = col.getAlias(); // getAlias() handles quotes i++; } // GENERATOR identifierGenerator = model.getIdentifier().createIdentifierGenerator(dialect); useIdentityColumn = identifierGenerator instanceof IdentityGenerator; identitySelectString = useIdentityColumn ? dialect.getIdentitySelectString() : null; // UNSAVED-VALUE: String unsavedValue = model.getIdentifier().getNullValue(); if ( unsavedValue==null || "null".equals(unsavedValue) ) { unsavedIdentifierValue=Cascades.SAVE_NULL; } else if ( "none".equals(unsavedValue) ) { unsavedIdentifierValue=Cascades.SAVE_NONE; } else if ( "any".equals(unsavedValue) ) { unsavedIdentifierValue=Cascades.SAVE_ANY; } else { Type idType = model.getIdentifier().getType(); try { unsavedIdentifierValue = new Cascades.IdentifierValue( ( (IdentifierType) idType ).stringToObject(unsavedValue) ); } catch (ClassCastException cce) { throw new MappingException("Bad identifier type: " + idType.getClass().getName() ); } catch (Exception e) { throw new MappingException("Could not parse identifier unsaved-value: " + unsavedValue); } } // VERSION: if ( model.isVersioned() ) { versionColumnName = ( (Column) model.getVersion().getColumnIterator().next() ).getQuotedName(dialect); } else { versionColumnName = null; } if ( model.isVersioned() ) { //versionPropertyName = model.getVersion().getName(); versioned = true; versionGetter = model.getVersion().getGetter(mappedClass); versionType = (VersionType) model.getVersion().getType(); } else { //versionPropertyName = null; versioned = false; versionType = null; versionGetter = null; } // VERSION UNSAVED-VALUE: String versionUnsavedValue = null; if ( model.isVersioned() ) { versionUnsavedValue = model.getVersion().getNullValue(); } if ( versionUnsavedValue==null || "undefined".equals(versionUnsavedValue) ) { unsavedVersionValue = Cascades.VERSION_UNDEFINED; } else if ( "null".equals(versionUnsavedValue) ) { unsavedVersionValue=Cascades.VERSION_SAVE_NULL; } else if ( "negative".equals(versionUnsavedValue) ) { unsavedVersionValue=Cascades.VERSION_NEGATIVE; /* * used to be none and any strategies but this is kind of a non sense for version * especially for none since an 'update where' would be generated * Lot's of hack to support none ? */ } else { // this should not happend since the DTD prevent it. throw new MappingException("Could not parse version unsaved-value: " + versionUnsavedValue); } // PROPERTIES propertyTypes = new Type[hydrateSpan]; propertyNames = new String[hydrateSpan]; propertyUpdateability = new boolean[hydrateSpan]; propertyInsertability = new boolean[hydrateSpan]; propertyNullability = new boolean[hydrateSpan]; getters = new Getter[hydrateSpan]; setters = new Setter[hydrateSpan]; cascadeStyles = new Cascades.CascadeStyle[hydrateSpan]; String[] setterNames = new String[hydrateSpan]; String[] getterNames = new String[hydrateSpan]; Class[] types = new Class[hydrateSpan]; iter = model.getPropertyClosureIterator(); i=0; int tempVersionProperty=-66; boolean foundCascade = false; boolean foundCustomAccessor = false; while( iter.hasNext() ) { Property prop = (Property) iter.next(); if ( prop==model.getVersion() ) tempVersionProperty = i; propertyNames[i] = prop.getName(); if ( !( prop.isBasicPropertyAccessor() ) ) foundCustomAccessor=true; getters[i] = prop.getGetter(mappedClass); setters[i] = prop.getSetter(mappedClass); getterNames[i]= getters[i].getMethodName(); setterNames[i]= setters[i].getMethodName(); types[i] = getters[i].getReturnType(); propertyTypes[i] = prop.getType(); propertyUpdateability[i] = prop.isUpdateable(); propertyInsertability[i] = prop.isInsertable(); propertyNullability[i] = prop.isNullable(); gettersByPropertyName.put( propertyNames[i], getters[i] ); settersByPropertyName.put( propertyNames[i], setters[i] ); typesByPropertyName.put( propertyNames[i], propertyTypes[i] ); cascadeStyles[i] = prop.getCascadeStyle(); if ( cascadeStyles[i]!=Cascades.STYLE_NONE ) foundCascade = true; i++; } optimizer = !foundCustomAccessor && Environment.useReflectionOptimizer() ? ReflectHelper.getBulkBean(mappedClass, getterNames, setterNames, types) : null; fastClass = FastClass.create(mappedClass); hasCascades = foundCascade; versionProperty = tempVersionProperty; // CALLBACK INTERFACES implementsLifecycle = Lifecycle.class.isAssignableFrom(mappedClass); implementsValidatable = Validatable.class.isAssignableFrom(mappedClass); cache = model.getCache(); hasCollections = initHasCollections(); // PROXIES concreteProxyClass = model.getProxyInterface(); hasProxy = concreteProxyClass!=null; if (hasProxy) { HashSet proxyInterfaceSet = new HashSet(); proxyInterfaceSet.add(HibernateProxy.class); if ( !mappedClass.equals(concreteProxyClass) ) { if ( !concreteProxyClass.isInterface() ) throw new MappingException( "proxy must be either an interface, or the class itself: " + mappedClass.getName() ); proxyInterfaceSet.add(concreteProxyClass); } if ( mappedClass.isInterface() ) proxyInterfaceSet.add(mappedClass); if (hasProxy) { iter = model.getSubclassIterator(); while ( iter.hasNext() ) { Subclass subclass = (Subclass) iter.next(); Class subclassProxy = subclass.getProxyInterface(); if (subclassProxy==null) throw new MappingException( "All subclasses must also have proxies: " + mappedClass.getName() ); if ( !subclass.getMappedClass().equals(subclassProxy) ) proxyInterfaceSet.add(subclassProxy); } } proxyInterfaces = (Class[]) proxyInterfaceSet.toArray(NO_CLASSES); proxyFactory = hasProxy ? CGLIBLazyInitializer.getProxyFactory(mappedClass, proxyInterfaces) : null; } else { proxyFactory = null; proxyInterfaces = null; } } //This is really ugly, but necessary: /** * Must be called by subclasses, at the end of their constructors */ protected void initSubclassPropertyAliasesMap(PersistentClass model) throws MappingException { // ALIASES Iterator iter = model.getSubclassPropertyClosureIterator(); while ( iter.hasNext() ) { Property prop = (Property) iter.next(); String propname = prop.getName(); String aliases[]; String[] cols; if ( prop.isFormula() ) { aliases = new String[1];
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -