Rev 2009 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2009 | es | 1 | package pt.estgp.es.exemplos.hibernate; |
2 | |||
3 | |||
4 | import org.apache.log4j.Logger; |
||
5 | import org.hibernate.Session; |
||
6 | import org.hibernate.SessionFactory; |
||
7 | import org.hibernate.cfg.Configuration; |
||
2049 | es | 8 | import org.hibernate.proxy.HibernateProxy; |
2009 | es | 9 | |
10 | public class HibernateUtils { |
||
11 | |||
12 | // private static Session session; |
||
13 | private static final 1.5.0/docs/api/java/util/logging/Logger.html">Logger logger = 1.5.0/docs/api/java/util/logging/Logger.html">Logger.getLogger(HibernateUtils.class); |
||
14 | private static SessionFactory sessionFactory; |
||
15 | private static SessionFactory migrationSessionFactory; |
||
16 | |||
17 | static |
||
18 | { |
||
19 | try |
||
20 | { |
||
21 | createFactory(); |
||
22 | |||
23 | // session = sessionFactory.openSession(); |
||
24 | } |
||
25 | catch (1.5.0/docs/api/java/lang/Throwable.html">Throwable e) |
||
26 | { |
||
27 | logger.fatal(e,e); |
||
28 | throw new 1.5.0/docs/api/java/lang/ExceptionInInitializerError.html">ExceptionInInitializerError(e); |
||
29 | } |
||
30 | } |
||
31 | |||
32 | private static void createFactory() |
||
33 | { |
||
34 | 1.5.0/docs/api/javax/security/auth/login/Configuration.html">Configuration config = new 1.5.0/docs/api/javax/security/auth/login/Configuration.html">Configuration(); |
||
35 | config.configure(1.5.0/docs/api/java/lang/Thread.html">Thread.currentThread().getContextClassLoader().getResource("pt/estgp/es/exemplos/hibernate/hibernate.cfg.xml")); |
||
36 | //config.setInterceptor(new DirtyCheckInterceptor()); |
||
37 | sessionFactory = config.buildSessionFactory(); |
||
38 | |||
39 | } |
||
40 | |||
41 | |||
42 | |||
43 | public static SessionFactory getSessionFactory() |
||
44 | { |
||
45 | if(sessionFactory.isClosed()) |
||
46 | createFactory(); |
||
47 | return sessionFactory; |
||
48 | } |
||
49 | |||
50 | public static synchronized Session openSession() |
||
51 | { |
||
52 | // if(!session.isOpen()) |
||
53 | // return sessionFactory.openSession(); |
||
54 | // return session; |
||
55 | return getCurrentSession(); |
||
56 | } |
||
57 | public static synchronized Session getCurrentSession() throws 1.5.0/docs/api/java/lang/IllegalStateException.html">IllegalStateException |
||
58 | { |
||
59 | // if(!examples.isOpen()) |
||
60 | // examples = sessionFactory.openSession(); |
||
61 | // return examples; |
||
62 | return getSessionFactory().getCurrentSession(); |
||
63 | } |
||
64 | |||
2049 | es | 65 | /** |
66 | * |
||
67 | * @param o |
||
68 | * @return |
||
69 | */ |
||
70 | public static 5+0%2Fdocs%2Fapi+Object">Object narrow(5+0%2Fdocs%2Fapi+Object">Object o) |
||
71 | { |
||
72 | if(o == null) |
||
73 | return null; |
||
74 | |||
75 | if (o instanceof HibernateProxy) |
||
76 | { |
||
77 | return ((HibernateProxy)o).getHibernateLazyInitializer().getImplementation(); |
||
78 | } |
||
79 | |||
80 | return o; |
||
81 | } |
||
82 | |||
83 | |||
2009 | es | 84 | } |