Search in sources :

Example 81 with EntityPersister

use of org.hibernate.persister.entity.EntityPersister in project hibernate-orm by hibernate.

the class MetamodelImpl method getImplementors.

/**
	 * Given the name of an entity class, determine all the class and interface names by which it can be
	 * referenced in an HQL query.
	 *
	 * @param className The name of the entity class
	 *
	 * @return the names of all persistent (mapped) classes that extend or implement the
	 *     given class or interface, accounting for implicit/explicit polymorphism settings
	 *     and excluding mapped subclasses/joined-subclasses of other classes in the result.
	 * @throws MappingException
	 */
public String[] getImplementors(String className) throws MappingException {
    final Class clazz;
    try {
        clazz = getSessionFactory().getServiceRegistry().getService(ClassLoaderService.class).classForName(className);
    } catch (ClassLoadingException e) {
        //for a dynamic-class
        return new String[] { className };
    }
    ArrayList<String> results = new ArrayList<>();
    for (EntityPersister checkPersister : entityPersisters().values()) {
        if (!Queryable.class.isInstance(checkPersister)) {
            continue;
        }
        final Queryable checkQueryable = Queryable.class.cast(checkPersister);
        final String checkQueryableEntityName = checkQueryable.getEntityName();
        final boolean isMappedClass = className.equals(checkQueryableEntityName);
        if (checkQueryable.isExplicitPolymorphism()) {
            if (isMappedClass) {
                //NOTE EARLY EXIT
                return new String[] { className };
            }
        } else {
            if (isMappedClass) {
                results.add(checkQueryableEntityName);
            } else {
                final Class mappedClass = checkQueryable.getMappedClass();
                if (mappedClass != null && clazz.isAssignableFrom(mappedClass)) {
                    final boolean assignableSuperclass;
                    if (checkQueryable.isInherited()) {
                        Class mappedSuperclass = entityPersister(checkQueryable.getMappedSuperclass()).getMappedClass();
                        assignableSuperclass = clazz.isAssignableFrom(mappedSuperclass);
                    } else {
                        assignableSuperclass = false;
                    }
                    if (!assignableSuperclass) {
                        results.add(checkQueryableEntityName);
                    }
                }
            }
        }
    }
    return results.toArray(new String[results.size()]);
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) ClassLoadingException(org.hibernate.boot.registry.classloading.spi.ClassLoadingException) Queryable(org.hibernate.persister.entity.Queryable) ArrayList(java.util.ArrayList) PersistentClass(org.hibernate.mapping.PersistentClass)

Example 82 with EntityPersister

use of org.hibernate.persister.entity.EntityPersister in project hibernate-orm by hibernate.

the class LoadQueryJoinAndFetchProcessor method renderManyToManyJoin.

private void renderManyToManyJoin(Join join, JoinFragment joinFragment) {
    // for many-to-many we have 3 table aliases.  By way of example, consider a normal m-n: User<->Role
    // where User is the FetchOwner and Role (User.roles) is the Fetch.  We'd have:
    //		1) the owner's table : user - in terms of rendering the joins (not the fetch select fragments), the
    // 			lhs table alias is only needed to qualify the lhs join columns, but we already have the qualified
    // 			columns here (aliasedLhsColumnNames)
    //final String ownerTableAlias = ...;
    //		2) the m-n table : user_role
    //		3) the element table : role
    final EntityPersister entityPersister = ((EntityQuerySpace) join.getRightHandSide()).getEntityPersister();
    final String entityTableAlias = aliasResolutionContext.resolveSqlTableAliasFromQuerySpaceUid(join.getRightHandSide().getUid());
    if (StringHelper.isEmpty(entityTableAlias)) {
        throw new IllegalStateException("Collection element (many-to-many) table alias cannot be empty");
    }
    final String manyToManyFilter;
    if (JoinDefinedByMetadata.class.isInstance(join) && CollectionPropertyNames.COLLECTION_ELEMENTS.equals(((JoinDefinedByMetadata) join).getJoinedPropertyName())) {
        final CollectionQuerySpace leftHandSide = (CollectionQuerySpace) join.getLeftHandSide();
        final CollectionPersister persister = leftHandSide.getCollectionPersister();
        manyToManyFilter = persister.getManyToManyFilterFragment(entityTableAlias, buildingParameters.getQueryInfluencers().getEnabledFilters());
    } else {
        manyToManyFilter = null;
    }
    addJoins(join, joinFragment, (Joinable) entityPersister, manyToManyFilter);
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) CollectionPersister(org.hibernate.persister.collection.CollectionPersister) JoinDefinedByMetadata(org.hibernate.loader.plan.spi.JoinDefinedByMetadata) EntityQuerySpace(org.hibernate.loader.plan.spi.EntityQuerySpace) CollectionQuerySpace(org.hibernate.loader.plan.spi.CollectionQuerySpace)

