use of org.hibernate.sql.Select in project hibernate-orm by hibernate.
the class AbstractEntityPersister method getNaturalIdentifierSnapshot.
public Object[] getNaturalIdentifierSnapshot(Serializable id, SharedSessionContractImplementor session) throws HibernateException {
if (!hasNaturalIdentifier()) {
throw new MappingException("persistent class did not define a natural-id : " + MessageHelper.infoString(this));
}
if (LOG.isTraceEnabled()) {
LOG.tracev("Getting current natural-id snapshot state for: {0}", MessageHelper.infoString(this, id, getFactory()));
}
int[] naturalIdPropertyIndexes = getNaturalIdentifierProperties();
int naturalIdPropertyCount = naturalIdPropertyIndexes.length;
boolean[] naturalIdMarkers = new boolean[getPropertySpan()];
Type[] extractionTypes = new Type[naturalIdPropertyCount];
for (int i = 0; i < naturalIdPropertyCount; i++) {
extractionTypes[i] = getPropertyTypes()[naturalIdPropertyIndexes[i]];
naturalIdMarkers[naturalIdPropertyIndexes[i]] = true;
}
///////////////////////////////////////////////////////////////////////
// TODO : look at perhaps caching this...
Select select = new Select(getFactory().getDialect());
if (getFactory().getSessionFactoryOptions().isCommentsEnabled()) {
select.setComment("get current natural-id state " + getEntityName());
}
select.setSelectClause(concretePropertySelectFragmentSansLeadingComma(getRootAlias(), naturalIdMarkers));
select.setFromClause(fromTableFragment(getRootAlias()) + fromJoinFragment(getRootAlias(), true, false));
String[] aliasedIdColumns = StringHelper.qualify(getRootAlias(), getIdentifierColumnNames());
String whereClause = new StringBuilder().append(StringHelper.join("=? and ", aliasedIdColumns)).append("=?").append(whereJoinFragment(getRootAlias(), true, false)).toString();
String sql = select.setOuterJoins("", "").setWhereClause(whereClause).toStatementString();
///////////////////////////////////////////////////////////////////////
Object[] snapshot = new Object[naturalIdPropertyCount];
try {
PreparedStatement ps = session.getJdbcCoordinator().getStatementPreparer().prepareStatement(sql);
try {
getIdentifierType().nullSafeSet(ps, id, 1, session);
ResultSet rs = session.getJdbcCoordinator().getResultSetReturn().extract(ps);
try {
//if there is no resulting row, return null
if (!rs.next()) {
return null;
}
final EntityKey key = session.generateEntityKey(id, this);
Object owner = session.getPersistenceContext().getEntity(key);
for (int i = 0; i < naturalIdPropertyCount; i++) {
snapshot[i] = extractionTypes[i].hydrate(rs, getPropertyAliases("", naturalIdPropertyIndexes[i]), session, null);
if (extractionTypes[i].isEntityType()) {
snapshot[i] = extractionTypes[i].resolve(snapshot[i], session, owner);
}
}
return snapshot;
} finally {
session.getJdbcCoordinator().getResourceRegistry().release(rs, ps);
}
} finally {
session.getJdbcCoordinator().getResourceRegistry().release(ps);
session.getJdbcCoordinator().afterStatementExecution();
}
} catch (SQLException e) {
throw getFactory().getSQLExceptionHelper().convert(e, "could not retrieve snapshot: " + MessageHelper.infoString(this, id, getFactory()), sql);
}
}
Aggregations