2015-04-12 23:49:00 +03:00
|
|
|
package com.iluwatar;
|
|
|
|
|
2015-04-13 21:22:03 +03:00
|
|
|
import java.lang.reflect.ParameterizedType;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import org.hibernate.Criteria;
|
|
|
|
import org.hibernate.Session;
|
|
|
|
import org.hibernate.SessionFactory;
|
|
|
|
import org.hibernate.Transaction;
|
|
|
|
import org.hibernate.cfg.Configuration;
|
|
|
|
import org.hibernate.criterion.Restrictions;
|
|
|
|
|
2015-04-12 23:49:00 +03:00
|
|
|
public abstract class DaoBase<E extends BaseEntity> {
|
2015-04-13 21:22:03 +03:00
|
|
|
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
protected Class<E> persistentClass = (Class<E>) ((ParameterizedType) getClass()
|
|
|
|
.getGenericSuperclass()).getActualTypeArguments()[0];
|
2015-04-12 23:49:00 +03:00
|
|
|
|
2015-04-13 21:22:03 +03:00
|
|
|
protected final SessionFactory sessionFactory = createSessionFactory();
|
|
|
|
|
|
|
|
private SessionFactory createSessionFactory() {
|
|
|
|
SessionFactory sessionFactory = new Configuration()
|
|
|
|
.addAnnotatedClass(Wizard.class)
|
|
|
|
.addAnnotatedClass(Spellbook.class)
|
|
|
|
.addAnnotatedClass(Spell.class)
|
|
|
|
.setProperty("hibernate.dialect", "org.hibernate.dialect.H2Dialect")
|
|
|
|
.setProperty("hibernate.connection.url", "jdbc:h2:mem:test;DB_CLOSE_DELAY=-1")
|
|
|
|
.setProperty("hibernate.current_session_context_class", "thread")
|
|
|
|
.setProperty("hibernate.show_sql", "true")
|
2015-04-13 22:08:01 +03:00
|
|
|
.setProperty("hibernate.hbm2ddl.auto", "create-drop")
|
2015-04-13 21:22:03 +03:00
|
|
|
.buildSessionFactory();
|
|
|
|
return sessionFactory;
|
|
|
|
}
|
|
|
|
|
|
|
|
private Session getSession() {
|
|
|
|
return sessionFactory.openSession();
|
|
|
|
}
|
|
|
|
|
2015-04-12 23:49:00 +03:00
|
|
|
E find(Long id) {
|
2015-04-13 21:22:03 +03:00
|
|
|
Session session = getSession();
|
|
|
|
Transaction tx = null;
|
|
|
|
E result = null;
|
|
|
|
try {
|
|
|
|
tx = session.beginTransaction();
|
|
|
|
Criteria criteria = sessionFactory.getCurrentSession().createCriteria(persistentClass);
|
|
|
|
criteria.add(Restrictions.idEq(id));
|
|
|
|
result = (E) criteria.uniqueResult();
|
|
|
|
tx.commit();
|
|
|
|
}
|
|
|
|
catch (Exception e) {
|
|
|
|
if (tx!=null) tx.rollback();
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
finally {
|
|
|
|
session.close();
|
|
|
|
}
|
|
|
|
return result;
|
2015-04-12 23:49:00 +03:00
|
|
|
}
|
|
|
|
|
2015-04-13 21:22:03 +03:00
|
|
|
void persist(E entity) {
|
|
|
|
Session session = getSession();
|
|
|
|
Transaction tx = null;
|
|
|
|
try {
|
|
|
|
tx = session.beginTransaction();
|
|
|
|
session.persist(entity);
|
|
|
|
tx.commit();
|
|
|
|
}
|
|
|
|
catch (Exception e) {
|
|
|
|
if (tx!=null) tx.rollback();
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
finally {
|
|
|
|
session.close();
|
|
|
|
}
|
2015-04-12 23:49:00 +03:00
|
|
|
}
|
|
|
|
|
2015-04-13 21:22:03 +03:00
|
|
|
E merge(E entity) {
|
|
|
|
Session session = getSession();
|
|
|
|
Transaction tx = null;
|
|
|
|
E result = null;
|
|
|
|
try {
|
|
|
|
tx = session.beginTransaction();
|
|
|
|
result = (E) session.merge(entity);
|
|
|
|
tx.commit();
|
|
|
|
}
|
|
|
|
catch (Exception e) {
|
|
|
|
if (tx!=null) tx.rollback();
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
finally {
|
|
|
|
session.close();
|
|
|
|
}
|
|
|
|
return result;
|
2015-04-12 23:49:00 +03:00
|
|
|
}
|
|
|
|
|
2015-04-13 21:22:03 +03:00
|
|
|
void delete(E entity) {
|
|
|
|
Session session = getSession();
|
|
|
|
Transaction tx = null;
|
|
|
|
try {
|
|
|
|
tx = session.beginTransaction();
|
|
|
|
session.delete(entity);
|
|
|
|
tx.commit();
|
|
|
|
}
|
|
|
|
catch (Exception e) {
|
|
|
|
if (tx!=null) tx.rollback();
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
finally {
|
|
|
|
session.close();
|
|
|
|
}
|
2015-04-12 23:49:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
List<E> findAll() {
|
2015-04-13 21:22:03 +03:00
|
|
|
Session session = getSession();
|
|
|
|
Transaction tx = null;
|
|
|
|
List<E> result = null;
|
|
|
|
try {
|
|
|
|
tx = session.beginTransaction();
|
|
|
|
Criteria criteria = session.createCriteria(persistentClass);
|
|
|
|
result = criteria.list();
|
|
|
|
}
|
|
|
|
catch (Exception e) {
|
|
|
|
if (tx!=null) tx.rollback();
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
finally {
|
|
|
|
session.close();
|
|
|
|
}
|
|
|
|
return result;
|
2015-04-12 23:49:00 +03:00
|
|
|
}
|
|
|
|
}
|