Added tutorials of the hibernate-release 4.3.9.Final as integration-tests
[hibernate4-maven-plugin] / src / it / tutorials / osgi / unmanaged-native / src / main / java / org / hibernate / osgitest / DataPointServiceImpl.java
1 /* 
2  * Hibernate, Relational Persistence for Idiomatic Java
3  * 
4  * JBoss, Home of Professional Open Source
5  * Copyright 2013 Red Hat Inc. and/or its affiliates and other contributors
6  * as indicated by the @authors tag. All rights reserved.
7  * See the copyright.txt in the distribution for a
8  * full listing of individual contributors.
9  *
10  * This copyrighted material is made available to anyone wishing to use,
11  * modify, copy, or redistribute it subject to the terms and conditions
12  * of the GNU Lesser General Public License, v. 2.1.
13  * This program is distributed in the hope that it will be useful, but WITHOUT A
14  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
15  * PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
16  * You should have received a copy of the GNU Lesser General Public License,
17  * v.2.1 along with this distribution; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19  * MA  02110-1301, USA.
20  */
21 package org.hibernate.osgitest;
22
23 import java.util.HashSet;
24 import java.util.List;
25 import java.util.Map;
26
27 import org.hibernate.Hibernate;
28 import org.hibernate.Session;
29 import org.hibernate.criterion.Restrictions;
30 import org.hibernate.envers.AuditReader;
31 import org.hibernate.envers.AuditReaderFactory;
32 import org.hibernate.envers.DefaultRevisionEntity;
33 import org.hibernate.osgitest.entity.DataPoint;
34
35 /**
36  * @author Brett Meyer
37  */
38 public class DataPointServiceImpl implements DataPointService {
39         
40         private HibernateUtil hibernateUtil = new HibernateUtil();
41
42         public void add(DataPoint dp) {
43                 Session s = hibernateUtil.getSession();
44                 s.getTransaction().begin();
45                 s.persist( dp );
46                 s.getTransaction().commit();
47                 s.close();
48         }
49
50         public void update(DataPoint dp) {
51                 Session s = hibernateUtil.getSession();
52                 s.getTransaction().begin();
53                 s.update( dp );
54                 s.getTransaction().commit();
55                 s.close();
56         }
57
58         public DataPoint get(long id) {
59                 Session s = hibernateUtil.getSession();
60                 s.getTransaction().begin();
61                 DataPoint dp = (DataPoint) s.createCriteria( DataPoint.class ).add(
62                                 Restrictions.eq( "id", id ) ).uniqueResult();
63                 s.getTransaction().commit();
64                 s.close();
65                 return dp;
66         }
67
68         // Test lazy loading (mainly to make sure the proxy classes work in OSGi)
69         public DataPoint load(long id) {
70                 Session s = hibernateUtil.getSession();
71                 s.getTransaction().begin();
72                 DataPoint dp = (DataPoint) s.load( DataPoint.class, new Long(id) );
73                 // initialize
74                 dp.getName();
75                 s.getTransaction().commit();
76                 s.close();
77                 return dp;
78         }
79
80         public List<DataPoint> getAll() {
81                 Session s = hibernateUtil.getSession();
82                 s.getTransaction().begin();
83                 List list = s.createQuery( "from DataPoint" ).list();
84                 s.getTransaction().commit();
85                 s.close();
86                 return list;
87         }
88         
89         public Map<Number, DefaultRevisionEntity> getRevisions(long id) {
90                 Session s = hibernateUtil.getSession();
91                 AuditReader reader = AuditReaderFactory.get(s);
92                 List<Number> revisionNums = reader.getRevisions( DataPoint.class, id );
93                 return reader.findRevisions( DefaultRevisionEntity.class, new HashSet<Number>(revisionNums) );
94         }
95
96         public void deleteAll() {
97                 Session s = hibernateUtil.getSession();
98                 s.getTransaction().begin();
99                 s.createQuery( "delete from DataPoint" ).executeUpdate();
100                 s.getTransaction().commit();
101                 s.close();
102         }
103
104 }