1 /*
2 * Hibernate, Relational Persistence for Idiomatic Java
3 *
4 * Copyright (c) 2008, Red Hat Middleware LLC or third-party contributors as
5 * indicated by the @author tags or express copyright attribution
6 * statements applied by the authors. All third-party contributions are
7 * distributed under license by Red Hat Middleware LLC.
8 *
9 * This copyrighted material is made available to anyone wishing to use, modify,
10 * copy, or redistribute it subject to the terms and conditions of the GNU
11 * Lesser General Public License, as published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
16 * for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this distribution; if not, write to:
20 * Free Software Foundation, Inc.
21 * 51 Franklin Street, Fifth Floor
22 * Boston, MA 02110-1301 USA
23 *
24 */
25 package org.hibernate.impl;
26
27 import org.hibernate.MappingException;
28 import org.hibernate.Query;
29 import org.hibernate.SQLQuery;
30 import org.hibernate.HibernateException;
31 import org.hibernate.ScrollableResults;
32 import org.hibernate.SessionException;
33 import org.hibernate.engine.query.sql.NativeSQLQuerySpecification;
34 import org.hibernate.engine.NamedQueryDefinition;
35 import org.hibernate.engine.NamedSQLQueryDefinition;
36 import org.hibernate.engine.SessionImplementor;
37 import org.hibernate.engine.QueryParameters;
38 import org.hibernate.engine.SessionFactoryImplementor;
39 import org.hibernate.engine.query.HQLQueryPlan;
40 import org.hibernate.engine.query.NativeSQLQueryPlan;
41
42 import java.util.List;
43
44 /**
45 * Functionality common to stateless and stateful sessions
46 *
47 * @author Gavin King
48 */
49 public abstract class AbstractSessionImpl implements SessionImplementor {
50
51 protected transient SessionFactoryImpl factory;
52 private boolean closed = false;
53
54 protected AbstractSessionImpl(SessionFactoryImpl factory) {
55 this.factory = factory;
56 }
57
58 public SessionFactoryImplementor getFactory() {
59 return factory;
60 }
61
62 public boolean isClosed() {
63 return closed;
64 }
65
66 protected void setClosed() {
67 closed = true;
68 }
69
70 protected void errorIfClosed() {
71 if ( closed ) {
72 throw new SessionException( "Session is closed!" );
73 }
74 }
75
76 public Query getNamedQuery(String queryName) throws MappingException {
77 errorIfClosed();
78 NamedQueryDefinition nqd = factory.getNamedQuery( queryName );
79 final Query query;
80 if ( nqd != null ) {
81 String queryString = nqd.getQueryString();
82 query = new QueryImpl(
83 queryString,
84 nqd.getFlushMode(),
85 this,
86 getHQLQueryPlan( queryString, false ).getParameterMetadata()
87 );
88 query.setComment( "named HQL query " + queryName );
89 }
90 else {
91 NamedSQLQueryDefinition nsqlqd = factory.getNamedSQLQuery( queryName );
92 if ( nsqlqd==null ) {
93 throw new MappingException( "Named query not known: " + queryName );
94 }
95 query = new SQLQueryImpl(
96 nsqlqd,
97 this,
98 factory.getQueryPlanCache().getSQLParameterMetadata( nsqlqd.getQueryString() )
99 );
100 query.setComment( "named native SQL query " + queryName );
101 nqd = nsqlqd;
102 }
103 initQuery( query, nqd );
104 return query;
105 }
106
107 public Query getNamedSQLQuery(String queryName) throws MappingException {
108 errorIfClosed();
109 NamedSQLQueryDefinition nsqlqd = factory.getNamedSQLQuery( queryName );
110 if ( nsqlqd==null ) {
111 throw new MappingException( "Named SQL query not known: " + queryName );
112 }
113 Query query = new SQLQueryImpl(
114 nsqlqd,
115 this,
116 factory.getQueryPlanCache().getSQLParameterMetadata( nsqlqd.getQueryString() )
117 );
118 query.setComment( "named native SQL query " + queryName );
119 initQuery( query, nsqlqd );
120 return query;
121 }
122
123 private void initQuery(Query query, NamedQueryDefinition nqd) {
124 query.setCacheable( nqd.isCacheable() );
125 query.setCacheRegion( nqd.getCacheRegion() );
126 if ( nqd.getTimeout()!=null ) query.setTimeout( nqd.getTimeout().intValue() );
127 if ( nqd.getFetchSize()!=null ) query.setFetchSize( nqd.getFetchSize().intValue() );
128 if ( nqd.getCacheMode() != null ) query.setCacheMode( nqd.getCacheMode() );
129 query.setReadOnly( nqd.isReadOnly() );
130 if ( nqd.getComment() != null ) query.setComment( nqd.getComment() );
131 }
132
133 public Query createQuery(String queryString) {
134 errorIfClosed();
135 QueryImpl query = new QueryImpl(
136 queryString,
137 this,
138 getHQLQueryPlan( queryString, false ).getParameterMetadata()
139 );
140 query.setComment( queryString );
141 return query;
142 }
143
144 public SQLQuery createSQLQuery(String sql) {
145 errorIfClosed();
146 SQLQueryImpl query = new SQLQueryImpl(
147 sql,
148 this,
149 factory.getQueryPlanCache().getSQLParameterMetadata( sql )
150 );
151 query.setComment( "dynamic native SQL query" );
152 return query;
153 }
154
155 protected HQLQueryPlan getHQLQueryPlan(String query, boolean shallow) throws HibernateException {
156 return factory.getQueryPlanCache().getHQLQueryPlan( query, shallow, getEnabledFilters() );
157 }
158
159 protected NativeSQLQueryPlan getNativeSQLQueryPlan(NativeSQLQuerySpecification spec) throws HibernateException {
160 return factory.getQueryPlanCache().getNativeSQLQueryPlan( spec );
161 }
162
163 public List list(NativeSQLQuerySpecification spec, QueryParameters queryParameters)
164 throws HibernateException {
165 return listCustomQuery( getNativeSQLQueryPlan( spec ).getCustomQuery(), queryParameters );
166 }
167
168 public ScrollableResults scroll(NativeSQLQuerySpecification spec, QueryParameters queryParameters)
169 throws HibernateException {
170 return scrollCustomQuery( getNativeSQLQueryPlan( spec ).getCustomQuery(), queryParameters );
171 }
172
173 }