Search in sources :

Example 11 with Select

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);
    }
}
Also used : SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) MappingException(org.hibernate.MappingException) EntityKey(org.hibernate.engine.spi.EntityKey) AssociationType(org.hibernate.type.AssociationType) JoinType(org.hibernate.sql.JoinType) CollectionType(org.hibernate.type.CollectionType) EntityType(org.hibernate.type.EntityType) ComponentType(org.hibernate.type.ComponentType) CompositeType(org.hibernate.type.CompositeType) VersionType(org.hibernate.type.VersionType) Type(org.hibernate.type.Type) SimpleSelect(org.hibernate.sql.SimpleSelect) Select(org.hibernate.sql.Select) ResultSet(java.sql.ResultSet)

Aggregations

Select (org.hibernate.sql.Select)11 SimpleSelect (org.hibernate.sql.SimpleSelect)6 JoinFragment (org.hibernate.sql.JoinFragment)4 Dialect (org.hibernate.dialect.Dialect)2 InsertSelect (org.hibernate.sql.InsertSelect)2 SelectFragment (org.hibernate.sql.SelectFragment)2 AssociationType (org.hibernate.type.AssociationType)2 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 Iterator (java.util.Iterator)1 MappingException (org.hibernate.MappingException)1 EntityKey (org.hibernate.engine.spi.EntityKey)1 OuterJoinableAssociation (org.hibernate.loader.OuterJoinableAssociation)1 JoinType (org.hibernate.sql.JoinType)1 SelectValues (org.hibernate.sql.SelectValues)1 InDatabaseValueGenerationStrategy (org.hibernate.tuple.InDatabaseValueGenerationStrategy)1 CollectionType (org.hibernate.type.CollectionType)1 ComponentType (org.hibernate.type.ComponentType)1 CompositeType (org.hibernate.type.CompositeType)1