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 126 127
| public abstract class AbstractHibernateDAO<T, ID extends Serializable> extends HibernateDaoSupport implements GenericDAO<T, ID> {
private Class<T> persistentClass;
public AbstractHibernateDAO() {
this.persistentClass = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
}
public Class<T> getPersistentClass() {
return persistentClass;
}
@SuppressWarnings("unchecked")
public T getById(ID id) {
return (T) getSession().get(getPersistentClass(), id);
}
@SuppressWarnings("unchecked")
public T getById(ID id, boolean lock) {
if (lock) {
return (T) getSession().get(getPersistentClass(), id,
LockMode.UPGRADE);
} else
return getById(id);
}
@SuppressWarnings("unchecked")
public T loadById(ID id) {
return (T) getSession().load(getPersistentClass(), id);
}
public void save(T entity) {
getSession().save(entity);
}
public void update(T entity) {
getSession().update(entity);
}
public void saveOrUpdate(T entity) {
getSession().saveOrUpdate(entity);
}
public void delete(T entity) {
getSession().delete(entity);
}
public void deleteById(ID id) {
getSession().delete(loadById(id));
}
@SuppressWarnings("unchecked")
public List<T> findAll() {
return findByCriteria();
}
/**
* Use this inside subclasses as a convenience method.
*/
@SuppressWarnings("unchecked")
protected List<T> findByCriteria(Criterion... criterion) {
Criteria crit = getSession().createCriteria(getPersistentClass());
for (Criterion c : criterion) {
crit.add(c);
}
return crit.list();
}
/**
* Find by criteria.
*/
@SuppressWarnings("unchecked")
public List<T> findByCriteria(Map criterias) {
Criteria criteria = getSession().createCriteria(getPersistentClass());
criteria.add(Restrictions.allEq(criterias));
return criteria.list();
}
/**
* This method will execute an HQL query and return the number of affected entities.
*/
protected int executeQuery(String query, String namedParams[], Object params[]) {
Query q = getSession().createQuery(query);
if (namedParams != null) {
for (int i = 0; i < namedParams.length; i++) {
q.setParameter(namedParams[i], params[i]);
}
}
return q.executeUpdate();
}
protected int executeQuery(String query) {
return executeQuery(query, null, null);
}
/**
* This method will execute a Named HQL query and return the number of affected entities.
*/
protected int executeNamedQuery(String namedQuery, String namedParams[], Object params[]) {
Query q = getSession().getNamedQuery(namedQuery);
if (namedParams != null) {
for (int i = 0; i < namedParams.length; i++) {
q.setParameter(namedParams[i], params[i]);
}
}
return q.executeUpdate();
}
protected int executeNamedQuery(String namedQuery) {
return executeNamedQuery(namedQuery, null, null);
}
@SuppressWarnings("unchecked")
public List<T> findByExample(T exampleInstance, String[] excludeProperty) {
Criteria crit = getSession().createCriteria(getPersistentClass());
Example example = Example.create(exampleInstance).excludeZeroes().enableLike().ignoreCase();
for (String exclude : excludeProperty) {
example.excludeProperty(exclude);
}
crit.add(example);
return crit.list();
}} |
Partager