Skip to content

Commit 63b22c1

Browse files
committed
Data fixture now working.
1 parent 7df4774 commit 63b22c1

File tree

8 files changed

+96
-11
lines changed

8 files changed

+96
-11
lines changed

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

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.iluwatar;
22

3-
import java.util.List;
43

54
/**
65
*
@@ -94,22 +93,22 @@ public static void initData() {
9493
WizardDao wizardDao = new WizardDaoImpl();
9594
Wizard wizard1 = new Wizard("Aderlard Boud");
9695
wizardDao.persist(wizard1);
97-
// wizard1.addSpellbook(spellbook1);
98-
// wizard1.addSpellbook(spellbook2);
96+
wizard1.addSpellbook(spellbookDao.findByName("Book of Orgymon"));
97+
wizard1.addSpellbook(spellbookDao.findByName("Book of Aras"));
9998
wizardDao.merge(wizard1);
10099
Wizard wizard2 = new Wizard("Anaxis Bajraktari");
101100
wizardDao.persist(wizard2);
102-
// wizard2.addSpellbook(spellbook3);
103-
// wizard2.addSpellbook(spellbook4);
101+
wizard2.addSpellbook(spellbookDao.findByName("Book of Kritior"));
102+
wizard2.addSpellbook(spellbookDao.findByName("Book of Tamaex"));
104103
wizardDao.merge(wizard2);
105104
Wizard wizard3 = new Wizard("Xuban Munoa");
106105
wizardDao.persist(wizard3);
107-
// wizard3.addSpellbook(spellbook5);
108-
// wizard3.addSpellbook(spellbook6);
106+
wizard3.addSpellbook(spellbookDao.findByName("Book of Idores"));
107+
wizard3.addSpellbook(spellbookDao.findByName("Book of Opaen"));
109108
wizardDao.merge(wizard3);
110109
Wizard wizard4 = new Wizard("Blasius Dehooge");
111110
wizardDao.persist(wizard4);
112-
// wizard4.addSpellbook(spellbook7);
111+
wizard4.addSpellbook(spellbookDao.findByName("Book of Kihione"));
113112
wizardDao.merge(wizard4);
114113
}
115114

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55

66
import org.hibernate.Criteria;
77
import org.hibernate.Session;
8-
import org.hibernate.SessionFactory;
98
import org.hibernate.Transaction;
10-
import org.hibernate.cfg.Configuration;
119
import org.hibernate.criterion.Restrictions;
1210

1311
public abstract class DaoBaseImpl<E extends BaseEntity> implements Dao<E> {
@@ -16,7 +14,7 @@ public abstract class DaoBaseImpl<E extends BaseEntity> implements Dao<E> {
1614
protected Class<E> persistentClass = (Class<E>) ((ParameterizedType) getClass()
1715
.getGenericSuperclass()).getActualTypeArguments()[0];
1816

19-
private Session getSession() {
17+
protected Session getSession() {
2018
return HibernateUtil.getSessionFactory().openSession();
2119
}
2220

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.iluwatar;
22

33
public interface SpellDao extends Dao<Spell> {
4+
5+
Spell findByName(String name);
46

57
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,31 @@
11
package com.iluwatar;
22

3+
import org.hibernate.Criteria;
4+
import org.hibernate.Session;
5+
import org.hibernate.Transaction;
6+
import org.hibernate.criterion.Expression;
7+
38
public class SpellDaoImpl extends DaoBaseImpl<Spell> implements SpellDao {
49

10+
@Override
11+
public Spell findByName(String name) {
12+
Session session = getSession();
13+
Transaction tx = null;
14+
Spell result = null;
15+
try {
16+
tx = session.beginTransaction();
17+
Criteria criteria = session.createCriteria(persistentClass);
18+
criteria.add(Expression.eq("name", name));
19+
result = (Spell) criteria.uniqueResult();
20+
tx.commit();
21+
}
22+
catch (Exception e) {
23+
if (tx!=null) tx.rollback();
24+
throw e;
25+
}
26+
finally {
27+
session.close();
28+
}
29+
return result;
30+
}
531
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.iluwatar;
22

33
public interface SpellbookDao extends Dao<Spellbook> {
4+
5+
Spellbook findByName(String name);
46

57
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,34 @@
11
package com.iluwatar;
22

3+
import org.hibernate.Criteria;
4+
import org.hibernate.Session;
5+
import org.hibernate.Transaction;
6+
import org.hibernate.criterion.Expression;
7+
38
public class SpellbookDaoImpl extends DaoBaseImpl<Spellbook> implements SpellbookDao {
49

10+
@Override
11+
public Spellbook findByName(String name) {
12+
Session session = getSession();
13+
Transaction tx = null;
14+
Spellbook result = null;
15+
try {
16+
tx = session.beginTransaction();
17+
Criteria criteria = session.createCriteria(persistentClass);
18+
criteria.add(Expression.eq("name", name));
19+
result = (Spellbook) criteria.uniqueResult();
20+
result.getSpells().size();
21+
result.getWizards().size();
22+
tx.commit();
23+
}
24+
catch (Exception e) {
25+
if (tx!=null) tx.rollback();
26+
throw e;
27+
}
28+
finally {
29+
session.close();
30+
}
31+
return result;
32+
}
33+
534
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.iluwatar;
22

33
public interface WizardDao extends Dao<Wizard> {
4+
5+
Wizard findByName(String name);
46

57
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,32 @@
11
package com.iluwatar;
22

3+
import org.hibernate.Criteria;
4+
import org.hibernate.Session;
5+
import org.hibernate.Transaction;
6+
import org.hibernate.criterion.Expression;
7+
38
public class WizardDaoImpl extends DaoBaseImpl<Wizard> implements WizardDao {
49

10+
@Override
11+
public Wizard findByName(String name) {
12+
Session session = getSession();
13+
Transaction tx = null;
14+
Wizard result = null;
15+
try {
16+
tx = session.beginTransaction();
17+
Criteria criteria = session.createCriteria(persistentClass);
18+
criteria.add(Expression.eq("name", name));
19+
result = (Wizard) criteria.uniqueResult();
20+
result.getSpellbooks().size();
21+
tx.commit();
22+
}
23+
catch (Exception e) {
24+
if (tx!=null) tx.rollback();
25+
throw e;
26+
}
27+
finally {
28+
session.close();
29+
}
30+
return result;
31+
}
532
}

0 commit comments

Comments
 (0)