下面列出了org.hibernate.cache.spi.access.SoftLock#org.hibernate.type.CollectionType 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public void evictCollection(Object value, CollectionType type) {
final Object pc;
if ( type.hasHolder( getSession().getEntityMode() ) ) {
pc = getSession().getPersistenceContext().removeCollectionHolder(value);
}
else if ( value instanceof PersistentCollection ) {
pc = value;
}
else {
return; //EARLY EXIT!
}
PersistentCollection collection = (PersistentCollection) pc;
if ( collection.unsetSession( getSession() ) ) evictCollection(collection);
}
public CollectionType getDefaultCollectionType() {
if ( isSorted() ) {
return getMetadata().getTypeResolver()
.getTypeFactory()
.sortedMap( getRole(), getReferencedPropertyName(), getComparator() );
}
else if ( hasOrder() ) {
return getMetadata().getTypeResolver()
.getTypeFactory()
.orderedMap( getRole(), getReferencedPropertyName() );
}
else {
return getMetadata().getTypeResolver()
.getTypeFactory()
.map( getRole(), getReferencedPropertyName() );
}
}
public CollectionType getDefaultCollectionType() {
if ( isSorted() ) {
return getMetadata().getTypeResolver()
.getTypeFactory()
.sortedSet( getRole(), getReferencedPropertyName(), getComparator() );
}
else if ( hasOrder() ) {
return getMetadata().getTypeResolver()
.getTypeFactory()
.orderedSet( getRole(), getReferencedPropertyName() );
}
else {
return getMetadata().getTypeResolver()
.getTypeFactory()
.set( getRole(), getReferencedPropertyName() );
}
}
protected QueryableCollection getQueryableCollection(
String entityName,
String propertyName,
SessionFactoryImplementor factory) throws HibernateException {
final PropertyMapping ownerMapping = (PropertyMapping) factory.getEntityPersister( entityName );
final Type type = ownerMapping.toType( propertyName );
if ( !type.isCollectionType() ) {
throw new MappingException(
"Property path [" + entityName + "." + propertyName + "] does not reference a collection"
);
}
final String role = ( (CollectionType) type ).getRole();
try {
return (QueryableCollection) factory.getCollectionPersister( role );
}
catch ( ClassCastException cce ) {
throw new QueryException( "collection role is not queryable: " + role );
}
catch ( Exception e ) {
throw new QueryException( "collection role not found: " + role );
}
}
public JoinDefinedByMetadata createCollectionJoin(
QuerySpace leftHandSide,
String lhsPropertyName,
CollectionQuerySpace rightHandSide,
boolean rightHandSideRequired,
CollectionType joinedPropertyType,
SessionFactoryImplementor sessionFactory) {
return new JoinImpl(
leftHandSide,
lhsPropertyName,
rightHandSide,
joinedPropertyType.getAssociatedJoinable( sessionFactory ).getKeyColumnNames(),
joinedPropertyType,
rightHandSideRequired
);
}
protected Serializable getCollectionKey(
CollectionPersister persister,
Object owner,
EntityEntry ownerEntry,
SharedSessionContractImplementor session) {
final CollectionType collectionType = persister.getCollectionType();
if ( ownerEntry != null ) {
// this call only works when the owner is associated with the Session, which is not always the case
return collectionType.getKeyOfOwner( owner, session );
}
if ( collectionType.getLHSPropertyName() == null ) {
// collection key is defined by the owning entity identifier
return persister.getOwnerEntityPersister().getIdentifier( owner, session );
}
else {
return (Serializable) persister.getOwnerEntityPersister().getPropertyValue( owner, collectionType.getLHSPropertyName() );
}
}
/**
* check sub elements-nullability. Returns property path that break
* nullability or null if none
*
* @param propertyType type to check
* @param value value to check
*
* @return property path
* @throws HibernateException error while getting subcomponent values
*/
private String checkSubElementsNullability(Type propertyType, Object value) throws HibernateException {
if ( propertyType.isComponentType() ) {
return checkComponentNullability( value, (CompositeType) propertyType );
}
if ( propertyType.isCollectionType() ) {
// persistent collections may have components
final CollectionType collectionType = (CollectionType) propertyType;
final Type collectionElementType = collectionType.getElementType( session.getFactory() );
if ( collectionElementType.isComponentType() ) {
// check for all components values in the collection
final CompositeType componentType = (CompositeType) collectionElementType;
final Iterator itr = CascadingActions.getLoadedElementsIterator( session, collectionType, value );
while ( itr.hasNext() ) {
final Object compositeElement = itr.next();
if ( compositeElement != null ) {
return checkComponentNullability( compositeElement, componentType );
}
}
}
}
return null;
}
@Override
Object processCollection(Object collection, CollectionType collectionType)
throws HibernateException {
if ( collection != null && ( collection instanceof PersistentCollection ) ) {
final SessionImplementor session = getSession();
PersistentCollection coll = (PersistentCollection) collection;
if ( coll.setCurrentSession( session ) ) {
reattachCollection( coll, collectionType );
}
return null;
}
else {
return processArrayOrNewCollection( collection, collectionType );
}
}
public void end(QueryTranslatorImpl q) throws QueryException {
ignoreInitialJoin = false;
Type propertyType = getPropertyType();
if ( propertyType != null && propertyType.isCollectionType() ) {
collectionRole = ( ( CollectionType ) propertyType ).getRole();
collectionName = q.createNameForCollection( collectionRole );
prepareForIndex( q );
}
else {
columns = currentColumns();
setType();
}
//important!!
continuation = false;
}
private void evictCachedCollections(Type[] types, Serializable id, EventSource source)
throws HibernateException {
for ( Type type : types ) {
if ( type.isCollectionType() ) {
CollectionPersister collectionPersister = source.getFactory().getMetamodel().collectionPersister( ( (CollectionType) type ).getRole() );
if ( collectionPersister.hasCache() ) {
final CollectionDataAccess cache = collectionPersister.getCacheAccessStrategy();
final Object ck = cache.generateCacheKey(
id,
collectionPersister,
source.getFactory(),
source.getTenantIdentifier()
);
final SoftLock lock = cache.lockItem( source, ck, null );
cache.remove( source, ck );
source.getActionQueue().registerProcess( (success, session) -> cache.unlockItem( session, ck, lock ) );
}
}
else if ( type.isComponentType() ) {
CompositeType actype = (CompositeType) type;
evictCachedCollections( actype.getSubtypes(), id, source );
}
}
}
public void end(QueryTranslatorImpl q) throws QueryException {
ignoreInitialJoin = false;
Type propertyType = getPropertyType();
if ( propertyType != null && propertyType.isCollectionType() ) {
collectionRole = ( ( CollectionType ) propertyType ).getRole();
collectionName = q.createNameForCollection( collectionRole );
prepareForIndex( q );
}
else {
columns = currentColumns();
setType();
}
//important!!
continuation = false;
}
protected QueryableCollection getQueryableCollection(String entityName, String propertyName, SessionFactoryImplementor factory)
throws HibernateException {
PropertyMapping ownerMapping = ( PropertyMapping ) factory.getEntityPersister( entityName );
Type type = ownerMapping.toType( propertyName );
if ( !type.isCollectionType() ) {
throw new MappingException(
"Property path [" + entityName + "." + propertyName + "] does not reference a collection"
);
}
String role = ( ( CollectionType ) type ).getRole();
try {
return ( QueryableCollection ) factory.getCollectionPersister( role );
}
catch ( ClassCastException cce ) {
throw new QueryException( "collection role is not queryable: " + role );
}
catch ( Exception e ) {
throw new QueryException( "collection role not found: " + role );
}
}
public CollectionType getCollectionType() {
if ( typeName == null ) {
return getDefaultCollectionType();
}
else {
return getMetadata().getTypeConfiguration().getTypeResolver()
.getTypeFactory()
.customCollection( typeName, typeParameters, role, referencedPropertyName );
}
}
public void resolveIndex(AST parent) throws SemanticException {
// An ident node can represent an index expression if the ident
// represents a naked property ref
// *Note: this makes the assumption (which is currently the case
// in the hql-sql grammar) that the ident is first resolved
// itself (addrExpr -> resolve()). The other option, if that
// changes, is to call resolve from here; but it is
// currently un-needed overhead.
if (!(isResolved() && nakedPropertyRef)) {
throw new UnsupportedOperationException();
}
String propertyName = getOriginalText();
if (!getDataType().isCollectionType()) {
throw new SemanticException("Collection expected; [" + propertyName + "] does not refer to a collection property");
}
// TODO : most of below was taken verbatim from DotNode; should either delegate this logic or super-type it
CollectionType type = (CollectionType) getDataType();
String role = type.getRole();
QueryableCollection queryableCollection = getSessionFactoryHelper().requireQueryableCollection(role);
String alias = null; // DotNode uses null here...
String columnTableAlias = getFromElement().getTableAlias();
int joinType = JoinFragment.INNER_JOIN;
boolean fetch = false;
FromElementFactory factory = new FromElementFactory(
getWalker().getCurrentFromClause(),
getFromElement(),
propertyName,
alias,
getFromElement().toColumns(columnTableAlias, propertyName, false),
true
);
FromElement elem = factory.createCollection(queryableCollection, role, joinType, fetch, true);
setFromElement(elem);
getWalker().addQuerySpaces(queryableCollection.getCollectionSpaces()); // Always add the collection's query spaces.
}
Object processCollection(Object collection, CollectionType type)
throws HibernateException {
if ( collection == CollectionType.UNFETCHED_COLLECTION ) {
return null;
}
EventSource session = getSession();
CollectionPersister persister = session.getFactory().getCollectionPersister( type.getRole() );
if ( isUpdate ) {
removeCollection( persister, extractCollectionKeyFromOwner( persister ), session );
}
if ( collection != null && ( collection instanceof PersistentCollection ) ) {
PersistentCollection wrapper = ( PersistentCollection ) collection;
wrapper.setCurrentSession( session );
if ( wrapper.wasInitialized() ) {
session.getPersistenceContext().addNewCollection( persister, wrapper );
}
else {
reattachCollection( wrapper, type );
}
}
else {
// otherwise a null or brand new collection
// this will also (inefficiently) handle arrays, which
// have no snapshot, so we can't do any better
//processArrayOrNewCollection(collection, type);
}
return null;
}
@Override
public Iterator getCascadableChildrenIterator(
EventSource session,
CollectionType collectionType,
Object collection) {
// delete does cascade to uninitialized collections
return getAllElementsIterator( session, collectionType, collection );
}
@Override
public Iterator getCascadableChildrenIterator(
EventSource session,
CollectionType collectionType,
Object collection) {
// lock doesn't cascade to uninitialized collections
return getLoadedElementsIterator( session, collectionType, collection );
}
@Override
public Iterator getCascadableChildrenIterator(
EventSource session,
CollectionType collectionType,
Object collection) {
// refresh doesn't cascade to uninitialized collections
return getLoadedElementsIterator( session, collectionType, collection );
}
@Override
public Iterator getCascadableChildrenIterator(
EventSource session,
CollectionType collectionType,
Object collection) {
// saves / updates don't cascade to uninitialized collections
return getLoadedElementsIterator( session, collectionType, collection );
}
public void resolveIndex(AST parent) throws SemanticException {
if ( isResolved() ) {
return;
}
Type propertyType = prepareLhs(); // Prepare the left hand side and get the data type.
dereferenceCollection( ( CollectionType ) propertyType, true, true, null, parent );
}
@Override
public Iterator getCascadableChildrenIterator(
EventSource session,
CollectionType collectionType,
Object collection) {
// persists don't cascade to uninitialized collections
return getLoadedElementsIterator( session, collectionType, collection );
}
@Override
public Iterator getCascadableChildrenIterator(
EventSource session,
CollectionType collectionType,
Object collection) {
// persists don't cascade to uninitialized collections
return getLoadedElementsIterator( session, collectionType, collection );
}
@Override
public Iterator getCascadableChildrenIterator(
EventSource session,
CollectionType collectionType,
Object collection) {
// replicate does cascade to uninitialized collections
return getLoadedElementsIterator( session, collectionType, collection );
}
/**
* Iterate just the elements of the collection that are already there. Don't load
* any new elements from the database.
*/
public static Iterator getLoadedElementsIterator(
SharedSessionContractImplementor 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();
}
}
private static void cascadeAssociation(
final CascadingAction action,
final CascadePoint cascadePoint,
final EventSource eventSource,
final int componentPathStackDepth,
final Object parent,
final Object child,
final Type type,
final CascadeStyle style,
final Object anything,
final boolean isCascadeDeleteEnabled) {
if ( type.isEntityType() || type.isAnyType() ) {
cascadeToOne( action, eventSource, parent, child, type, style, anything, isCascadeDeleteEnabled );
}
else if ( type.isCollectionType() ) {
cascadeCollection(
action,
cascadePoint,
eventSource,
componentPathStackDepth,
parent,
child,
style,
anything,
(CollectionType) type
);
}
}
/**
* 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();
}
}
@Override
Object processCollection(Object collection, CollectionType type) throws HibernateException {
if ( collection == CollectionType.UNFETCHED_COLLECTION ) {
return null;
}
EventSource session = getSession();
CollectionPersister persister = session.getFactory().getCollectionPersister( type.getRole() );
final Serializable collectionKey = extractCollectionKeyFromOwner( persister );
if ( collection!=null && (collection instanceof PersistentCollection) ) {
PersistentCollection wrapper = (PersistentCollection) collection;
if ( wrapper.setCurrentSession(session) ) {
//a "detached" collection!
if ( !isOwnerUnchanged( wrapper, persister, collectionKey ) ) {
// if the collection belonged to a different entity,
// clean up the existing state of the collection
removeCollection( persister, collectionKey, session );
}
reattachCollection(wrapper, type);
}
else {
// a collection loaded in the current session
// can not possibly be the collection belonging
// to the entity passed to update()
removeCollection(persister, collectionKey, session);
}
}
else {
// null or brand new collection
// this will also (inefficiently) handle arrays, which have
// no snapshot, so we can't do any better
removeCollection(persister, collectionKey, session);
}
return null;
}
@Override
Object processCollection(Object collection, CollectionType type) throws HibernateException {
if (collection != null) {
evictCollection(collection, type);
}
return null;
}
@Override
public Object processCollection(Object collection, CollectionType type) throws HibernateException {
if ( collection == CollectionType.UNFETCHED_COLLECTION ) {
return null;
}
final EventSource session = getSession();
final CollectionPersister persister = session.getFactory().getMetamodel().collectionPersister( type.getRole() );
if ( isUpdate ) {
removeCollection( persister, extractCollectionKeyFromOwner( persister ), session );
}
if ( collection != null && collection instanceof PersistentCollection ) {
final PersistentCollection wrapper = (PersistentCollection) collection;
wrapper.setCurrentSession( (SessionImplementor) session );
if ( wrapper.wasInitialized() ) {
session.getPersistenceContext().addNewCollection( persister, wrapper );
}
else {
reattachCollection( wrapper, type );
}
}
else {
// otherwise a null or brand new collection
// this will also (inefficiently) handle arrays, which
// have no snapshot, so we can't do any better
//processArrayOrNewCollection(collection, type);
}
return null;
}
/**
* {@inheritDoc}
*/
Object processCollection(Object collection, CollectionType type) throws HibernateException {
if ( collection == CollectionType.UNFETCHED_COLLECTION ) {
return null;
}
EventSource session = getSession();
CollectionPersister persister = session.getFactory().getCollectionPersister( type.getRole() );
final Serializable collectionKey = extractCollectionKeyFromOwner( persister );
if ( collection!=null && (collection instanceof PersistentCollection) ) {
PersistentCollection wrapper = (PersistentCollection) collection;
if ( wrapper.setCurrentSession(session) ) {
//a "detached" collection!
if ( !isOwnerUnchanged( wrapper, persister, collectionKey ) ) {
// if the collection belonged to a different entity,
// clean up the existing state of the collection
removeCollection( persister, collectionKey, session );
}
reattachCollection(wrapper, type);
}
else {
// a collection loaded in the current session
// can not possibly be the collection belonging
// to the entity passed to update()
removeCollection(persister, collectionKey, session);
}
}
else {
// null or brand new collection
// this will also (inefficiently) handle arrays, which have
// no snapshot, so we can't do any better
removeCollection(persister, collectionKey, session);
}
return null;
}