Added tutorials of the hibernate-release 5.0.12.Final
[hibernate4-maven-plugin] / src / it / tutorials-5.0.12 / 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 org.hibernate.Session;
24 import org.hibernate.criterion.Restrictions;
25 import org.hibernate.envers.AuditReader;
26 import org.hibernate.envers.AuditReaderFactory;
27 import org.hibernate.envers.DefaultRevisionEntity;
28 import org.hibernate.osgitest.entity.DataPoint;
29
30 import java.util.HashSet;
31 import java.util.List;
32 import java.util.Map;
33
34 /**
35  * @author Brett Meyer
36  */
37 public class DataPointServiceImpl implements DataPointService {
38         
39         private HibernateUtil hibernateUtil = new HibernateUtil();
40
41         public void add(DataPoint dp) {
42                 Session s = hibernateUtil.getSession();
43                 s.getTransaction().begin();
44                 s.persist( dp );
45                 s.getTransaction().commit();
46                 s.close();
47         }
48
49         public void update(DataPoint dp) {
50                 Session s = hibernateUtil.getSession();
51                 s.getTransaction().begin();
52                 s.update( dp );
53                 s.getTransaction().commit();
54                 s.close();
55         }
56
57         public DataPoint get(long id) {
58                 Session s = hibernateUtil.getSession();
59                 s.getTransaction().begin();
60                 DataPoint dp = (DataPoint) s.createCriteria( DataPoint.class ).add(
61                                 Restrictions.eq( "id", id ) ).uniqueResult();
62                 s.getTransaction().commit();
63                 s.close();
64                 return dp;
65         }
66
67         // Test lazy loading (mainly to make sure the proxy classes work in OSGi)
68         public DataPoint load(long id) {
69                 Session s = hibernateUtil.getSession();
70                 s.getTransaction().begin();
71                 DataPoint dp = (DataPoint) s.load( DataPoint.class, new Long(id) );
72                 // initialize
73                 dp.getName();
74                 s.getTransaction().commit();
75                 s.close();
76                 return dp;
77         }
78
79         public List<DataPoint> getAll() {
80                 Session s = hibernateUtil.getSession();
81                 s.getTransaction().begin();
82                 List list = s.createQuery( "from DataPoint" ).list();
83                 s.getTransaction().commit();
84                 s.close();
85                 return list;
86         }
87         
88         public Map<Number, DefaultRevisionEntity> getRevisions(long id) {
89                 Session s = hibernateUtil.getSession();
90                 AuditReader reader = AuditReaderFactory.get(s);
91                 List<Number> revisionNums = reader.getRevisions( DataPoint.class, id );
92                 return reader.findRevisions( DefaultRevisionEntity.class, new HashSet<Number>(revisionNums) );
93         }
94
95         public void deleteAll() {
96                 Session s = hibernateUtil.getSession();
97                 s.getTransaction().begin();
98                 s.createQuery( "delete from DataPoint" ).executeUpdate();
99                 s.getTransaction().commit();
100                 s.close();
101         }
102
103 }