Source Code Cross Referenced for BaseDAO.java in  » ESB » wso2-esb » org » wso2 » esb » persistence » dao » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » ESB » wso2 esb » org.wso2.esb.persistence.dao 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright 2004,2005 The Apache Software Foundation.
003:         *
004:         * Licensed under the Apache License, Version 2.0 (the "License");
005:         * you may not use this file except in compliance with the License.
006:         * You may obtain a copy of the License at
007:         *
008:         *      http://www.apache.org/licenses/LICENSE-2.0
009:         *
010:         * Unless required by applicable law or agreed to in writing, software
011:         * distributed under the License is distributed on an "AS IS" BASIS,
012:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013:         * See the License for the specific language governing permissions and
014:         * limitations under the License.
015:         */
016:        package org.wso2.esb.persistence.dao;
017:
018:        import org.apache.log4j.Logger;
019:        import org.hibernate.*;
020:        import org.hibernate.exception.ConstraintViolationException;
021:        import org.wso2.esb.persistence.dataobject.BaseDO;
022:        import org.wso2.esb.util.HibernateConfig;
023:
024:        import java.util.Date;
025:        import java.util.List;
026:
027:        /**
028:         * The base DataAccessObject from which each DAO of the ESB is extended
029:         */
030:        public abstract class BaseDAO {
031:
032:            protected Logger log = Logger.getLogger(BaseDAO.class);
033:
034:            /**
035:             * The HibernateConfiguration
036:             */
037:            protected HibernateConfig hbConfig;
038:
039:            public BaseDAO(HibernateConfig hbConfig) {
040:                this .hbConfig = hbConfig;
041:            }
042:
043:            /**
044:             * To save a entity
045:             *
046:             * @param trasientInstance
047:             * @return The entity ID
048:             */
049:            public Long create(BaseDO trasientInstance) {
050:                Session session = hbConfig.currentSession();
051:                Transaction tx = session.beginTransaction();
052:                try {
053:                    trasientInstance.setLastUpdatedTime(new Date());
054:                    session.persist(trasientInstance);
055:                    session.flush();
056:                    tx.commit();
057:                } catch (ConstraintViolationException e) {
058:                    String msg = "Trying to create duplicate entity : "
059:                            + trasientInstance;
060:                    log.warn(msg, e);
061:                } catch (HibernateException e) {
062:                    tx.rollback();
063:                    handleException("Cannot create entity " + trasientInstance,
064:                            e);
065:                } finally {
066:                    hbConfig.closeSession();
067:                }
068:
069:                return trasientInstance.getId();
070:            }
071:
072:            /**
073:             * To create a new entity or update already existed entity
074:             *
075:             * @param instance
076:             * @return The entity ID
077:             */
078:            public Long createOrUpdate(BaseDO instance) {
079:                Session session = hbConfig.currentSession();
080:                Transaction tx = session.beginTransaction();
081:                try {
082:                    instance.setLastUpdatedTime(new Date());
083:                    session.saveOrUpdate(instance);
084:                    session.flush();
085:                    tx.commit();
086:                } catch (ConstraintViolationException e) {
087:                    String msg = "Trying to create duplicate entity "
088:                            + instance;
089:                    log.warn(msg, e);
090:                } catch (HibernateException e) {
091:                    tx.rollback();
092:                    handleException("Cannot create entity " + instance, e);
093:                } finally {
094:                    hbConfig.closeSession();
095:                }
096:
097:                return instance.getId();
098:            }
099:
100:            /**
101:             * To update a already existed entity
102:             *
103:             * @param abstractDO
104:             */
105:            public void update(BaseDO abstractDO) {
106:                Session session = hbConfig.currentSession();
107:                Transaction tx = session.beginTransaction();
108:                try {
109:                    abstractDO.setLastUpdatedTime(new Date());
110:                    session.merge(abstractDO);
111:                    session.flush();
112:                    tx.commit();
113:                } catch (HibernateException e) {
114:                    tx.rollback();
115:                    handleException("Unable to update " + abstractDO, e);
116:                } finally {
117:                    hbConfig.closeSession();
118:                }
119:            }
120:
121:            /**
122:             * To delete a entity
123:             *
124:             * @param abstractDO
125:             */
126:            public void delete(BaseDO abstractDO) {
127:                Session session = hbConfig.currentSession();
128:                Transaction tx = session.beginTransaction();
129:                try {
130:                    session.delete(abstractDO);
131:                    session.flush();
132:                    tx.commit();
133:                } catch (HibernateException e) {
134:                    tx.rollback();
135:                    handleException("Unable to delete " + abstractDO, e);
136:                } finally {
137:                    hbConfig.closeSession();
138:                }
139:            }
140:
141:            /**
142:             * To Retrieves entities as a List
143:             *
144:             * @param hqlQuery
145:             * @return The resultset
146:             */
147:            public List select(String hqlQuery) {
148:                Session session = hbConfig.currentSession();
149:                Transaction tx = session.beginTransaction();
150:                Query query;
151:                List result = null;
152:                try {
153:                    query = session.createQuery(hqlQuery);
154:                    result = query.list();
155:                } catch (HibernateException e) {
156:                    tx.rollback();
157:                    handleException("Error executing HQL query : " + hqlQuery,
158:                            e);
159:                } finally {
160:                    hbConfig.closeSession();
161:                }
162:                return result;
163:            }
164:
165:            /**
166:             * To execute a query
167:             *
168:             * @param hqlQuery
169:             * @return The resultset
170:             */
171:            public int execute(String hqlQuery) {
172:                Session session = hbConfig.currentSession();
173:                Transaction tx = session.beginTransaction();
174:                Query query;
175:                int result = -1;
176:                try {
177:                    query = session.createQuery(hqlQuery);
178:                    result = query.executeUpdate();
179:                } catch (HibernateException e) {
180:                    tx.rollback();
181:                    handleException("Error executing HQL query : " + hqlQuery,
182:                            e);
183:                } finally {
184:                    hbConfig.closeSession();
185:                }
186:                return result;
187:            }
188:
189:            /**
190:             * To Retrieves one unique entity as a Object
191:             *
192:             * @param hqlQuery
193:             * @return The entity
194:             */
195:            public Object selectUnique(String hqlQuery) {
196:
197:                Session session = hbConfig.currentSession();
198:                Transaction tx = session.beginTransaction();
199:                Query query;
200:                Object result = null;
201:                try {
202:                    query = session.createQuery(hqlQuery);
203:                    result = query.uniqueResult();
204:                } catch (HibernateException e) {
205:                    tx.rollback();
206:                    handleException("Error executing HQL query : " + hqlQuery,
207:                            e);
208:                } finally {
209:                    hbConfig.closeSession();
210:                }
211:                return result;
212:            }
213:
214:            /**
215:             * To Retrieves entities as a List using SQL
216:             *
217:             * @param sqlQuery
218:             * @param aClass
219:             * @return The resultset
220:             */
221:            public List selectBySQL(String sqlQuery, Class aClass) {
222:                Session session = hbConfig.currentSession();
223:                List result = null;
224:
225:                Transaction tx = session.beginTransaction();
226:                try {
227:                    SQLQuery query = session.createSQLQuery(sqlQuery)
228:                            .addEntity(aClass);
229:                    result = query.list();
230:
231:                } catch (HibernateException e) {
232:                    tx.rollback();
233:                    handleException("Error executing SQL : " + sqlQuery, e);
234:                } finally {
235:                    if (session != null) {
236:                        try {
237:                            hbConfig.closeSession();
238:                        } catch (HibernateException ignore) {
239:                            log.warn(ignore);
240:                        }
241:                    }
242:                }
243:                return result;
244:            }
245:
246:            private void handleException(String msg, Exception e) {
247:                log.error(msg, e);
248:                throw new RuntimeException(msg, e);
249:            }
250:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.