001: //$Id: AbstractSessionImpl.java 10018 2006-06-15 05:21:06Z steve.ebersole@jboss.com $
002: package org.hibernate.impl;
003:
004: import org.hibernate.MappingException;
005: import org.hibernate.Query;
006: import org.hibernate.SQLQuery;
007: import org.hibernate.HibernateException;
008: import org.hibernate.ScrollableResults;
009: import org.hibernate.SessionException;
010: import org.hibernate.engine.query.sql.NativeSQLQuerySpecification;
011: import org.hibernate.engine.NamedQueryDefinition;
012: import org.hibernate.engine.NamedSQLQueryDefinition;
013: import org.hibernate.engine.SessionImplementor;
014: import org.hibernate.engine.QueryParameters;
015: import org.hibernate.engine.SessionFactoryImplementor;
016: import org.hibernate.engine.query.HQLQueryPlan;
017: import org.hibernate.engine.query.NativeSQLQueryPlan;
018:
019: import java.util.List;
020:
021: /**
022: * Functionality common to stateless and stateful sessions
023: *
024: * @author Gavin King
025: */
026: public abstract class AbstractSessionImpl implements SessionImplementor {
027:
028: protected transient SessionFactoryImpl factory;
029: private boolean closed = false;
030:
031: protected AbstractSessionImpl(SessionFactoryImpl factory) {
032: this .factory = factory;
033: }
034:
035: public SessionFactoryImplementor getFactory() {
036: return factory;
037: }
038:
039: public boolean isClosed() {
040: return closed;
041: }
042:
043: protected void setClosed() {
044: closed = true;
045: }
046:
047: protected void errorIfClosed() {
048: if (closed) {
049: throw new SessionException("Session is closed!");
050: }
051: }
052:
053: public Query getNamedQuery(String queryName)
054: throws MappingException {
055: errorIfClosed();
056: NamedQueryDefinition nqd = factory.getNamedQuery(queryName);
057: final Query query;
058: if (nqd != null) {
059: String queryString = nqd.getQueryString();
060: query = new QueryImpl(queryString, nqd.getFlushMode(),
061: this , getHQLQueryPlan(queryString, false)
062: .getParameterMetadata());
063: query.setComment("named HQL query " + queryName);
064: } else {
065: NamedSQLQueryDefinition nsqlqd = factory
066: .getNamedSQLQuery(queryName);
067: if (nsqlqd == null) {
068: throw new MappingException("Named query not known: "
069: + queryName);
070: }
071: query = new SQLQueryImpl(nsqlqd, this , factory
072: .getQueryPlanCache().getSQLParameterMetadata(
073: nsqlqd.getQueryString()));
074: query.setComment("named native SQL query " + queryName);
075: nqd = nsqlqd;
076: }
077: initQuery(query, nqd);
078: return query;
079: }
080:
081: public Query getNamedSQLQuery(String queryName)
082: throws MappingException {
083: errorIfClosed();
084: NamedSQLQueryDefinition nsqlqd = factory
085: .getNamedSQLQuery(queryName);
086: if (nsqlqd == null) {
087: throw new MappingException("Named SQL query not known: "
088: + queryName);
089: }
090: Query query = new SQLQueryImpl(nsqlqd, this , factory
091: .getQueryPlanCache().getSQLParameterMetadata(
092: nsqlqd.getQueryString()));
093: query.setComment("named native SQL query " + queryName);
094: initQuery(query, nsqlqd);
095: return query;
096: }
097:
098: private void initQuery(Query query, NamedQueryDefinition nqd) {
099: query.setCacheable(nqd.isCacheable());
100: query.setCacheRegion(nqd.getCacheRegion());
101: if (nqd.getTimeout() != null)
102: query.setTimeout(nqd.getTimeout().intValue());
103: if (nqd.getFetchSize() != null)
104: query.setFetchSize(nqd.getFetchSize().intValue());
105: if (nqd.getCacheMode() != null)
106: query.setCacheMode(nqd.getCacheMode());
107: query.setReadOnly(nqd.isReadOnly());
108: if (nqd.getComment() != null)
109: query.setComment(nqd.getComment());
110: }
111:
112: public Query createQuery(String queryString) {
113: errorIfClosed();
114: QueryImpl query = new QueryImpl(queryString, this ,
115: getHQLQueryPlan(queryString, false)
116: .getParameterMetadata());
117: query.setComment(queryString);
118: return query;
119: }
120:
121: public SQLQuery createSQLQuery(String sql) {
122: errorIfClosed();
123: SQLQueryImpl query = new SQLQueryImpl(sql, this , factory
124: .getQueryPlanCache().getSQLParameterMetadata(sql));
125: query.setComment("dynamic native SQL query");
126: return query;
127: }
128:
129: protected HQLQueryPlan getHQLQueryPlan(String query, boolean shallow)
130: throws HibernateException {
131: return factory.getQueryPlanCache().getHQLQueryPlan(query,
132: shallow, getEnabledFilters());
133: }
134:
135: protected NativeSQLQueryPlan getNativeSQLQueryPlan(
136: NativeSQLQuerySpecification spec) throws HibernateException {
137: return factory.getQueryPlanCache().getNativeSQLQueryPlan(spec);
138: }
139:
140: public List list(NativeSQLQuerySpecification spec,
141: QueryParameters queryParameters) throws HibernateException {
142: return listCustomQuery(getNativeSQLQueryPlan(spec)
143: .getCustomQuery(), queryParameters);
144: }
145:
146: public ScrollableResults scroll(NativeSQLQuerySpecification spec,
147: QueryParameters queryParameters) throws HibernateException {
148: return scrollCustomQuery(getNativeSQLQueryPlan(spec)
149: .getCustomQuery(), queryParameters);
150: }
151:
152: }
|