KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > regis > hibernate > HibernateRegisSession


1 package org.sapia.regis.hibernate;
2
3 import org.hibernate.Session;
4 import org.hibernate.Transaction;
5 import org.sapia.regis.Node;
6 import org.sapia.regis.RWSession;
7 import org.sapia.regis.impl.NodeImpl;
8
9 public class HibernateRegisSession implements RWSession{
10   
11   private Session session;
12   private Transaction tx;
13   
14   HibernateRegisSession(Session deleg){
15     session = deleg;
16   }
17   
18   public Node attach(Node node) {
19     return (Node)session.load(NodeImpl.class, (((NodeImpl)node).getId()));
20   }
21   
22   public void begin() {
23     if(tx != null){
24       tx.commit();
25     }
26     tx = session.beginTransaction();
27   }
28   
29   public void commit() {
30     if(tx == null){
31       throw new IllegalStateException JavaDoc("Session not in transaction");
32     }
33     tx.commit();
34     tx = null;
35   }
36   
37   public void rollback() {
38     if(tx == null){
39       throw new IllegalStateException JavaDoc("Session not in transaction");
40     }
41     try{
42       tx.rollback();
43     }finally{
44       tx = null;
45     }
46   }
47   
48   public void close() {
49     Sessions.close();
50     if(session.isOpen()){
51       try{
52         session.flush();
53       }catch(RuntimeException JavaDoc e){
54         session.close();
55         throw e;
56       }
57       session.close();
58     }
59   }
60
61 }
62
Popular Tags