WIP:site
[hibernate4-maven-plugin] / src / it / tutorials-5.0.12 / osgi / unmanaged-jpa / 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.List;
24
25 import javax.persistence.EntityManager;
26
27 import org.hibernate.osgitest.entity.DataPoint;
28
29 /**
30  * @author Brett Meyer
31  */
32 public class DataPointServiceImpl implements DataPointService {
33         
34         private HibernateUtil hibernateUtil = new HibernateUtil();
35
36         public void add(DataPoint dp) {
37                 EntityManager em = hibernateUtil.getEntityManager();
38                 em.getTransaction().begin();
39                 em.persist( dp );
40                 em.getTransaction().commit();
41                 em.close();
42         }
43
44         public void update(DataPoint dp) {
45                 EntityManager em = hibernateUtil.getEntityManager();
46                 em.getTransaction().begin();
47                 em.merge( dp );
48                 em.getTransaction().commit();
49                 em.close();
50         }
51
52         public DataPoint get(long id) {
53                 EntityManager em = hibernateUtil.getEntityManager();
54                 em.getTransaction().begin();
55                 DataPoint dp = (DataPoint) em.createQuery( "from DataPoint dp where dp.id=" + id ).getSingleResult();
56                 em.getTransaction().commit();
57                 em.close();
58                 return dp;
59         }
60
61         public List<DataPoint> getAll() {
62                 EntityManager em = hibernateUtil.getEntityManager();
63                 em.getTransaction().begin();
64                 List list = em.createQuery( "from DataPoint" ).getResultList();
65                 em.getTransaction().commit();
66                 em.close();
67                 return list;
68         }
69
70         public void deleteAll() {
71                 EntityManager em = hibernateUtil.getEntityManager();
72                 em.getTransaction().begin();
73                 em.createQuery( "delete from DataPoint" ).executeUpdate();
74                 em.getTransaction().commit();
75                 em.close();
76         }
77
78 }