Example 83 with EntityPersister

use of org.hibernate.persister.entity.EntityPersister in project hibernate-orm by hibernate.

the class Util method resolveResultClasses.

/**
	 * Resolve the given result classes
	 *
	 * @param context The context for the resolution.  See {@link ResultSetMappingResolutionContext}
	 * @param resultClasses The Classes to which the results should be mapped
	 */
public static void resolveResultClasses(ResultClassesResolutionContext context, Class... resultClasses) {
    int i = 0;
    for (Class resultClass : resultClasses) {
        context.addQueryReturns(new NativeSQLQueryRootReturn("alias" + (++i), resultClass.getName(), LockMode.READ));
        try {
            final EntityPersister persister = context.getSessionFactory().getEntityPersister(resultClass.getName());
            context.addQuerySpaces((String[]) persister.getQuerySpaces());
        } catch (Exception ignore) {
        }
    }
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) NativeSQLQueryRootReturn(org.hibernate.engine.query.spi.sql.NativeSQLQueryRootReturn) UnknownSqlResultSetMappingException(org.hibernate.procedure.UnknownSqlResultSetMappingException)

Example 84 with EntityPersister

use of org.hibernate.persister.entity.EntityPersister in project hibernate-orm by hibernate.

the class EntityType method isEqual.

@Override
public boolean isEqual(Object x, Object y, SessionFactoryImplementor factory) {
    // associations (many-to-one and one-to-one) can be null...
    if (x == null || y == null) {
        return x == y;
    }
    EntityPersister persister = getAssociatedEntityPersister(factory);
    if (!persister.canExtractIdOutOfEntity()) {
        return super.isEqual(x, y);
    }
    final Class mappedClass = persister.getMappedClass();
    Serializable xid;
    if (x instanceof HibernateProxy) {
        xid = ((HibernateProxy) x).getHibernateLazyInitializer().getIdentifier();
    } else {
        if (mappedClass.isAssignableFrom(x.getClass())) {
            xid = persister.getIdentifier(x);
        } else {
            //JPA 2 case where @IdClass contains the id and not the associated entity
            xid = (Serializable) x;
        }
    }
    Serializable yid;
    if (y instanceof HibernateProxy) {
        yid = ((HibernateProxy) y).getHibernateLazyInitializer().getIdentifier();
    } else {
        if (mappedClass.isAssignableFrom(y.getClass())) {
            yid = persister.getIdentifier(y);
        } else {
            //JPA 2 case where @IdClass contains the id and not the associated entity
            yid = (Serializable) y;
        }
    }
    return persister.getIdentifierType().isEqual(xid, yid, factory);
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) Serializable(java.io.Serializable) HibernateProxy(org.hibernate.proxy.HibernateProxy)

Example 85 with EntityPersister

use of org.hibernate.persister.entity.EntityPersister in project hibernate-orm by hibernate.

the class OneToOneType method isNull.

@Override
public boolean isNull(Object owner, SharedSessionContractImplementor session) {
    if (propertyName != null) {
        final EntityPersister ownerPersister = session.getFactory().getMetamodel().entityPersister(entityName);
        final Serializable id = session.getContextEntityIdentifier(owner);
        final EntityKey entityKey = session.generateEntityKey(id, ownerPersister);
        return session.getPersistenceContext().isPropertyNull(entityKey, getPropertyName());
    } else {
        return false;
    }
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) EntityKey(org.hibernate.engine.spi.EntityKey) Serializable(java.io.Serializable)

Aggregations

EntityPersister (org.hibernate.persister.entity.EntityPersister)166 Test (org.junit.Test)59 Serializable (java.io.Serializable)34 Session (org.hibernate.Session)31 Type (org.hibernate.type.Type)29 EntityEntry (org.hibernate.engine.spi.EntityEntry)21 EventSource (org.hibernate.event.spi.EventSource)15 EntityRegionAccessStrategy (org.hibernate.cache.spi.access.EntityRegionAccessStrategy)13 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)13 TestForIssue (org.hibernate.testing.TestForIssue)13 SessionImplementor (org.hibernate.engine.spi.SessionImplementor)12 SequenceStyleGenerator (org.hibernate.id.enhanced.SequenceStyleGenerator)12 EntityType (org.hibernate.type.EntityType)12 ArrayList (java.util.ArrayList)11 CompositeType (org.hibernate.type.CompositeType)11 HibernateProxy (org.hibernate.proxy.HibernateProxy)10 PreparedStatement (java.sql.PreparedStatement)8 List (java.util.List)8 EntityKey (org.hibernate.engine.spi.EntityKey)8 QueryParameters (org.hibernate.engine.spi.QueryParameters)8