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);
}
use of org.hibernate.persister.entity.Loadable in project hibernate-orm by hibernate.
the class SizeExpression method toSqlString.
@Override
public String toSqlString(Criteria criteria, CriteriaQuery criteriaQuery) throws HibernateException {
final String entityName = criteriaQuery.getEntityName(criteria, propertyName);
final String role = entityName + '.' + criteriaQuery.getPropertyName(propertyName);
final QueryableCollection cp = (QueryableCollection) criteriaQuery.getFactory().getCollectionPersister(role);
final String[] fk = cp.getKeyColumnNames();
final String[] pk = ((Loadable) cp.getOwnerEntityPersister()).getIdentifierColumnNames();
final ConditionFragment subQueryRestriction = new ConditionFragment().setTableAlias(criteriaQuery.getSQLAlias(criteria, propertyName)).setCondition(pk, fk);
return String.format(Locale.ROOT, "? %s (select count(*) from %s where %s)", op, cp.getTableName(), subQueryRestriction.toFragmentString());
}
use of org.hibernate.persister.entity.Loadable in project hibernate-orm by hibernate.
the class AbstractEmptinessExpression method toSqlString.
@Override
public final String toSqlString(Criteria criteria, CriteriaQuery criteriaQuery) throws HibernateException {
final String entityName = criteriaQuery.getEntityName(criteria, propertyName);
final String actualPropertyName = criteriaQuery.getPropertyName(propertyName);
final String sqlAlias = criteriaQuery.getSQLAlias(criteria, propertyName);
final SessionFactoryImplementor factory = criteriaQuery.getFactory();
final QueryableCollection collectionPersister = getQueryableCollection(entityName, actualPropertyName, factory);
final String[] collectionKeys = collectionPersister.getKeyColumnNames();
final String[] ownerKeys = ((Loadable) factory.getEntityPersister(entityName)).getIdentifierColumnNames();
final String innerSelect = "(select 1 from " + collectionPersister.getTableName() + " where " + new ConditionFragment().setTableAlias(sqlAlias).setCondition(ownerKeys, collectionKeys).toFragmentString() + ")";
return excludeEmpty() ? "exists " + innerSelect : "not exists " + innerSelect;
}
Aggregations