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()]);
}
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);
}
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) {
}
}
}
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);
}
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;
}
}
Aggregations