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                    try {
072                            queryString = SQLTransformer.transformFromJpqlToHql(queryString);
073    
074                            return new QueryImpl(_session.createQuery(queryString), strictName);
075                    }
076                    catch (Exception e) {
077                            throw ExceptionTranslator.translate(e);
078                    }
079            }
080    
081            public SQLQuery createSQLQuery(String queryString) throws ORMException {
082                    return createSQLQuery(queryString, true);
083            }
084    
085            public SQLQuery createSQLQuery(String queryString, boolean strictName)
086                    throws ORMException {
087    
088                    try {
089                            queryString = SQLTransformer.transformFromJpqlToHql(queryString);
090    
091                            return new SQLQueryImpl(
092                                    _session.createSQLQuery(queryString), strictName);
093                    }
094                    catch (Exception e) {
095                            throw ExceptionTranslator.translate(e);
096                    }
097            }
098    
099            public void delete(Object object) throws ORMException {
100                    try {
101                            _session.delete(object);
102                    }
103                    catch (Exception e) {
104                            throw ExceptionTranslator.translate(e);
105                    }
106            }
107    
108            public void evict(Object object) throws ORMException {
109                    try {
110                            _session.evict(object);
111                    }
112                    catch (Exception e) {
113                            throw ExceptionTranslator.translate(e);
114                    }
115            }
116    
117            public void flush() throws ORMException {
118                    try {
119                            _session.flush();
120                    }
121                    catch (Exception e) {
122                            throw ExceptionTranslator.translate(e);
123                    }
124            }
125    
126            public Object get(Class<?> clazz, Serializable id) throws ORMException {
127                    try {
128                            return _session.get(clazz, id);
129                    }
130                    catch (Exception e) {
131                            throw ExceptionTranslator.translate(e);
132                    }
133            }
134    
135            /**
136             * @deprecated
137             */
138            public Object get(Class<?> clazz, Serializable id, LockMode lockMode)
139                    throws ORMException {
140    
141                    try {
142                            return _session.get(
143                                    clazz, id, LockModeTranslator.translate(lockMode));
144                    }
145                    catch (Exception e) {
146                            throw ExceptionTranslator.translate(e);
147                    }
148            }
149    
150            public Object getWrappedSession() {
151                    return _session;
152            }
153    
154            public Object load(Class<?> clazz, Serializable id) throws ORMException {
155                    try {
156                            return _session.load(clazz, id);
157                    }
158                    catch (Exception e) {
159                            throw ExceptionTranslator.translate(e);
160                    }
161            }
162    
163            public Object merge(Object object) throws ORMException {
164                    try {
165                            return _session.merge(object);
166                    }
167                    catch (Exception e) {
168                            throw ExceptionTranslator.translate(e);
169                    }
170            }
171    
172            public Serializable save(Object object) throws ORMException {
173                    try {
174                            return _session.save(object);
175                    }
176                    catch (Exception e) {
177                            throw ExceptionTranslator.translate(e);
178                    }
179            }
180    
181            public void saveOrUpdate(Object object) throws ORMException {
182                    try {
183                            _session.saveOrUpdate(object);
184                    }
185                    catch (Exception e) {
186                            throw ExceptionTranslator.translate(e);
187                    }
188            }
189    
190            private org.hibernate.Session _session;
191    
192    }