|
1 | 1 | package com.iluwatar; |
2 | 2 |
|
| 3 | +import java.lang.reflect.ParameterizedType; |
| 4 | +import java.util.List; |
| 5 | + |
| 6 | +import org.hibernate.Criteria; |
| 7 | +import org.hibernate.Session; |
| 8 | +import org.hibernate.SessionFactory; |
| 9 | +import org.hibernate.Transaction; |
| 10 | +import org.hibernate.cfg.Configuration; |
| 11 | +import org.hibernate.criterion.Restrictions; |
| 12 | + |
3 | 13 | public abstract class DaoBase<E extends BaseEntity> { |
| 14 | + |
| 15 | + @SuppressWarnings("unchecked") |
| 16 | + protected Class<E> persistentClass = (Class<E>) ((ParameterizedType) getClass() |
| 17 | + .getGenericSuperclass()).getActualTypeArguments()[0]; |
4 | 18 |
|
| 19 | + protected final SessionFactory sessionFactory = createSessionFactory(); |
| 20 | + |
| 21 | + private SessionFactory createSessionFactory() { |
| 22 | + SessionFactory sessionFactory = new Configuration() |
| 23 | + .addAnnotatedClass(Wizard.class) |
| 24 | + .addAnnotatedClass(Spellbook.class) |
| 25 | + .addAnnotatedClass(Spell.class) |
| 26 | + .setProperty("hibernate.dialect", "org.hibernate.dialect.H2Dialect") |
| 27 | + .setProperty("hibernate.connection.url", "jdbc:h2:mem:test;DB_CLOSE_DELAY=-1") |
| 28 | + .setProperty("hibernate.current_session_context_class", "thread") |
| 29 | + .setProperty("hibernate.show_sql", "true") |
| 30 | + .setProperty("hibernate.hbm2ddl.auto", "create") |
| 31 | + .buildSessionFactory(); |
| 32 | + return sessionFactory; |
| 33 | + } |
| 34 | + |
| 35 | + private Session getSession() { |
| 36 | + return sessionFactory.openSession(); |
| 37 | + } |
| 38 | + |
5 | 39 | E find(Long id) { |
6 | | - |
| 40 | + Session session = getSession(); |
| 41 | + Transaction tx = null; |
| 42 | + E result = null; |
| 43 | + try { |
| 44 | + tx = session.beginTransaction(); |
| 45 | + Criteria criteria = sessionFactory.getCurrentSession().createCriteria(persistentClass); |
| 46 | + criteria.add(Restrictions.idEq(id)); |
| 47 | + result = (E) criteria.uniqueResult(); |
| 48 | + tx.commit(); |
| 49 | + } |
| 50 | + catch (Exception e) { |
| 51 | + if (tx!=null) tx.rollback(); |
| 52 | + throw e; |
| 53 | + } |
| 54 | + finally { |
| 55 | + session.close(); |
| 56 | + } |
| 57 | + return result; |
7 | 58 | } |
8 | 59 |
|
9 | | - void persist(E e) { |
10 | | - |
| 60 | + void persist(E entity) { |
| 61 | + Session session = getSession(); |
| 62 | + Transaction tx = null; |
| 63 | + try { |
| 64 | + tx = session.beginTransaction(); |
| 65 | + session.persist(entity); |
| 66 | + tx.commit(); |
| 67 | + } |
| 68 | + catch (Exception e) { |
| 69 | + if (tx!=null) tx.rollback(); |
| 70 | + throw e; |
| 71 | + } |
| 72 | + finally { |
| 73 | + session.close(); |
| 74 | + } |
11 | 75 | } |
12 | 76 |
|
13 | | - E merge(E e) { |
14 | | - |
| 77 | + E merge(E entity) { |
| 78 | + Session session = getSession(); |
| 79 | + Transaction tx = null; |
| 80 | + E result = null; |
| 81 | + try { |
| 82 | + tx = session.beginTransaction(); |
| 83 | + result = (E) session.merge(entity); |
| 84 | + tx.commit(); |
| 85 | + } |
| 86 | + catch (Exception e) { |
| 87 | + if (tx!=null) tx.rollback(); |
| 88 | + throw e; |
| 89 | + } |
| 90 | + finally { |
| 91 | + session.close(); |
| 92 | + } |
| 93 | + return result; |
15 | 94 | } |
16 | 95 |
|
17 | | - void remove(E e) { |
18 | | - |
| 96 | + void delete(E entity) { |
| 97 | + Session session = getSession(); |
| 98 | + Transaction tx = null; |
| 99 | + try { |
| 100 | + tx = session.beginTransaction(); |
| 101 | + session.delete(entity); |
| 102 | + tx.commit(); |
| 103 | + } |
| 104 | + catch (Exception e) { |
| 105 | + if (tx!=null) tx.rollback(); |
| 106 | + throw e; |
| 107 | + } |
| 108 | + finally { |
| 109 | + session.close(); |
| 110 | + } |
19 | 111 | } |
20 | 112 |
|
21 | 113 | List<E> findAll() { |
22 | | - |
| 114 | + Session session = getSession(); |
| 115 | + Transaction tx = null; |
| 116 | + List<E> result = null; |
| 117 | + try { |
| 118 | + tx = session.beginTransaction(); |
| 119 | + Criteria criteria = session.createCriteria(persistentClass); |
| 120 | + result = criteria.list(); |
| 121 | + } |
| 122 | + catch (Exception e) { |
| 123 | + if (tx!=null) tx.rollback(); |
| 124 | + throw e; |
| 125 | + } |
| 126 | + finally { |
| 127 | + session.close(); |
| 128 | + } |
| 129 | + return result; |
23 | 130 | } |
24 | 131 | } |
0 commit comments