001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.dao.orm.hibernate;
016    
017    import com.liferay.portal.dao.orm.common.SQLTransformer;
018    import com.liferay.portal.kernel.dao.orm.LockMode;
019    import com.liferay.portal.kernel.dao.orm.ORMException;
020    import com.liferay.portal.kernel.dao.orm.Query;
021    import com.liferay.portal.kernel.dao.orm.SQLQuery;
022    import com.liferay.portal.kernel.dao.orm.Session;
023    
024    import java.io.Serializable;
025    
026    import java.sql.Connection;
027    
028    /**
029     * @author Brian Wing Shun Chan
030     * @author Shuyang Zhou
031     */
032    public class SessionImpl implements Session {
033    
034            public SessionImpl(org.hibernate.Session session) {
035                    _session = session;
036            }
037    
038            public void clear() throws ORMException {
039                    try {
040                            _session.clear();
041                    }
042                    catch (Exception e) {
043                            throw ExceptionTranslator.translate(e);
044                    }
045            }
046    
047            public Connection close() throws ORMException {
048                    try {
049                            return _session.close();
050                    }
051                    catch (Exception e) {
052                            throw ExceptionTranslator.translate(e);
053                    }
054            }
055    
056            public boolean contains(Object object) throws ORMException {
057                    try {
058                            return _session.contains(object);
059                    }
060                    catch (Exception e) {
061                            throw ExceptionTranslator.translate(e);
062                    }
063            }
064    
065            public Query createQuery(String queryString) throws ORMException {
066                    return createQuery(queryString, true);
067            }
068    
069            public Query createQuery(String queryString, boolean strictName)
070                    throws ORMException {
071    
072                    try {
073                            queryString = SQLTransformer.transformFromJpqlToHql(queryString);
074    
075                            return new QueryImpl(_session.createQuery(queryString), strictName);
076                    }
077                    catch (Exception e) {
078                            throw ExceptionTranslator.translate(e);
079                    }
080            }
081    
082            public SQLQuery createSQLQuery(String queryString) throws ORMException {
083                    return createSQLQuery(queryString, true);
084            }
085    
086            public SQLQuery createSQLQuery(String queryString, boolean strictName)
087                    throws ORMException {
088    
089                    try {
090                            queryString = SQLTransformer.transformFromJpqlToHql(queryString);
091    
092                            return new SQLQueryImpl(
093                                    _session.createSQLQuery(queryString), strictName);
094                    }
095                    catch (Exception e) {
096                            throw ExceptionTranslator.translate(e);
097                    }
098            }
099    
100            public void delete(Object object) throws ORMException {
101                    try {
102                            _session.delete(object);
103                    }
104                    catch (Exception e) {
105                            throw ExceptionTranslator.translate(e);
106                    }
107            }
108    
109            public void evict(Object object) throws ORMException {
110                    try {
111                            _session.evict(object);
112                    }
113                    catch (Exception e) {
114                            throw ExceptionTranslator.translate(e);
115                    }
116            }
117    
118            public void flush() throws ORMException {
119                    try {
120                            _session.flush();
121                    }
122                    catch (Exception e) {
123                            throw ExceptionTranslator.translate(e);
124                    }
125            }
126    
127            public Object get(Class<?> clazz, Serializable id) throws ORMException {
128                    try {
129                            return _session.get(clazz, id);
130                    }
131                    catch (Exception e) {
132                            throw ExceptionTranslator.translate(e);
133                    }
134            }
135    
136            /**
137             * @deprecated
138             */
139            public Object get(Class<?> clazz, Serializable id, LockMode lockMode)
140                    throws ORMException {
141    
142                    try {
143                            return _session.get(
144                                    clazz, id, LockModeTranslator.translate(lockMode));
145                    }
146                    catch (Exception e) {
147                            throw ExceptionTranslator.translate(e);
148                    }
149            }
150    
151            public Object getWrappedSession() {
152                    return _session;
153            }
154    
155            public Object load(Class<?> clazz, Serializable id) throws ORMException {
156                    try {
157                            return _session.load(clazz, id);
158                    }
159                    catch (Exception e) {
160                            throw ExceptionTranslator.translate(e);
161                    }
162            }
163    
164            public Object merge(Object object) throws ORMException {
165                    try {
166                            return _session.merge(object);
167                    }
168                    catch (Exception e) {
169                            throw ExceptionTranslator.translate(e);
170                    }
171            }
172    
173            public Serializable save(Object object) throws ORMException {
174                    try {
175                            return _session.save(object);
176                    }
177                    catch (Exception e) {
178                            throw ExceptionTranslator.translate(e);
179                    }
180            }
181    
182            public void saveOrUpdate(Object object) throws ORMException {
183                    try {
184                            _session.saveOrUpdate(object);
185                    }
186                    catch (Exception e) {
187                            throw ExceptionTranslator.translate(e);
188                    }
189            }
190    
191            private org.hibernate.Session _session;
192    
193    }