-
Notifications
You must be signed in to change notification settings - Fork 10
/
GenericDao.java
125 lines (111 loc) · 3.6 KB
/
GenericDao.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/**
* Copyright (C) 2011 JTalks.org Team
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
package org.jtalks.common.model.dao.hibernate;
import org.hibernate.SessionFactory;
import org.hibernate.classic.Session;
import org.jtalks.common.model.dao.Crud;
import org.jtalks.common.model.entity.Entity;
import java.lang.reflect.ParameterizedType;
/**
* Basic class for access to the specified {@link Entity} objects.
* Uses to load objects from database, save, update or delete them.
* The implementation is based on the Hibernate.
*
* @author Alexandre Teterin
*/
public class GenericDao<T extends Entity> implements Crud<T> {
/**
* Hibernate SessionFactory
*/
private final SessionFactory sessionFactory;
/**
* Type of entity
*/
private final Class<T> type;
/**
* @param sessionFactory The SessionFactory.
*/
public GenericDao(SessionFactory sessionFactory, Class<T> type) {
this.sessionFactory = sessionFactory;
this.type = type;
}
/**
* Get current Hibernate session.
*
* @return current Session
*/
public Session session() {
return sessionFactory.getCurrentSession();
}
/**
* Save or update entity.
* <p/>
* This operation cascades to associated instances if the association
* is mapped with cascade="save-update".
* This operation will result in INSERT SQL statement (for saving new entity),
* or in UPDATE statement for updating existing entity.
*
* @param entity object to save
*/
@Override
public void saveOrUpdate(T entity) {
session().saveOrUpdate(entity);
//todo this is workaround due our security solution on service layer,
// for further processing it require that entity must be persistent,
// after security solution refactoring the next line need to be removed for improving performance.
session().flush();
}
/**
* {@inheritDoc}
*/
@Override
public boolean delete(Long id) {
String deleteQuery = "delete " + type.getCanonicalName() + " e where e.id= :id";
return session().createQuery(deleteQuery).setLong("id", id).executeUpdate() != 0;
}
/**
* <p>Delete the entity by object reference.</p>
* <p>This method deletes all cascaded references.</p>
*
* @param entity Entity to be deleted.
* @throws {@link org.hibernate.StaleStateException} Throws exception if entity not exist.
*/
@Override
public void delete(T entity) {
session().delete(entity);
}
/**
* {@inheritDoc}
*/
@Override
@SuppressWarnings("unchecked")
public T get(Long id) {
return (T) session().get(type, id);
}
/**
* {@inheritDoc}
*/
@Override
public boolean isExist(Long id) {
return get(id) != null;
}
/**
* {@inheritDoc}
*/
@Override
public void flush() {
session().flush();
}
}