use of org.hibernate.type.AssociationType in project hibernate-orm by hibernate.
the class BatchFetchStrategyHelperTest method testManyToOneDefaultFetch.
@Test
public void testManyToOneDefaultFetch() {
final AssociationType associationType = determineAssociationType(AnEntity.class, "otherEntityDefault");
final org.hibernate.FetchMode fetchMode = determineFetchMode(AnEntity.class, "otherEntityDefault");
assertSame(org.hibernate.FetchMode.JOIN, fetchMode);
final FetchStyle fetchStyle = FetchStrategyHelper.determineFetchStyleByMetadata(fetchMode, associationType, sessionFactory());
// batch size is ignored with org.hibernate.FetchMode.JOIN
assertSame(FetchStyle.JOIN, fetchStyle);
final FetchTiming fetchTiming = FetchStrategyHelper.determineFetchTiming(fetchStyle, associationType, sessionFactory());
assertSame(FetchTiming.IMMEDIATE, fetchTiming);
}
use of org.hibernate.type.AssociationType in project yyl_example by Relucent.
the class HibernateTest method test3.
private static void test3(SessionFactory sessionFactory) {
System.out.println("--------------------------------------------------");
ClassMetadata metadata = sessionFactory.getClassMetadata(Table1.class);
System.out.println("EntityName: " + metadata.getMappedClass());
System.out.println("IdProperty: " + metadata.getIdentifierPropertyName());
for (String propertyName : metadata.getPropertyNames()) {
Type type = metadata.getPropertyType(propertyName);
System.out.println("Property: " + propertyName);
System.out.println(" isComponentType: " + type.isComponentType());
System.out.println(" isAnyType: " + type.isAnyType());
System.out.println(" isCollection: " + type.isCollectionType());
System.out.println(" isAssociationType: " + type.isAssociationType());
if (type instanceof AssociationType) {
AssociationType associationType = (AssociationType) type;
String associatedEntityName = associationType.getAssociatedEntityName((SessionFactoryImpl) sessionFactory);
ClassMetadata associatedClassMetadata = sessionFactory.getClassMetadata(associatedEntityName);
Class<?> mappedClass = associatedClassMetadata.getMappedClass();
System.out.println(" associatedClass: " + mappedClass);
} else {
System.out.println(" propertyClass: " + type.getReturnedClass());
}
System.out.println();
}
}
use of org.hibernate.type.AssociationType 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.type.AssociationType in project hibernate-orm by hibernate.
the class Loader method loadFromResultSet.
/**
* Hydrate the state an object from the SQL <tt>ResultSet</tt>, into
* an array or "hydrated" values (do not resolve associations yet),
* and pass the hydrates state to the session.
*/
private void loadFromResultSet(final ResultSet rs, final int i, final Object object, final String instanceEntityName, final EntityKey key, final String rowIdAlias, final LockMode lockMode, final Loadable rootPersister, final SharedSessionContractImplementor session) throws SQLException, HibernateException {
final Serializable id = key.getIdentifier();
// Get the persister for the _subclass_
final Loadable persister = (Loadable) getFactory().getEntityPersister(instanceEntityName);
if (LOG.isTraceEnabled()) {
LOG.tracef("Initializing object from ResultSet: %s", MessageHelper.infoString(persister, id, getFactory()));
}
boolean fetchAllPropertiesRequested = isEagerPropertyFetchEnabled(i);
// add temp entry so that the next step is circular-reference
// safe - only needed because some types don't take proper
// advantage of two-phase-load (esp. components)
TwoPhaseLoad.addUninitializedEntity(key, object, persister, lockMode, session);
//This is not very nice (and quite slow):
final String[][] cols = persister == rootPersister ? getEntityAliases()[i].getSuffixedPropertyAliases() : getEntityAliases()[i].getSuffixedPropertyAliases(persister);
final Object[] values = persister.hydrate(rs, id, object, rootPersister, cols, fetchAllPropertiesRequested, session);
final Object rowId = persister.hasRowId() ? rs.getObject(rowIdAlias) : null;
final AssociationType[] ownerAssociationTypes = getOwnerAssociationTypes();
if (ownerAssociationTypes != null && ownerAssociationTypes[i] != null) {
String ukName = ownerAssociationTypes[i].getRHSUniqueKeyPropertyName();
if (ukName != null) {
final int index = ((UniqueKeyLoadable) persister).getPropertyIndex(ukName);
final Type type = persister.getPropertyTypes()[index];
// polymorphism not really handled completely correctly,
// perhaps...well, actually its ok, assuming that the
// entity name used in the lookup is the same as the
// the one used here, which it will be
EntityUniqueKey euk = new EntityUniqueKey(//polymorphism comment above
rootPersister.getEntityName(), ukName, type.semiResolve(values[index], session, object), type, persister.getEntityMode(), session.getFactory());
session.getPersistenceContext().addEntity(euk, object);
}
}
TwoPhaseLoad.postHydrate(persister, id, values, rowId, object, lockMode, session);
}
use of org.hibernate.type.AssociationType in project hibernate-orm by hibernate.
the class JoinWalker method walkCollectionTree.
/**
* For a collection role, return a list of associations to be fetched by outerjoin
*/
private void walkCollectionTree(final QueryableCollection persister, final String alias, final PropertyPath path, final int currentDepth) throws MappingException {
if (persister.isOneToMany()) {
walkEntityTree((OuterJoinLoadable) persister.getElementPersister(), alias, path, currentDepth);
} else {
Type type = persister.getElementType();
if (type.isAssociationType()) {
// a many-to-many;
// decrement currentDepth here to allow join across the association table
// without exceeding MAX_FETCH_DEPTH (i.e. the "currentDepth - 1" bit)
AssociationType associationType = (AssociationType) type;
String[] aliasedLhsColumns = persister.getElementColumnNames(alias);
String[] lhsColumns = persister.getElementColumnNames();
// if the current depth is 0, the root thing being loaded is the
// many-to-many collection itself. Here, it is alright to use
// an inner join...
boolean useInnerJoin = currentDepth == 0;
final JoinType joinType = getJoinType(associationType, persister.getFetchMode(), path, persister.getTableName(), lhsColumns, !useInnerJoin, currentDepth - 1, //operations which cascade as far as the collection also cascade to collection elements
null);
addAssociationToJoinTreeIfNecessary(associationType, aliasedLhsColumns, alias, path, currentDepth - 1, joinType);
} else if (type.isComponentType()) {
walkCompositeElementTree((CompositeType) type, persister.getElementColumnNames(), persister, alias, path, currentDepth);
}
}
}
Aggregations