1 /*
2 * Created on 2003-11-16
3 *
4 * /
5 package com.javaeye.crm;
6
7 Import Net.sf.hibernate.HibernateException;
8 Import net.sf.hibernate.Session;
9 Import net.sf.hibernate.SessionFactory;
10 Import net.sf.hibernate.transaction;
11 Import net.sf.hibernate.cfg.configuration;
12 Import net.sf.hibord.tool.hbm2ddl.schemaExport;
13
14 / **
15 *
16 * Get the session connection factory class
17 *
18 * @Author Robbin Fan
19 *
20 * /
21 Public Class HibernateSession {
twenty two
23 private static final threadlocal sessionthread = new threadlocal ();
twenty four
25 private static final threadlocal transactionsthread = new threadlocal ();
26
27 private static sessionFactory sf = null;
Twist
29 / **
30 * Get the session used in the current thread
31 *
32 * @Return session
33 * @Throws HibernateException
34 * /
35 public static session currentsession () throws hibernateException {
36 session s = (session) sessionthread.get ();
37 IF (s == null) {
...}
38 IF (sf == null) sf = new configuration (). Configure (). BuildSessionFactory ();
39 s = sf.opensession ();
40 sessionthread.set (s);
41}
42 RETURN S;
43}
44
45 / **
46 * Get a new session
47 *
48 * @Return session
49 * @Throws HibernateException
50 * /
51 public static session newsession () THROWS HibernateException {
52
53 IF (sf == null) sf = new configuration (). Configure (). BuildSessionFactory ();
54 session s = sf.opensession ();
55 RETURN S;
56}
57
58 / **
59 * Start or join the current session Transaction
60 *
61 * @Return Transaction
62 * @Throws HibernateException
63 * /
64 Public Static Transaction CurrentTransaction () throws HibernateException {
...}
TransactionThread.get (); 005;
66 IF (tx == null) {...}
67 tx = currentations (). Begintransaction ();
68 Transactionthread.Set (TX);
69}
70 Return TX;
71}
72
73 / **
74 * Submit the current session TRANSAction
75 *
76 * @Throws HibernateException
77 * /
78 public static void committransaction () throws hibernateException {
...}
Transactions TX = (Transaction) TransactionsThread.get ();
80 Transactionthread.Set (NULL);
81 IF (tx! = Null) tx.commit ();
82}
83
84 / **
85 * rollback the current transaction
86 *
87 * @Throws HibernateException
88 * /
89 public static void rollbacktransaction () throws hibernateException {
...}
TRANSACTION TX = (Transaction) TransactionsThread.get ();
91 Transactionthread.Set (NULL);
92 IF (tx! = Null) tx.rollback ();
93}
94
95 / **
96 * Close the session used by the current thread
97 *
98 * @Throws HibernateException
99 * /
100 public static void closesession () throws hibernateException {
101 session s = (session) sessionthread.get ();
102 sessionthread.set (null);
103 IF (s! = Null) s.close ();
104}
105
106 / **
107 * Generate a database DDL based on the mapping file and persistent object, generate the file as create_table.sql
108 *
109 * @Param args parameters
110 * /
111 public static void main (string [] args) {
...}
112 try {
...}
113 string conf = "hibernate.cfg.xml";
114 if (args.length! = 0) conf = args [0];
115 Configuration CFG = New Configuration (). Configure ("/" conf);
116 SchemaExport SE = New SchemaExport (CFG);
117 //se.setoutputfile ("create_table.sql ");
118 SE.CREATE (TRUE, TRUE);
119} catch (hibernateException e) {
120 System.out.println (E.GetMessage ());
121}
122
123}
124}