use of org.hibernate.persister.entity.Loadable in project hibernate-orm by hibernate.
the class BasicLoader method postInstantiate.
protected void postInstantiate() {
Loadable[] persisters = getEntityPersisters();
String[] suffixes = getSuffixes();
descriptors = new EntityAliases[persisters.length];
for (int i = 0; i < descriptors.length; i++) {
descriptors[i] = new DefaultEntityAliases(persisters[i], suffixes[i]);
}
CollectionPersister[] collectionPersisters = getCollectionPersisters();
List bagRoles = null;
if (collectionPersisters != null) {
String[] collectionSuffixes = getCollectionSuffixes();
collectionDescriptors = new CollectionAliases[collectionPersisters.length];
for (int i = 0; i < collectionPersisters.length; i++) {
if (isBag(collectionPersisters[i])) {
if (bagRoles == null) {
bagRoles = new ArrayList();
}
bagRoles.add(collectionPersisters[i].getRole());
}
collectionDescriptors[i] = new GeneratedCollectionAliases(collectionPersisters[i], collectionSuffixes[i]);
}
} else {
collectionDescriptors = null;
}
if (bagRoles != null && bagRoles.size() > 1) {
throw new MultipleBagFetchException(bagRoles);
}
}
use of org.hibernate.persister.entity.Loadable in project hibernate-orm by hibernate.
the class Loader method shouldUseFollowOnLocking.
protected boolean shouldUseFollowOnLocking(QueryParameters parameters, Dialect dialect, List<AfterLoadAction> afterLoadActions) {
if ((parameters.getLockOptions().getFollowOnLocking() == null && dialect.useFollowOnLocking(parameters)) || (parameters.getLockOptions().getFollowOnLocking() != null && parameters.getLockOptions().getFollowOnLocking())) {
// currently only one lock mode is allowed in follow-on locking
final LockMode lockMode = determineFollowOnLockMode(parameters.getLockOptions());
final LockOptions lockOptions = new LockOptions(lockMode);
if (lockOptions.getLockMode() != LockMode.UPGRADE_SKIPLOCKED) {
if (lockOptions.getLockMode() != LockMode.NONE) {
LOG.usingFollowOnLocking();
}
lockOptions.setTimeOut(parameters.getLockOptions().getTimeOut());
lockOptions.setScope(parameters.getLockOptions().getScope());
afterLoadActions.add(new AfterLoadAction() {
@Override
public void afterLoad(SharedSessionContractImplementor session, Object entity, Loadable persister) {
((Session) session).buildLockRequest(lockOptions).lock(persister.getEntityName(), entity);
}
});
parameters.setLockOptions(new LockOptions());
return true;
}
}
return false;
}
use of org.hibernate.persister.entity.Loadable 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.persister.entity.Loadable in project hibernate-orm by hibernate.
the class Loader method createSubselects.
private void createSubselects(List keys, QueryParameters queryParameters, SharedSessionContractImplementor session) {
if (keys.size() > 1) {
//if we only returned one entity, query by key is more efficient
Set[] keySets = transpose(keys);
Map namedParameterLocMap = buildNamedParameterLocMap(queryParameters);
final Loadable[] loadables = getEntityPersisters();
final String[] aliases = getAliases();
final String subselectQueryString = SubselectFetch.createSubselectFetchQueryFragment(queryParameters);
for (Object key : keys) {
final EntityKey[] rowKeys = (EntityKey[]) key;
for (int i = 0; i < rowKeys.length; i++) {
if (rowKeys[i] != null && loadables[i].hasSubselectLoadableCollections()) {
SubselectFetch subselectFetch = new SubselectFetch(subselectQueryString, aliases[i], loadables[i], queryParameters, keySets[i], namedParameterLocMap);
session.getPersistenceContext().getBatchFetchQueue().addSubselect(rowKeys[i], subselectFetch);
}
}
}
}
}
use of org.hibernate.persister.entity.Loadable in project hibernate-orm by hibernate.
the class CriteriaLoader method applyLocks.
@Override
protected String applyLocks(String sql, QueryParameters parameters, Dialect dialect, List<AfterLoadAction> afterLoadActions) throws QueryException {
final LockOptions lockOptions = parameters.getLockOptions();
if (lockOptions == null || (lockOptions.getLockMode() == LockMode.NONE && (lockOptions.getAliasLockCount() == 0 || (lockOptions.getAliasLockCount() == 1 && lockOptions.getAliasSpecificLockMode("this_") == LockMode.NONE)))) {
return sql;
}
if ((parameters.getLockOptions().getFollowOnLocking() == null && dialect.useFollowOnLocking(parameters)) || (parameters.getLockOptions().getFollowOnLocking() != null && parameters.getLockOptions().getFollowOnLocking())) {
final LockMode lockMode = determineFollowOnLockMode(lockOptions);
if (lockMode != LockMode.UPGRADE_SKIPLOCKED) {
// Dialect prefers to perform locking in a separate step
LOG.usingFollowOnLocking();
final LockOptions lockOptionsToUse = new LockOptions(lockMode);
lockOptionsToUse.setTimeOut(lockOptions.getTimeOut());
lockOptionsToUse.setScope(lockOptions.getScope());
afterLoadActions.add(new AfterLoadAction() {
@Override
public void afterLoad(SharedSessionContractImplementor session, Object entity, Loadable persister) {
((Session) session).buildLockRequest(lockOptionsToUse).lock(persister.getEntityName(), entity);
}
});
parameters.setLockOptions(new LockOptions());
return sql;
}
}
final LockOptions locks = new LockOptions(lockOptions.getLockMode());
locks.setScope(lockOptions.getScope());
locks.setTimeOut(lockOptions.getTimeOut());
final Map<String, String[]> keyColumnNames = dialect.forUpdateOfColumns() ? new HashMap() : null;
final String[] drivingSqlAliases = getAliases();
for (int i = 0; i < drivingSqlAliases.length; i++) {
final LockMode lockMode = lockOptions.getAliasSpecificLockMode(drivingSqlAliases[i]);
if (lockMode != null) {
final Lockable drivingPersister = (Lockable) getEntityPersisters()[i];
final String rootSqlAlias = drivingPersister.getRootTableAlias(drivingSqlAliases[i]);
locks.setAliasSpecificLockMode(rootSqlAlias, lockMode);
if (keyColumnNames != null) {
keyColumnNames.put(rootSqlAlias, drivingPersister.getRootTableIdentifierColumnNames());
}
}
}
return dialect.applyLocksToSql(sql, locks, keyColumnNames);
}
Aggregations