@Override public Class<T> getParameterType() { return expectedType == null ? null : expectedType.getReturnedClass(); } }
private void addAssociationsToTheSetForOneProperty(String name, Type type, String prefix, SessionFactoryImplementor factory) { if ( type.isCollectionType() ) { CollectionType collType = (CollectionType) type; Type assocType = collType.getElementType( factory ); addAssociationsToTheSetForOneProperty(name, assocType, prefix, factory); } //ToOne association else if ( type.isEntityType() || type.isAnyType() ) { associations.add( prefix + name ); } else if ( type.isComponentType() ) { CompositeType componentType = (CompositeType) type; addAssociationsToTheSetForAllProperties( componentType.getPropertyNames(), componentType.getSubtypes(), (prefix.equals( "" ) ? name : prefix + name) + ".", factory); } }
private static NonIdentifierAttributeNature decode(Type type) { if ( type.isAssociationType() ) { AssociationType associationType = (AssociationType) type; if ( type.isComponentType() ) { // an any type is both an association and a composite... return NonIdentifierAttributeNature.ANY; } return type.isCollectionType() ? NonIdentifierAttributeNature.COLLECTION : NonIdentifierAttributeNature.ENTITY; } else { if ( type.isComponentType() ) { return NonIdentifierAttributeNature.COMPOSITE; } return NonIdentifierAttributeNature.BASIC; } }
@Override public boolean equals(Object other) { if ( this == other ) { return true; } if ( other == null || getClass() != other.getClass() ) { return false; } final TypedValue that = (TypedValue) other; return type.getReturnedClass() == that.type.getReturnedClass() && type.isEqual( that.value, value ); }
public void collectQuerySpaces(Collection<String> spaces) { for ( EntityPersister persister : alias2Persister.values() ) { Collections.addAll( spaces, (String[]) persister.getQuerySpaces() ); } for ( CollectionPersister persister : alias2CollectionPersister.values() ) { final Type elementType = persister.getElementType(); if ( elementType.isEntityType() && ! elementType.isAnyType() ) { final Joinable joinable = ( (EntityType) elementType ).getAssociatedJoinable( factory ); Collections.addAll( spaces, (String[]) ( (EntityPersister) joinable ).getQuerySpaces() ); } } } }
final EntityPersister ownerPersister = collectionPersister.getOwnerEntityPersister(); if ( ownerPersister.getIdentifierType().getReturnedClass().isInstance( key ) ) { return getEntity( session.generateEntityKey( key, collectionPersister.getOwnerEntityPersister() ) ); if ( ownerPersister.isInstance( key ) ) { final Serializable owenerId = ownerPersister.getIdentifier( key, session ); if ( owenerId == null ) { return null; final CollectionType collectionType = collectionPersister.getCollectionType(); if ( collectionType.getLHSPropertyName() != null ) { final Object owner = getEntity( new EntityUniqueKey( ownerPersister.getEntityName(), collectionType.getLHSPropertyName(), key, collectionPersister.getKeyType(), final Serializable ownerId = ownerPersister.getIdByUniqueKey( key, collectionType.getLHSPropertyName(), session ); return getEntity( session.generateEntityKey( ownerId, ownerPersister ) );
private void addParentChildEntityNameByPropertyAndValue(AbstractEntityInsertAction action, BatchIdentifier batchIdentifier, Type type, Object value) { if ( type.isEntityType() ) { final EntityType entityType = (EntityType) type; final String entityName = entityType.getName(); final String rootEntityName = action.getSession().getFactory().getMetamodel().entityPersister( entityName ).getRootEntityName(); else if ( type.isCollectionType() ) { CollectionType collectionType = (CollectionType) type; final SessionFactoryImplementor sessionFactory = ( (SessionImplementor) action.getSession() ) .getSessionFactory(); if ( collectionType.getElementType( sessionFactory ).isEntityType() && !sessionFactory.getMetamodel().collectionPersister( collectionType.getRole() ).isManyToMany() ) { String entityName = collectionType.getAssociatedEntityName( sessionFactory ); String rootEntityName = action.getSession().getFactory().getMetamodel().entityPersister( entityName ).getRootEntityName(); batchIdentifier.getChildEntityNames().add( entityName ); if ( !rootEntityName.equals( entityName ) ) { else if ( type.isComponentType() && value != null ) {
@Override public String toLoggableString(Object value, SessionFactoryImplementor factory) throws HibernateException { if ( value == null ) { return "null"; } if ( !getReturnedClass().isInstance( value ) && !PersistentCollection.class.isInstance( value ) ) { // its most likely the collection-key final CollectionPersister persister = getPersister( factory ); if ( persister.getKeyType().getReturnedClass().isInstance( value ) ) { return getRole() + "#" + getPersister( factory ).getKeyType().toLoggableString( value, factory ); } else { // although it could also be the collection-id if ( persister.getIdentifierType() != null && persister.getIdentifierType().getReturnedClass().isInstance( value ) ) { return getRole() + "#" + getPersister( factory ).getIdentifierType().toLoggableString( value, factory ); } } } return renderLoggableString( value, factory ); }
/** * Get the id value from the owning entity key, usually the same as the key, but might be some * other property, in the case of property-ref * * @param key The collection owner key * @param session The session from which the request is originating. * @return The collection owner's id, if it can be obtained from the key; * otherwise, null is returned */ public Serializable getIdOfOwnerOrNull(Serializable key, SharedSessionContractImplementor session) { Serializable ownerId = null; if ( foreignKeyPropertyName == null ) { ownerId = key; } else { Type keyType = getPersister( session ).getKeyType(); EntityPersister ownerPersister = getPersister( session ).getOwnerEntityPersister(); // TODO: Fix this so it will work for non-POJO entity mode Class ownerMappedClass = ownerPersister.getMappedClass(); if ( ownerMappedClass.isAssignableFrom( keyType.getReturnedClass() ) && keyType.getReturnedClass().isInstance( key ) ) { // the key is the owning entity itself, so get the ID from the key ownerId = ownerPersister.getIdentifier( key, session ); } else { // TODO: check if key contains the owner ID } } return ownerId; }
private boolean hasInverseCollection(ClassMetadata meta, Class<?> elementType) { SessionFactoryImplementor impl = (SessionFactoryImplementor) sessionFactory; for (Type type : meta.getPropertyTypes()) { if (type.isCollectionType()) { String role = ((CollectionType) type).getRole(); CollectionPersister persister = impl.getCollectionPersister(role); return persister.isInverse() && persister.getElementType() .getReturnedClass().equals(elementType); } } return false; }
|| type instanceof TimeType || type instanceof BinaryType || (type.isAssociationType() && !type.isCollectionType()) || type instanceof AttributeConverterTypeAdapter) else if (type.isAssociationType() && type.isCollectionType()) && type.getReturnedClass().isEnum()) registerEnum(type.getReturnedClass()); knownEmbeddedtypes.add(type.getReturnedClass().getName()); String [] names = embed.getPropertyNames(); Type [] types = embed.getSubtypes(); for (int n = 0; n < names.length; n++) registerEntityField(type.getReturnedClass(), names[n], types[n]);
final Object anything, final CollectionType type) { final CollectionPersister persister = eventSource.getFactory().getCollectionPersister( type.getRole() ); final Type elemType = persister.getElementType(); if ( elemType.isEntityType() || elemType.isAnyType() || elemType.isComponentType() ) { cascadeCollectionElements( action, elemType, anything, persister.isCascadeDeleteEnabled() );
CollectionReturn collRtn = (CollectionReturn) ownerDescriptor; String role = collRtn.getOwnerEntityName() + "." + collRtn.getOwnerProperty(); CollectionPersister persister = getFactory().getMetamodel().collectionPersister( role ); EntityType ownerType = (EntityType) persister.getElementType(); entityName = ownerType.getAssociatedEntityName( getFactory() ); Queryable persister = determineAppropriateOwnerPersister( fetchRtn.getOwner() ); Type ownerType = persister.getPropertyType( fetchRtn.getOwnerProperty() ); if ( ownerType.isEntityType() ) { entityName = ( (EntityType) ownerType ).getAssociatedEntityName( getFactory() ); else if ( ownerType.isCollectionType() ) { Type ownerCollectionElementType = ( (CollectionType) ownerType ).getElementType( getFactory() ); if ( ownerCollectionElementType.isEntityType() ) { entityName = ( (EntityType) ownerCollectionElementType ).getAssociatedEntityName( getFactory() ); return (Queryable) getFactory().getMetamodel().entityPersister( entityName );
@Override public void setIdentifier(Object entity, Serializable id, EntityMode entityMode, SharedSessionContractImplementor session) { final Object[] extractedValues = mappedIdentifierType.getPropertyValues( id, entityMode ); final Object[] injectionValues = new Object[extractedValues.length]; final PersistenceContext persistenceContext = session.getPersistenceContext(); for ( int i = 0; i < virtualIdComponent.getSubtypes().length; i++ ) { final Type virtualPropertyType = virtualIdComponent.getSubtypes()[i]; final Type idClassPropertyType = mappedIdentifierType.getSubtypes()[i]; if ( virtualPropertyType.isEntityType() && !idClassPropertyType.isEntityType() ) { if ( session == null ) { throw new AssertionError( final EntityKey entityKey = session.generateEntityKey( (Serializable) extractedValues[i], sessionFactory.getMetamodel().entityPersister( associatedEntityName ) ); if ( association == null ) { association = sessionFactory.getMetamodel().entityPersister( entityName ).getPropertyValue( entity, virtualIdComponent.getPropertyNames()[i]
private static void addIdToCollectionInfoString( CollectionPersister persister, Serializable id, SessionFactoryImplementor factory, StringBuilder s ) { // Need to use the identifier type of the collection owner // since the incoming is value is actually the owner's id. // Using the collection's key type causes problems with // property-ref keys. // Also need to check that the expected identifier type matches // the given ID. Due to property-ref keys, the collection key // may not be the owner key. Type ownerIdentifierType = persister.getOwnerEntityPersister() .getIdentifierType(); if ( id.getClass().isAssignableFrom( ownerIdentifierType.getReturnedClass() ) ) { s.append( ownerIdentifierType.toLoggableString( id, factory ) ); } else { // TODO: This is a crappy backup if a property-ref is used. // If the reference is an object w/o toString(), this isn't going to work. s.append( id.toString() ); } }
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 ); } } } }
id = entityEntry.getPersister().getPropertyValue( owner, foreignKeyPropertyName ); Type keyType = getPersister( session ).getKeyType(); Class returnedClass = keyType.getReturnedClass(); id = keyType.semiResolve( entityEntry.getLoadedValue( foreignKeyPropertyName ), session,
@Test @SuppressWarnings("unchecked") public void testPropertyAccessIgnoresStaticFields() { // verify that the entity persister is configured with property intValue as an Integer rather than // using the static field reference and determining the type to be String. assertTrue( sessionFactory() .getMetamodel() .entityPersister( FooEntity.class ) .getPropertyType( "intValue" ) .getReturnedClass() .isAssignableFrom( Integer.class ) ); } }
private boolean isNonIdentifierWhereConditionsRequired(String entityName, String propertyName, SessionImplementor session) { final Type propertyType = session.getSessionFactory().getMetamodel().entityPersister( entityName ).getPropertyType( propertyName ); if ( propertyType.isCollectionType() ) { final CollectionType collectionType = (CollectionType) propertyType; final Type collectionElementType = collectionType.getElementType( session.getSessionFactory() ); if ( collectionElementType instanceof ComponentType ) { // required for Embeddables return true; } else if ( collectionElementType instanceof MaterializedClobType || collectionElementType instanceof MaterializedNClobType ) { // for Map<> using @Lob annotations return collectionType instanceof MapType; } } return false; } }