2 * Hibernate, Relational Persistence for Idiomatic Java
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.
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,
21 package org.hibernate.osgitest;
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;
30 import java.util.HashSet;
31 import java.util.List;
37 public class DataPointServiceImpl implements DataPointService {
39 private HibernateUtil hibernateUtil = new HibernateUtil();
41 public void add(DataPoint dp) {
42 Session s = hibernateUtil.getSession();
43 s.getTransaction().begin();
45 s.getTransaction().commit();
49 public void update(DataPoint dp) {
50 Session s = hibernateUtil.getSession();
51 s.getTransaction().begin();
53 s.getTransaction().commit();
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();
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) );
74 s.getTransaction().commit();
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();
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) );
95 public void deleteAll() {
96 Session s = hibernateUtil.getSession();
97 s.getTransaction().begin();
98 s.createQuery( "delete from DataPoint" ).executeUpdate();
99 s.getTransaction().commit();