📄 cascades.java
字号:
public String toString() { return "VERSION_UNDEFINED"; } }; /** * Assume the transient instance is newly instantiated if the version * is negative, otherwise assume it is a detached instance. */ public static final VersionValue VERSION_NEGATIVE = new VersionValue() { public final Boolean isUnsaved(Object version) throws MappingException { log.trace("version unsaved-value strategy NEGATIVE"); if (version==null) return Boolean.TRUE; if (version instanceof Number) { return ( (Number) version ).longValue() < 0l ? Boolean.TRUE : Boolean.FALSE; } else { throw new MappingException("unsaved-value NEGATIVE may only be used with short, int and long types"); } } public Object getDefaultValue(Object currentValue) { return IdentifierGeneratorFactory.createNumber( -1l, currentValue.getClass() ); } public String toString() { return "VERSION_NEGATIVE"; } }; /** * Cascade an action to the child or children */ private static void cascade( final SessionImplementor session, final Object child, final Type type, final CascadingAction action, final CascadeStyle style, final int cascadeTo, final Object anything, final boolean isCascadeDeleteEnabled) throws HibernateException { if (child!=null) { if ( type.isAssociationType() ) { AssociationType associationType = (AssociationType) type; boolean cascadeNow = associationType.getForeignKeyDirection().cascadeNow(cascadeTo) && ( session.getEntityMode()!=EntityMode.DOM4J || associationType.isEmbeddedInXML() ); if ( cascadeNow ) { cascadeAssociation( session, child, type, action, style, cascadeTo, anything, isCascadeDeleteEnabled ); } } else if ( type.isComponentType() ) { AbstractComponentType componentType = (AbstractComponentType) type; Object[] children = componentType.getPropertyValues(child, session); Type[] types = componentType.getSubtypes(); for ( int i=0; i<types.length; i++ ) { CascadeStyle componentPropertyStyle = componentType.getCascadeStyle(i); if ( componentPropertyStyle.doCascade(action) ) { cascade( session, children[i], types[i], action, componentPropertyStyle, cascadeTo, anything, false ); } } } } } private static void cascadeAssociation( final SessionImplementor session, final Object child, final Type type, final CascadingAction action, final CascadeStyle style, final int cascadeTo, final Object anything, final boolean isCascadeDeleteEnabled ) { if ( type.isEntityType() || type.isAnyType() ) { final String entityName = type.isEntityType() ? ( (EntityType) type ).getAssociatedEntityName() : null; if ( style.reallyDoCascade(action) ) { //not really necessary, but good for consistency... action.cascade(session, child, entityName, anything, isCascadeDeleteEnabled); } } else if ( type.isCollectionType() ) { final int cascadeVia; if ( cascadeTo==CASCADE_AFTER_INSERT_BEFORE_DELETE) { cascadeVia = CASCADE_AFTER_INSERT_BEFORE_DELETE_VIA_COLLECTION; } else { cascadeVia = cascadeTo; } CollectionType pctype = (CollectionType) type; CollectionPersister persister = session.getFactory() .getCollectionPersister( pctype.getRole() ); Type elemType = persister.getElementType(); //cascade to current collection elements if ( elemType.isEntityType() || elemType.isAnyType() || elemType.isComponentType() ) { cascadeCollection( action, style, pctype, elemType, child, cascadeVia, session, anything, persister.isCascadeDeleteEnabled() ); } } } /** * Cascade an action from the parent object to all its children */ public static void cascade( final SessionImplementor session, final EntityPersister persister, final Object parent, final Cascades.CascadingAction action, final int cascadeTo) throws HibernateException { cascade(session, persister, parent, action, cascadeTo, null); } /** * Cascade an action from the parent object to all its children */ public static void cascade( final SessionImplementor session, final EntityPersister persister, final Object parent, final Cascades.CascadingAction action, final int cascadeTo, final Object anything) throws HibernateException { if ( persister.hasCascades() ) { // performance opt if ( log.isTraceEnabled() ) { log.trace( "processing cascade " + action + " for: " + persister.getEntityName() ); } Type[] types = persister.getPropertyTypes(); Cascades.CascadeStyle[] cascadeStyles = persister.getPropertyCascadeStyles(); for ( int i=0; i<types.length; i++) { CascadeStyle style = cascadeStyles[i]; if ( style.doCascade(action) ) { // associations cannot be field-level lazy="true", so don't // need to check that the field is fetched (laziness for // associations is always done by proxying currently) cascade( session, persister.getPropertyValue( parent, i, session.getEntityMode() ), types[i], action, style, cascadeTo, anything, false ); } } if ( log.isTraceEnabled() ) { log.trace( "done processing cascade " + action + " for: " + persister.getEntityName() ); } } } /** * Cascade to the collection elements */ private static void cascadeCollection( final CascadingAction action, final CascadeStyle style, final CollectionType collectionType, final Type elemType, final Object child, final int cascadeVia, final SessionImplementor session, final Object anything, final boolean isCascadeDeleteEnabled) throws HibernateException { // we can't cascade to non-embedded elements boolean embeddedElements = session.getEntityMode()!=EntityMode.DOM4J || ( (EntityType) collectionType.getElementType( session.getFactory() ) ).isEmbeddedInXML(); boolean reallyDoCascade = style.reallyDoCascade(action) && embeddedElements && child!=CollectionType.UNFETCHED_COLLECTION; if ( reallyDoCascade ) { if ( log.isTraceEnabled() ) { log.trace( "cascade " + action + " for collection: " + collectionType.getRole() ); } Iterator iter = action.getCascadableChildrenIterator(session, collectionType, child); while ( iter.hasNext() ) { cascade( session, iter.next(), elemType, action, style, cascadeVia, anything, isCascadeDeleteEnabled ); } if ( log.isTraceEnabled() ) { log.trace( "done cascade " + action + " for collection: " + collectionType.getRole() ); } } final boolean deleteOrphans = style.hasOrphanDelete() && action.deleteOrphans() && elemType.isEntityType() && child instanceof PersistentCollection; //a newly instantiated collection can't have orphans if ( deleteOrphans ) { // handle orphaned entities!! if ( log.isTraceEnabled() ) { log.trace( "deleting orphans for collection: " + collectionType.getRole() ); } // we can do the cast since orphan-delete does not apply to: // 1. newly instantiated collections // 2. arrays (we can't track orphans for detached arrays) final String entityName = collectionType.getAssociatedEntityName( session.getFactory() ); deleteOrphans( entityName, (PersistentCollection) child, session ); if ( log.isTraceEnabled() ) { log.trace( "done deleting orphans for collection: " + collectionType.getRole() ); } } } /** * Delete any entities that were removed from the collection */ private static void deleteOrphans(String entityName, PersistentCollection pc, SessionImplementor session) throws HibernateException { if ( pc.wasInitialized() ) { //can't be any orphans if it was not initialized! CollectionEntry ce = session.getPersistenceContext().getCollectionEntry(pc); if (ce!=null) { Iterator orphanIter = ce.getOrphans(entityName, pc).iterator(); while ( orphanIter.hasNext() ) { Object orphan = orphanIter.next(); if (orphan!=null) { if ( log.isTraceEnabled() ) log.trace("deleting orphaned: " + entityName); session.delete(entityName, orphan, false); } } } } } /** * Iterate just the elements of the collection that are already there. Don't load * any new elements from the database. */ public static Iterator getLoadedElementsIterator(SessionImplementor session, CollectionType collectionType, Object collection) { if ( collectionIsInitialized(collection) ) { // handles arrays and newly instantiated collections return collectionType.getElementsIterator(collection, session); } else { // does not handle arrays (thats ok, cos they can't be lazy) // or newly instantiated collections, so we can do the cast return ( (PersistentCollection) collection ).queuedAdditionIterator(); } } /** * Iterate all the collection elements, loading them from the database if necessary. */ private static Iterator getAllElementsIterator(SessionImplementor session, CollectionType collectionType, Object collection) { return collectionType.getElementsIterator(collection, session); } private static final Map STYLES = new HashMap(); static { STYLES.put("all", STYLE_ALL); STYLES.put("all-delete-orphan", STYLE_ALL_DELETE_ORPHAN); STYLES.put("save-update", STYLE_SAVE_UPDATE); STYLES.put("persist", STYLE_PERSIST); STYLES.put("merge", STYLE_MERGE); STYLES.put("lock", STYLE_LOCK); STYLES.put("refresh", STYLE_REFRESH); STYLES.put("replicate", STYLE_REPLICATE); STYLES.put("evict", STYLE_EVICT); STYLES.put("delete", STYLE_DELETE); STYLES.put("delete-orphan", STYLE_DELETE_ORPHAN); STYLES.put("none", STYLE_NONE); } public static CascadeStyle getCascadeStyle(String cascade) { CascadeStyle style = (CascadeStyle) STYLES.get(cascade); if (style==null) { throw new MappingException("Unsupported cascade style: " + cascade); } else { return style; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -