Skip to content

Commit 82eebea

Browse files
committed
Work on DAO example.
1 parent 0bded40 commit 82eebea

File tree

8 files changed

+187
-44
lines changed

8 files changed

+187
-44
lines changed

dao/src/main/java/com/iluwatar/App.java

Lines changed: 21 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,27 @@
22

33
import java.util.List;
44

5-
import org.hibernate.Query;
6-
import org.hibernate.Session;
7-
import org.hibernate.SessionFactory;
8-
import org.hibernate.cfg.Configuration;
95

10-
public class App
11-
{
12-
public static void main( String[] args ) {
13-
14-
SessionFactory sessionFactory = new Configuration()
15-
.addAnnotatedClass(Wizard.class)
16-
.addAnnotatedClass(Spellbook.class)
17-
.addAnnotatedClass(Spell.class)
18-
.setProperty("hibernate.dialect", "org.hibernate.dialect.H2Dialect")
19-
.setProperty("hibernate.connection.url", "jdbc:h2:mem:test;DB_CLOSE_DELAY=-1")
20-
.setProperty("hibernate.current_session_context_class", "thread")
21-
.setProperty("hibernate.show_sql", "true")
22-
.setProperty("hibernate.hbm2ddl.auto", "create")
23-
.buildSessionFactory();
24-
25-
Session session = sessionFactory.getCurrentSession();
26-
session.beginTransaction();
27-
Wizard wizard = new Wizard();
28-
wizard.setFirstName("Jugga");
29-
Spellbook spellbook = new Spellbook();
30-
Spell spell = new Spell();
31-
spellbook.getSpells().add(spell);
32-
wizard.getSpellbooks().add(spellbook);
33-
session.persist(wizard);
34-
Query query = session.createQuery("from Wizard");
35-
List<?> list = query.list();
36-
Wizard p1 = (Wizard) list.get(0);
37-
System.out.println(String.format("id=%d firstName=%s", p1.getId(), p1.getFirstName()));
38-
session.getTransaction().commit();
6+
public class App {
7+
public static void main( String[] args ) {
8+
WizardDao dao = new WizardDao();
9+
persistData(dao);
10+
queryData(dao);
11+
}
12+
13+
public static void persistData(WizardDao dao) {
14+
Spell spell = new Spell("Fireball");
15+
Spellbook spellbook = new Spellbook("Book of fire");
16+
spellbook.getSpells().add(spell);
17+
Wizard wizard = new Wizard("Jugga");
18+
wizard.getSpellbooks().add(spellbook);
19+
dao.persist(wizard);
20+
}
21+
22+
public static void queryData(WizardDao dao) {
23+
List<Wizard> wizards = dao.findAll();
24+
for (Wizard w: wizards) {
25+
System.out.println(w);
26+
}
3927
}
4028
}
Lines changed: 115 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,131 @@
11
package com.iluwatar;
22

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+
313
public abstract class DaoBase<E extends BaseEntity> {
14+
15+
@SuppressWarnings("unchecked")
16+
protected Class<E> persistentClass = (Class<E>) ((ParameterizedType) getClass()
17+
.getGenericSuperclass()).getActualTypeArguments()[0];
418

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+
539
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;
758
}
859

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+
}
1175
}
1276

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;
1594
}
1695

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+
}
19111
}
20112

21113
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;
23130
}
24131
}

dao/src/main/java/com/iluwatar/Spell.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@
1010
public class Spell extends BaseEntity {
1111

1212
private String name;
13+
14+
public Spell() {
15+
}
16+
17+
public Spell(String name) {
18+
this();
19+
this.name = name;
20+
}
1321

1422
@ManyToOne
1523
@JoinColumn(name="SPELLBOOK_ID_FK", referencedColumnName="ID")
@@ -22,4 +30,9 @@ public String getName() {
2230
public void setName(String name) {
2331
this.name = name;
2432
}
33+
34+
@Override
35+
public String toString() {
36+
return name;
37+
}
2538
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.iluwatar;
2+
3+
public class SpellDao extends DaoBase<Spell> {
4+
5+
}

dao/src/main/java/com/iluwatar/Spellbook.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ public class Spellbook extends BaseEntity {
1717
public Spellbook() {
1818
spells = new HashSet<Spell>();
1919
}
20+
21+
public Spellbook(String name) {
22+
this();
23+
this.name = name;
24+
}
2025

2126
private String name;
2227

@@ -50,4 +55,9 @@ public Set<Spell> getSpells() {
5055
public void setSpells(Set<Spell> spells) {
5156
this.spells = spells;
5257
}
58+
59+
@Override
60+
public String toString() {
61+
return name;
62+
}
5363
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.iluwatar;
2+
3+
public class SpellbookDao extends DaoBase<Spellbook> {
4+
5+
}

dao/src/main/java/com/iluwatar/Wizard.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,22 @@ public Wizard() {
1616
spellbooks = new HashSet<Spellbook>();
1717
}
1818

19-
private String firstName;
19+
public Wizard(String name) {
20+
this();
21+
this.name = name;
22+
}
23+
24+
private String name;
2025

2126
@OneToMany(mappedBy = "wizard", orphanRemoval = true, cascade = CascadeType.ALL)
2227
private Set<Spellbook> spellbooks;
2328

2429
public String getFirstName() {
25-
return firstName;
30+
return name;
2631
}
2732

2833
public void setFirstName(String firstName) {
29-
this.firstName = firstName;
34+
this.name = firstName;
3035
}
3136

3237
public Set<Spellbook> getSpellbooks() {
@@ -35,5 +40,10 @@ public Set<Spellbook> getSpellbooks() {
3540

3641
public void setSpellbooks(Set<Spellbook> spellbooks) {
3742
this.spellbooks = spellbooks;
43+
}
44+
45+
@Override
46+
public String toString() {
47+
return name;
3848
}
3949
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.iluwatar;
2+
3+
public class WizardDao extends DaoBase<Wizard> {
4+
5+
}

0 commit comments

Comments
 (0)