use of org.hibernate.persister.entity.Joinable in project hibernate-orm by hibernate.
the class AbstractLoadPlanBuildingAssociationVisitationStrategy method startingEntity.
// Entities ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@Override
public void startingEntity(EntityDefinition entityDefinition) {
// see if the EntityDefinition is a root...
final boolean isRoot = fetchSourceStack.isEmpty();
if (!isRoot) {
// if not, this call should represent a fetch which should have been handled in #startingAttribute
return;
}
// if we get here, it is a root
log.tracef("%s Starting root entity : %s", StringHelper.repeat(">>", fetchSourceStack.size()), entityDefinition.getEntityPersister().getEntityName());
if (!supportsRootEntityReturns()) {
throw new HibernateException("This strategy does not support root entity returns");
}
final EntityReturnImpl entityReturn = new EntityReturnImpl(entityDefinition, querySpaces);
addRootReturn(entityReturn);
pushToStack(entityReturn);
// also add an AssociationKey for the root so we can later on recognize circular references back to the root.
final Joinable entityPersister = (Joinable) entityDefinition.getEntityPersister();
associationKeyRegistered(new AssociationKey(entityPersister.getTableName(), entityPersister.getKeyColumnNames()));
}
use of org.hibernate.persister.entity.Joinable in project hibernate-orm by hibernate.
the class LoadQueryJoinAndFetchProcessor method renderEntityJoin.
private void renderEntityJoin(Join join, JoinFragment joinFragment) {
final EntityQuerySpace rightHandSide = (EntityQuerySpace) join.getRightHandSide();
// see if there is already aliases registered for this entity query space (collection joins)
EntityReferenceAliases aliases = aliasResolutionContext.resolveEntityReferenceAliases(rightHandSide.getUid());
if (aliases == null) {
aliasResolutionContext.generateEntityReferenceAliases(rightHandSide.getUid(), rightHandSide.getEntityPersister());
}
final Joinable joinable = (Joinable) rightHandSide.getEntityPersister();
addJoins(join, joinFragment, joinable, null);
}
use of org.hibernate.persister.entity.Joinable in project hibernate-orm by hibernate.
the class EntityBasedAssociationAttribute method getAssociationKey.
@Override
public AssociationKey getAssociationKey() {
final AssociationType type = getType();
if (type.isAnyType()) {
return new AssociationKey(JoinHelper.getLHSTableName(type, attributeNumber(), (OuterJoinLoadable) getSource()), JoinHelper.getLHSColumnNames(type, attributeNumber(), 0, (OuterJoinLoadable) getSource(), sessionFactory()));
}
final Joinable joinable = type.getAssociatedJoinable(sessionFactory());
if (type.getForeignKeyDirection() == ForeignKeyDirection.FROM_PARENT) {
final String lhsTableName;
final String[] lhsColumnNames;
if (joinable.isCollection()) {
final QueryableCollection collectionPersister = (QueryableCollection) joinable;
lhsTableName = collectionPersister.getTableName();
lhsColumnNames = collectionPersister.getElementColumnNames();
} else {
final OuterJoinLoadable entityPersister = (OuterJoinLoadable) source();
lhsTableName = getLHSTableName(type, attributeNumber(), entityPersister);
lhsColumnNames = getLHSColumnNames(type, attributeNumber(), entityPersister, sessionFactory());
}
return new AssociationKey(lhsTableName, lhsColumnNames);
} else {
return new AssociationKey(joinable.getTableName(), getRHSColumnNames(type, sessionFactory()));
}
}
use of org.hibernate.persister.entity.Joinable in project hibernate-orm by hibernate.
the class AttributeNodeImpl method internalMakeSubgraph.
@SuppressWarnings("unchecked")
private <X> SubgraphImpl<X> internalMakeSubgraph(Class<X> type) {
if (attribute.getPersistentAttributeType() == Attribute.PersistentAttributeType.BASIC || attribute.getPersistentAttributeType() == Attribute.PersistentAttributeType.EMBEDDED) {
throw new IllegalArgumentException(String.format("Attribute [%s] is not of managed type", getAttributeName()));
}
if (attribute.getPersistentAttributeType() == Attribute.PersistentAttributeType.ELEMENT_COLLECTION) {
throw new IllegalArgumentException(String.format("Collection elements [%s] is not of managed type", getAttributeName()));
}
if (subgraphMap == null) {
subgraphMap = new HashMap<>();
}
final Helper.AttributeSource attributeSource = Helper.resolveAttributeSource(sessionFactory(), managedType);
final AssociationType attributeType = (AssociationType) attributeSource.findType(attribute.getName());
final Joinable joinable = attributeType.getAssociatedJoinable(sessionFactory());
if (joinable.isCollection()) {
final EntityPersister elementEntityPersister = ((QueryableCollection) joinable).getElementPersister();
if (type == null) {
type = elementEntityPersister.getMappedClass();
} else {
if (!isTreatableAs(elementEntityPersister, type)) {
throw new IllegalArgumentException(String.format("Collection elements [%s] cannot be treated as requested type [%s] : %s", getAttributeName(), type.getName(), elementEntityPersister.getMappedClass().getName()));
}
}
} else {
final EntityPersister entityPersister = (EntityPersister) joinable;
if (type == null) {
type = entityPersister.getMappedClass();
} else {
if (!isTreatableAs(entityPersister, type)) {
throw new IllegalArgumentException(String.format("Attribute [%s] cannot be treated as requested type [%s] : %s", getAttributeName(), type.getName(), entityPersister.getMappedClass().getName()));
}
}
}
ManagedType managedType = null;
try {
managedType = sessionFactory.getMetamodel().entity(type.getName());
} catch (IllegalArgumentException e) {
// do nothing
}
if (managedType == null) {
managedType = attribute.getDeclaringType();
}
final SubgraphImpl<X> subgraph = new SubgraphImpl<>(this.sessionFactory, managedType, type);
subgraphMap.put(type, subgraph);
return subgraph;
}
use of org.hibernate.persister.entity.Joinable in project dhis2-core by dhis2.
the class AbstractPropertyIntrospectorService method updateJoinTables.
protected void updateJoinTables() {
if (!roleToRole.isEmpty()) {
return;
}
Map<String, List<String>> joinTableToRoles = new HashMap<>();
SessionFactoryImplementor sessionFactoryImplementor = (SessionFactoryImplementor) sessionFactory;
Iterator<?> collectionIterator = sessionFactory.getAllCollectionMetadata().values().iterator();
while (collectionIterator.hasNext()) {
CollectionPersister collectionPersister = (CollectionPersister) collectionIterator.next();
CollectionType collectionType = collectionPersister.getCollectionType();
if (collectionPersister.isManyToMany() && collectionType.isAssociationType()) {
Joinable associatedJoinable = collectionType.getAssociatedJoinable(sessionFactoryImplementor);
if (!joinTableToRoles.containsKey(associatedJoinable.getTableName())) {
joinTableToRoles.put(associatedJoinable.getTableName(), new ArrayList<>());
}
joinTableToRoles.get(associatedJoinable.getTableName()).add(collectionPersister.getRole());
} else if (collectionPersister.isInverse()) {
if (SetType.class.isInstance(collectionType)) {
SetType setType = (SetType) collectionType;
setType.getAssociatedJoinable(sessionFactoryImplementor);
}
}
}
Iterator<Map.Entry<String, List<String>>> entryIterator = joinTableToRoles.entrySet().iterator();
while (entryIterator.hasNext()) {
Map.Entry<String, List<String>> entry = entryIterator.next();
if (entry.getValue().size() < 2) {
entryIterator.remove();
}
}
for (Map.Entry<String, List<String>> entry : joinTableToRoles.entrySet()) {
roleToRole.put(entry.getValue().get(0), entry.getValue().get(1));
roleToRole.put(entry.getValue().get(1), entry.getValue().get(0));
}
}
Aggregations