Salome HOME
Id now is Long instead of Integer. First unit test is created. hibernate-3.5.jar...
[tools/siman.git] / Workspace / Siman-Common / src / org / splat / dal / dao / kernel / GenericDAOImpl.java
1 /*****************************************************************************
2  * Company         EURIWARE
3  * Application     SIMAN
4  * File            $Id$ 
5  * Creation date   08.10.2012
6  * @author         $Author$
7  * @version        $Revision$
8  *****************************************************************************/
9
10 package org.splat.dal.dao.kernel;
11
12 import java.io.Serializable;
13
14 import org.hibernate.Session;
15 import org.hibernate.SessionFactory;
16 import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
17
18 /**
19  * Generic DAO implementation.
20  * 
21  * @author <a href="mailto:roman.kozlov@opencascade.com">Roman Kozlov (RKV)</a>
22  * 
23  * @param <T>
24  *            Persistent object class
25  * @param <PK>
26  *            Primary key class
27  */
28 public abstract class GenericDAOImpl<T, PK extends Serializable> extends HibernateDaoSupport implements
29                 GenericDAO<T, PK> {
30 //      /**
31 //       * Hibernate session factory.
32 //       */
33 //      private SessionFactory _sessionFactory;
34
35         /**
36          * Persist the newInstance object into database.
37          * 
38          * @param newInstance
39          *            new object as a transient instance
40          * @return new primary key for the created persistent object
41          */
42         @SuppressWarnings("unchecked")
43         public PK create(T newInstance) {
44                 return (PK) getSession().save(newInstance);
45         }
46
47         /**
48          * Retrieve an object that was previously persisted to the database using the indicated id as primary key.
49          * 
50          * @param id
51          *            primary key of an object to read
52          * @return an object found by the given key
53          */
54         @SuppressWarnings("unchecked")
55         public T read(PK id) {
56                 return (T) getSession().get(getType(), id);
57         }
58
59         /** Save changes made to a persistent object.
60          * @param transientObject transient instance of the object to update
61          */
62         public void update(T transientObject) {
63                 getSession().update(transientObject);
64         }
65
66         /** Remove an object from persistent storage in the database.
67          * @param persistentObject a persistent object to delete from the database
68          */
69         public void delete(T persistentObject) {
70                 getSession().delete(persistentObject);
71         }
72
73         /**
74          * Get persistent object type.
75          * @return Persistent object class to be processed by this DAO
76          */
77         abstract protected Class<T> getType();
78         
79 //      /**
80 //       * Get the current hibernate session factory.
81 //       * @return hibernate session
82 //       */
83 //      private Session getSession() {
84 //              return getSessionFactory().getCurrentSession();
85 //      }
86 //      
87 //      /**
88 //       * Get the hibernate session factory.
89 //       * @return the hibernate session factory
90 //       */
91 //      public SessionFactory getSessionFactory() {
92 //              return _sessionFactory;
93 //      }
94 //
95 //      /**
96 //       * Set the hibernate session factory.
97 //       * @param sessionFactory the hibernate session factory to set
98 //       */
99 //      public void setSessionFactory(SessionFactory sessionFactory) {
100 //              _sessionFactory = sessionFactory;
101 //      }
102 }