Skip to content

Commit 7df4774

Browse files
committed
Work on data initialization.
1 parent 94235d7 commit 7df4774

11 files changed

+144
-56
lines changed
Lines changed: 105 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,118 @@
11
package com.iluwatar;
22

3+
import java.util.List;
34

4-
5+
/**
6+
*
7+
*
8+
*
9+
*/
510
public class App {
11+
612
public static void main( String[] args ) {
7-
WizardDaoImpl dao = new WizardDaoImpl();
8-
persistData(dao);
13+
initData();
914
queryData();
1015
}
1116

12-
public static void persistData(WizardDaoImpl dao) {
13-
Spell spell = new Spell("Fireball");
14-
Spellbook spellbook = new Spellbook("Book of fire");
15-
spell.setSpellbook(spellbook);
16-
spellbook.getSpells().add(spell);
17-
Wizard wizard = new Wizard("Jugga");
18-
spellbook.setWizard(wizard);
19-
wizard.getSpellbooks().add(spellbook);
20-
dao.persist(wizard);
17+
public static void initData() {
18+
Spell spell1 = new Spell("Ice dart");
19+
Spell spell2 = new Spell("Invisibility");
20+
Spell spell3 = new Spell("Stun bolt");
21+
Spell spell4 = new Spell("Confusion");
22+
Spell spell5 = new Spell("Darkness");
23+
Spell spell6 = new Spell("Fireball");
24+
Spell spell7 = new Spell("Enchant weapon");
25+
Spell spell8 = new Spell("Rock armour");
26+
Spell spell9 = new Spell("Light");
27+
Spell spell10 = new Spell("Bee swarm");
28+
Spell spell11 = new Spell("Haste");
29+
Spell spell12 = new Spell("Levitation");
30+
Spell spell13 = new Spell("Magic lock");
31+
Spell spell14 = new Spell("Summon hell bat");
32+
Spell spell15 = new Spell("Water walking");
33+
Spell spell16 = new Spell("Magic storm");
34+
Spell spell17 = new Spell("Entangle");
35+
SpellDao spellDao = new SpellDaoImpl();
36+
spellDao.persist(spell1);
37+
spellDao.persist(spell2);
38+
spellDao.persist(spell3);
39+
spellDao.persist(spell4);
40+
spellDao.persist(spell5);
41+
spellDao.persist(spell6);
42+
spellDao.persist(spell7);
43+
spellDao.persist(spell8);
44+
spellDao.persist(spell9);
45+
spellDao.persist(spell10);
46+
spellDao.persist(spell11);
47+
spellDao.persist(spell12);
48+
spellDao.persist(spell13);
49+
spellDao.persist(spell14);
50+
spellDao.persist(spell15);
51+
spellDao.persist(spell16);
52+
spellDao.persist(spell17);
53+
54+
SpellbookDao spellbookDao = new SpellbookDaoImpl();
55+
Spellbook spellbook1 = new Spellbook("Book of Orgymon");
56+
spellbookDao.persist(spellbook1);
57+
spellbook1.addSpell(spell1);
58+
spellbook1.addSpell(spell2);
59+
spellbook1.addSpell(spell3);
60+
spellbook1.addSpell(spell4);
61+
spellbookDao.merge(spellbook1);
62+
Spellbook spellbook2 = new Spellbook("Book of Aras");
63+
spellbookDao.persist(spellbook2);
64+
spellbook2.addSpell(spell5);
65+
spellbook2.addSpell(spell6);
66+
spellbookDao.merge(spellbook2);
67+
Spellbook spellbook3 = new Spellbook("Book of Kritior");
68+
spellbookDao.persist(spellbook3);
69+
spellbook3.addSpell(spell7);
70+
spellbook3.addSpell(spell8);
71+
spellbook3.addSpell(spell9);
72+
spellbookDao.merge(spellbook3);
73+
Spellbook spellbook4 = new Spellbook("Book of Tamaex");
74+
spellbookDao.persist(spellbook4);
75+
spellbook4.addSpell(spell10);
76+
spellbook4.addSpell(spell11);
77+
spellbook4.addSpell(spell12);
78+
spellbookDao.merge(spellbook4);
79+
Spellbook spellbook5 = new Spellbook("Book of Idores");
80+
spellbookDao.persist(spellbook5);
81+
spellbook5.addSpell(spell13);
82+
spellbookDao.merge(spellbook5);
83+
Spellbook spellbook6 = new Spellbook("Book of Opaen");
84+
spellbookDao.persist(spellbook6);
85+
spellbook6.addSpell(spell14);
86+
spellbook6.addSpell(spell15);
87+
spellbookDao.merge(spellbook6);
88+
Spellbook spellbook7 = new Spellbook("Book of Kihione");
89+
spellbookDao.persist(spellbook7);
90+
spellbook7.addSpell(spell16);
91+
spellbook7.addSpell(spell17);
92+
spellbookDao.merge(spellbook7);
93+
94+
WizardDao wizardDao = new WizardDaoImpl();
95+
Wizard wizard1 = new Wizard("Aderlard Boud");
96+
wizardDao.persist(wizard1);
97+
// wizard1.addSpellbook(spellbook1);
98+
// wizard1.addSpellbook(spellbook2);
99+
wizardDao.merge(wizard1);
100+
Wizard wizard2 = new Wizard("Anaxis Bajraktari");
101+
wizardDao.persist(wizard2);
102+
// wizard2.addSpellbook(spellbook3);
103+
// wizard2.addSpellbook(spellbook4);
104+
wizardDao.merge(wizard2);
105+
Wizard wizard3 = new Wizard("Xuban Munoa");
106+
wizardDao.persist(wizard3);
107+
// wizard3.addSpellbook(spellbook5);
108+
// wizard3.addSpellbook(spellbook6);
109+
wizardDao.merge(wizard3);
110+
Wizard wizard4 = new Wizard("Blasius Dehooge");
111+
wizardDao.persist(wizard4);
112+
// wizard4.addSpellbook(spellbook7);
113+
wizardDao.merge(wizard4);
21114
}
22115

23116
public static void queryData() {
24-
MagicService magicService = new MagicServiceImpl();
25-
for (Wizard w: magicService.findAllWizards()) {
26-
System.out.println(w);
27-
}
28117
}
29118
}
Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,6 @@
11
package com.iluwatar;
22

3-
import java.util.List;
43

54
public interface MagicService {
6-
7-
List<Wizard> findAllWizards();
8-
9-
List<Spellbook> findAllSpellbooks();
10-
11-
List<Spell> findAllSpells();
125

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

3-
import java.util.List;
4-
53
public class MagicServiceImpl implements MagicService {
64

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-
}
215
}
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 interface SpellDao extends Dao<Spell> {
4+
5+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
package com.iluwatar;
22

3-
public class SpellDaoImpl extends DaoBaseImpl<Spell> {
3+
public class SpellDaoImpl extends DaoBaseImpl<Spell> implements SpellDao {
44

55
}

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

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,9 @@
66
import javax.persistence.CascadeType;
77
import javax.persistence.Column;
88
import javax.persistence.Entity;
9-
import javax.persistence.FetchType;
109
import javax.persistence.GeneratedValue;
1110
import javax.persistence.Id;
12-
import javax.persistence.JoinColumn;
13-
import javax.persistence.ManyToOne;
11+
import javax.persistence.ManyToMany;
1412
import javax.persistence.OneToMany;
1513
import javax.persistence.Table;
1614

@@ -20,6 +18,7 @@ public class Spellbook extends BaseEntity {
2018

2119
public Spellbook() {
2220
spells = new HashSet<Spell>();
21+
wizards = new HashSet<Wizard>();
2322
}
2423

2524
public Spellbook(String name) {
@@ -42,11 +41,10 @@ public void setId(Long id) {
4241

4342
private String name;
4443

45-
@ManyToOne
46-
@JoinColumn(name="WIZARD_ID_FK", referencedColumnName="WIZARD_ID")
47-
private Wizard wizard;
44+
@ManyToMany(mappedBy = "spellbooks")
45+
private Set<Wizard> wizards;
4846

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

5250
public String getName() {
@@ -57,12 +55,12 @@ public void setName(String name) {
5755
this.name = name;
5856
}
5957

60-
public Wizard getWizard() {
61-
return wizard;
58+
public Set<Wizard> getWizards() {
59+
return wizards;
6260
}
6361

64-
public void setWizard(Wizard wizard) {
65-
this.wizard = wizard;
62+
public void setWizards(Set<Wizard> wizards) {
63+
this.wizards = wizards;
6664
}
6765

6866
public Set<Spell> getSpells() {
@@ -72,6 +70,11 @@ public Set<Spell> getSpells() {
7270
public void setSpells(Set<Spell> spells) {
7371
this.spells = spells;
7472
}
73+
74+
public void addSpell(Spell spell) {
75+
spell.setSpellbook(this);
76+
spells.add(spell);
77+
}
7578

7679
@Override
7780
public String toString() {
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 interface SpellbookDao extends Dao<Spellbook> {
4+
5+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
package com.iluwatar;
22

3-
public class SpellbookDaoImpl extends DaoBaseImpl<Spellbook> {
3+
public class SpellbookDaoImpl extends DaoBaseImpl<Spellbook> implements SpellbookDao {
44

55
}

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@
66
import javax.persistence.CascadeType;
77
import javax.persistence.Column;
88
import javax.persistence.Entity;
9-
import javax.persistence.FetchType;
109
import javax.persistence.GeneratedValue;
1110
import javax.persistence.Id;
12-
import javax.persistence.OneToMany;
11+
import javax.persistence.ManyToMany;
1312
import javax.persistence.Table;
1413

1514
@Entity
@@ -40,7 +39,7 @@ public void setId(Long id) {
4039

4140
private String name;
4241

43-
@OneToMany(mappedBy = "wizard", orphanRemoval = true, fetch = FetchType.LAZY, cascade = CascadeType.ALL)
42+
@ManyToMany(cascade = CascadeType.ALL)
4443
private Set<Spellbook> spellbooks;
4544

4645
public String getFirstName() {
@@ -59,6 +58,11 @@ public void setSpellbooks(Set<Spellbook> spellbooks) {
5958
this.spellbooks = spellbooks;
6059
}
6160

61+
public void addSpellbook(Spellbook spellbook) {
62+
spellbook.getWizards().add(this);
63+
spellbooks.add(spellbook);
64+
}
65+
6266
@Override
6367
public String toString() {
6468
return name;
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 interface WizardDao extends Dao<Wizard> {
4+
5+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
package com.iluwatar;
22

3-
public class WizardDaoImpl extends DaoBaseImpl<Wizard> {
3+
public class WizardDaoImpl extends DaoBaseImpl<Wizard> implements WizardDao {
44

55
}

0 commit comments

Comments
 (0)