Source Code Cross Referenced for HibernateUtil.java in  » Ajax » zk » org » zkoss » zkplus » hibernate » 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 » Ajax » zk » org.zkoss.zkplus.hibernate 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /* HibernateUtil.java
002:
003:        {{IS_NOTE
004:        	Purpose:
005:        		
006:        	Description:
007:        		
008:        	History:
009:        		Mon Sep  4 17:18:21     2006, Created by henrichen
010:        }}IS_NOTE
011:
012:        Copyright (C) 2006 Potix Corporation. All Rights Reserved.
013:
014:        {{IS_RIGHT
015:        }}IS_RIGHT
016:         */
017:        package org.zkoss.zkplus.hibernate;
018:
019:        import org.zkoss.zk.ui.WebApp;
020:        import org.zkoss.zk.ui.Desktop;
021:        import org.zkoss.zk.ui.Execution;
022:        import org.zkoss.zk.ui.Executions;
023:        import org.zkoss.util.logging.Log;
024:        import org.zkoss.lang.JVMs;
025:
026:        import org.hibernate.Session;
027:        import org.hibernate.SessionFactory;
028:        import org.hibernate.ConnectionReleaseMode;
029:        import org.hibernate.HibernateException;
030:        import org.hibernate.cfg.Configuration;
031:        import org.hibernate.cfg.AnnotationConfiguration;
032:
033:        /**
034:         * <p>Utitlity to access Hibernate Session. This implemenation works with the Hibernate's 
035:         * thread session context (version 3.1+). That is, you have to specified 
036:         * hibernate's configuration file "hibernate.cfg.xml" to as follows:</p>
037:         *
038:         * <pre><code>
039:         *  &lt;session-factory>
040:         *		...
041:         *		&lt;property name="current_session_context_class">thread&lt;/property>
042:         *  &lt;/session-factory>
043:         * </code><pre>
044:         *
045:         * <p>Since ZK 3.0.1, if your hibernate configuration file name is not the default "hibernate.cfg.xml", you can 
046:         * specify it in WEB-INF/zk.xml. Just add following lines:
047:         * <pre><code>
048:         *	&lt;preference>
049:         *	&lt;name>HibernateUtil.config&lt;/name>
050:         *	&lt;value>YOUR-HIBERNATE-FILENAME&lt;/value>
051:         * </code></pre>
052:         * </p>
053:        
054:         * <p> Also notice that the zkplus.jar must be put under application's WEB-INF/lib because
055:         * the SessionFactory is stored as a class static member.
056:         *
057:         * @author henrichen
058:         */
059:        public class HibernateUtil {
060:            public static String CONFIG = "HibernateUtil.config";
061:            private static final Log log = Log.lookup(HibernateUtil.class);
062:
063:            private static SessionFactory _factory;
064:
065:            /**
066:             * Get the singleton hibernate Session Factory.
067:             */
068:            public static SessionFactory getSessionFactory() {
069:                return initSessionFactory((WebApp) null);
070:            }
071:
072:            /**
073:             * Wrapping HibernateUtil.getSessionFactory().getCurrentSession() into a simple API.
074:             */
075:            public static Session currentSession() throws HibernateException {
076:                return getSessionFactory().getCurrentSession();
077:            }
078:
079:            /**
080:             * Wrapping HibernateUtil.getSessionFactory().getCurrentSession().close() into a simple API.
081:             */
082:            public static void closeSession() throws HibernateException {
083:                currentSession().close();
084:            }
085:
086:            /**
087:             * Used in {@link HibernateSessionFactoryListener} to init
088:             * Hibernate SessionFactory.
089:             * @deprecated Use {@link #initSessionFactory(WebApp app)} instead.
090:             */
091:            /* package */static SessionFactory initSessionFactory() {
092:                return initSessionFactory((WebApp) null);
093:            }
094:
095:            /**
096:             * Used in {@link HibernateSessionFactoryListener} to init
097:             * Hibernate SessionFactory.
098:             * @param app web applicaton, given null will try to get it from current Execution.
099:             * @since 3.0.1
100:             */
101:            /* package */static SessionFactory initSessionFactory(WebApp app) {
102:                if (_factory == null) {
103:                    //read hibernate.config preference
104:                    if (app == null) {
105:                        final Execution exec = Executions.getCurrent();
106:                        if (exec != null) {
107:                            final Desktop desktop = exec.getDesktop();
108:                            if (desktop != null) {
109:                                app = desktop.getWebApp();
110:                            }
111:                        }
112:                    }
113:                    String resource = null;
114:                    if (app != null) {
115:                        final org.zkoss.zk.ui.util.Configuration config = app
116:                                .getConfiguration();
117:                        resource = config.getPreference(CONFIG, null);
118:                    }
119:                    return initSessionFactory(resource);
120:                }
121:                return _factory;
122:            }
123:
124:            /*package*/static SessionFactory initSessionFactory(String resource) {
125:                if (_factory == null) {
126:                    try {
127:                        // Create the SessionFactory per JavaVM version and allow JDK 1.4 compatibility
128:                        if (JVMs.isJava5()) {
129:                            _factory = java5Factory(resource);
130:                        } else {
131:                            _factory = java4Factory(resource);
132:                        }
133:                        log.info("Hibernate configuration file loaded: "
134:                                + (resource == null ? "hibernate.cfg.xml"
135:                                        : resource));
136:                    } catch (Throwable ex) {
137:                        // Make sure you log the exception, as it might be swallowed
138:                        log.error("Initial SessionFactory creation failed."
139:                                + ex);
140:                        throw new ExceptionInInitializerError(ex);
141:                    }
142:                }
143:                return _factory;
144:            }
145:
146:            //We have to put the instantiation of AnnotationConfiguration
147:            //in a separate method. Otherwise, it will be loaded even if
148:            //isJava5 is false
149:            /*private*/static SessionFactory java5Factory(String resource) {
150:                return resource == null ? new AnnotationConfiguration()
151:                        .configure().buildSessionFactory()
152:                        : new AnnotationConfiguration().configure(resource)
153:                                .buildSessionFactory();
154:            }
155:
156:            /*private */static SessionFactory java4Factory(String resource) {
157:                return resource == null ? new Configuration().configure()
158:                        .buildSessionFactory() : new Configuration().configure(
159:                        resource).buildSessionFactory();
160:            }
161:
162:            /**
163:             * Used in {@link HibernateSessionFactoryListener} to init
164:             * Hibernate SessionFactory.
165:             */
166:            /* package */static void cleanupSessionFactory() {
167:                if (_factory != null) {
168:                    _factory.close(); // Free all resources
169:                    _factory = null;
170:                }
171:            }
172:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.