Skip to content

Commit 885ec87

Browse files
committed
Moving towards Service Layer example.
1 parent 08c3145 commit 885ec87

File tree

14 files changed

+84
-42
lines changed

14 files changed

+84
-42
lines changed
Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
package com.iluwatar;
22

3-
import java.util.List;
43

54

65
public class App {
76
public static void main( String[] args ) {
8-
WizardDao dao = new WizardDao();
7+
WizardDaoImpl dao = new WizardDaoImpl();
98
persistData(dao);
10-
queryData(dao);
9+
queryData();
1110
}
1211

13-
public static void persistData(WizardDao dao) {
12+
public static void persistData(WizardDaoImpl dao) {
1413
Spell spell = new Spell("Fireball");
1514
Spellbook spellbook = new Spellbook("Book of fire");
1615
spell.setSpellbook(spellbook);
@@ -21,16 +20,10 @@ public static void persistData(WizardDao dao) {
2120
dao.persist(wizard);
2221
}
2322

24-
public static void queryData(WizardDao dao) {
25-
List<Wizard> wizards = dao.findAll();
26-
for (Wizard w: wizards) {
23+
public static void queryData() {
24+
MagicService magicService = new MagicServiceImpl();
25+
for (Wizard w: magicService.findAllWizards()) {
2726
System.out.println(w);
28-
for (Spellbook spellbook: w.getSpellbooks()) {
29-
System.out.println(spellbook);
30-
for (Spell spell: spellbook.getSpells()) {
31-
System.out.println(spell);
32-
}
33-
}
3427
}
3528
}
3629
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.iluwatar;
2+
3+
import java.util.List;
4+
5+
public interface Dao<E extends BaseEntity> {
6+
7+
E find(Long id);
8+
9+
void persist(E entity);
10+
11+
E merge(E entity);
12+
13+
void delete(E entity);
14+
15+
List<E> findAll();
16+
}

dao/src/main/java/com/iluwatar/DaoBase.java renamed to dao/src/main/java/com/iluwatar/DaoBaseImpl.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import org.hibernate.cfg.Configuration;
1111
import org.hibernate.criterion.Restrictions;
1212

13-
public abstract class DaoBase<E extends BaseEntity> {
13+
public abstract class DaoBaseImpl<E extends BaseEntity> implements Dao<E> {
1414

1515
@SuppressWarnings("unchecked")
1616
protected Class<E> persistentClass = (Class<E>) ((ParameterizedType) getClass()
@@ -36,7 +36,8 @@ private Session getSession() {
3636
return sessionFactory.openSession();
3737
}
3838

39-
E find(Long id) {
39+
@Override
40+
public E find(Long id) {
4041
Session session = getSession();
4142
Transaction tx = null;
4243
E result = null;
@@ -57,7 +58,8 @@ E find(Long id) {
5758
return result;
5859
}
5960

60-
void persist(E entity) {
61+
@Override
62+
public void persist(E entity) {
6163
Session session = getSession();
6264
Transaction tx = null;
6365
try {
@@ -74,7 +76,8 @@ void persist(E entity) {
7476
}
7577
}
7678

77-
E merge(E entity) {
79+
@Override
80+
public E merge(E entity) {
7881
Session session = getSession();
7982
Transaction tx = null;
8083
E result = null;
@@ -93,7 +96,8 @@ E merge(E entity) {
9396
return result;
9497
}
9598

96-
void delete(E entity) {
99+
@Override
100+
public void delete(E entity) {
97101
Session session = getSession();
98102
Transaction tx = null;
99103
try {
@@ -110,7 +114,8 @@ void delete(E entity) {
110114
}
111115
}
112116

113-
List<E> findAll() {
117+
@Override
118+
public List<E> findAll() {
114119
Session session = getSession();
115120
Transaction tx = null;
116121
List<E> result = null;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.iluwatar;
2+
3+
import java.util.List;
4+
5+
public interface MagicService {
6+
7+
List<Wizard> findAllWizards();
8+
9+
List<Spellbook> findAllSpellbooks();
10+
11+
List<Spell> findAllSpells();
12+
13+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.iluwatar;
2+
3+
import java.util.List;
4+
5+
public class MagicServiceImpl implements MagicService {
6+
7+
@Override
8+
public List<Wizard> findAllWizards() {
9+
return new WizardDaoImpl().findAll();
10+
}
11+
12+
@Override
13+
public List<Spellbook> findAllSpellbooks() {
14+
return new SpellbookDaoImpl().findAll();
15+
}
16+
17+
@Override
18+
public List<Spell> findAllSpells() {
19+
return new SpellDaoImpl().findAll();
20+
}
21+
}

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
import javax.persistence.Entity;
55
import javax.persistence.GeneratedValue;
66
import javax.persistence.Id;
7-
import javax.persistence.Inheritance;
8-
import javax.persistence.InheritanceType;
97
import javax.persistence.JoinColumn;
108
import javax.persistence.ManyToOne;
119
import javax.persistence.Table;

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

Lines changed: 0 additions & 5 deletions
This file was deleted.
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 SpellDaoImpl extends DaoBaseImpl<Spell> {
4+
5+
}

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
import javax.persistence.FetchType;
1010
import javax.persistence.GeneratedValue;
1111
import javax.persistence.Id;
12-
import javax.persistence.Inheritance;
13-
import javax.persistence.InheritanceType;
1412
import javax.persistence.JoinColumn;
1513
import javax.persistence.ManyToOne;
1614
import javax.persistence.OneToMany;
@@ -48,7 +46,7 @@ public void setId(Long id) {
4846
@JoinColumn(name="WIZARD_ID_FK", referencedColumnName="WIZARD_ID")
4947
private Wizard wizard;
5048

51-
@OneToMany(mappedBy = "spellbook", orphanRemoval = true, fetch = FetchType.EAGER, cascade = CascadeType.ALL)
49+
@OneToMany(mappedBy = "spellbook", orphanRemoval = true, fetch = FetchType.LAZY, cascade = CascadeType.ALL)
5250
private Set<Spell> spells;
5351

5452
public String getName() {

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

Lines changed: 0 additions & 5 deletions
This file was deleted.

0 commit comments

Comments
 (0)