Work on data initialization.

This commit is contained in:
Ilkka Seppala 2015-04-14 23:38:50 +03:00
parent 94235d71a1
commit 7df4774d8f
11 changed files with 144 additions and 56 deletions

View File

@ -1,29 +1,118 @@
package com.iluwatar;
import java.util.List;
/**
*
*
*
*/
public class App {
public static void main( String[] args ) {
WizardDaoImpl dao = new WizardDaoImpl();
persistData(dao);
initData();
queryData();
}
public static void persistData(WizardDaoImpl dao) {
Spell spell = new Spell("Fireball");
Spellbook spellbook = new Spellbook("Book of fire");
spell.setSpellbook(spellbook);
spellbook.getSpells().add(spell);
Wizard wizard = new Wizard("Jugga");
spellbook.setWizard(wizard);
wizard.getSpellbooks().add(spellbook);
dao.persist(wizard);
public static void initData() {
Spell spell1 = new Spell("Ice dart");
Spell spell2 = new Spell("Invisibility");
Spell spell3 = new Spell("Stun bolt");
Spell spell4 = new Spell("Confusion");
Spell spell5 = new Spell("Darkness");
Spell spell6 = new Spell("Fireball");
Spell spell7 = new Spell("Enchant weapon");
Spell spell8 = new Spell("Rock armour");
Spell spell9 = new Spell("Light");
Spell spell10 = new Spell("Bee swarm");
Spell spell11 = new Spell("Haste");
Spell spell12 = new Spell("Levitation");
Spell spell13 = new Spell("Magic lock");
Spell spell14 = new Spell("Summon hell bat");
Spell spell15 = new Spell("Water walking");
Spell spell16 = new Spell("Magic storm");
Spell spell17 = new Spell("Entangle");
SpellDao spellDao = new SpellDaoImpl();
spellDao.persist(spell1);
spellDao.persist(spell2);
spellDao.persist(spell3);
spellDao.persist(spell4);
spellDao.persist(spell5);
spellDao.persist(spell6);
spellDao.persist(spell7);
spellDao.persist(spell8);
spellDao.persist(spell9);
spellDao.persist(spell10);
spellDao.persist(spell11);
spellDao.persist(spell12);
spellDao.persist(spell13);
spellDao.persist(spell14);
spellDao.persist(spell15);
spellDao.persist(spell16);
spellDao.persist(spell17);
SpellbookDao spellbookDao = new SpellbookDaoImpl();
Spellbook spellbook1 = new Spellbook("Book of Orgymon");
spellbookDao.persist(spellbook1);
spellbook1.addSpell(spell1);
spellbook1.addSpell(spell2);
spellbook1.addSpell(spell3);
spellbook1.addSpell(spell4);
spellbookDao.merge(spellbook1);
Spellbook spellbook2 = new Spellbook("Book of Aras");
spellbookDao.persist(spellbook2);
spellbook2.addSpell(spell5);
spellbook2.addSpell(spell6);
spellbookDao.merge(spellbook2);
Spellbook spellbook3 = new Spellbook("Book of Kritior");
spellbookDao.persist(spellbook3);
spellbook3.addSpell(spell7);
spellbook3.addSpell(spell8);
spellbook3.addSpell(spell9);
spellbookDao.merge(spellbook3);
Spellbook spellbook4 = new Spellbook("Book of Tamaex");
spellbookDao.persist(spellbook4);
spellbook4.addSpell(spell10);
spellbook4.addSpell(spell11);
spellbook4.addSpell(spell12);
spellbookDao.merge(spellbook4);
Spellbook spellbook5 = new Spellbook("Book of Idores");
spellbookDao.persist(spellbook5);
spellbook5.addSpell(spell13);
spellbookDao.merge(spellbook5);
Spellbook spellbook6 = new Spellbook("Book of Opaen");
spellbookDao.persist(spellbook6);
spellbook6.addSpell(spell14);
spellbook6.addSpell(spell15);
spellbookDao.merge(spellbook6);
Spellbook spellbook7 = new Spellbook("Book of Kihione");
spellbookDao.persist(spellbook7);
spellbook7.addSpell(spell16);
spellbook7.addSpell(spell17);
spellbookDao.merge(spellbook7);
WizardDao wizardDao = new WizardDaoImpl();
Wizard wizard1 = new Wizard("Aderlard Boud");
wizardDao.persist(wizard1);
// wizard1.addSpellbook(spellbook1);
// wizard1.addSpellbook(spellbook2);
wizardDao.merge(wizard1);
Wizard wizard2 = new Wizard("Anaxis Bajraktari");
wizardDao.persist(wizard2);
// wizard2.addSpellbook(spellbook3);
// wizard2.addSpellbook(spellbook4);
wizardDao.merge(wizard2);
Wizard wizard3 = new Wizard("Xuban Munoa");
wizardDao.persist(wizard3);
// wizard3.addSpellbook(spellbook5);
// wizard3.addSpellbook(spellbook6);
wizardDao.merge(wizard3);
Wizard wizard4 = new Wizard("Blasius Dehooge");
wizardDao.persist(wizard4);
// wizard4.addSpellbook(spellbook7);
wizardDao.merge(wizard4);
}
public static void queryData() {
MagicService magicService = new MagicServiceImpl();
for (Wizard w: magicService.findAllWizards()) {
System.out.println(w);
}
}
}

View File

@ -1,13 +1,6 @@
package com.iluwatar;
import java.util.List;
public interface MagicService {
List<Wizard> findAllWizards();
List<Spellbook> findAllSpellbooks();
List<Spell> findAllSpells();
}

View File

@ -1,21 +1,5 @@
package com.iluwatar;
import java.util.List;
public class MagicServiceImpl implements MagicService {
@Override
public List<Wizard> findAllWizards() {
return new WizardDaoImpl().findAll();
}
@Override
public List<Spellbook> findAllSpellbooks() {
return new SpellbookDaoImpl().findAll();
}
@Override
public List<Spell> findAllSpells() {
return new SpellDaoImpl().findAll();
}
}

View File

@ -0,0 +1,5 @@
package com.iluwatar;
public interface SpellDao extends Dao<Spell> {
}

View File

@ -1,5 +1,5 @@
package com.iluwatar;
public class SpellDaoImpl extends DaoBaseImpl<Spell> {
public class SpellDaoImpl extends DaoBaseImpl<Spell> implements SpellDao {
}

View File

@ -6,11 +6,9 @@ import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.Table;
@ -20,6 +18,7 @@ public class Spellbook extends BaseEntity {
public Spellbook() {
spells = new HashSet<Spell>();
wizards = new HashSet<Wizard>();
}
public Spellbook(String name) {
@ -42,11 +41,10 @@ public class Spellbook extends BaseEntity {
private String name;
@ManyToOne
@JoinColumn(name="WIZARD_ID_FK", referencedColumnName="WIZARD_ID")
private Wizard wizard;
@ManyToMany(mappedBy = "spellbooks")
private Set<Wizard> wizards;
@OneToMany(mappedBy = "spellbook", orphanRemoval = true, fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@OneToMany(mappedBy = "spellbook", orphanRemoval = true, cascade = CascadeType.ALL)
private Set<Spell> spells;
public String getName() {
@ -57,12 +55,12 @@ public class Spellbook extends BaseEntity {
this.name = name;
}
public Wizard getWizard() {
return wizard;
public Set<Wizard> getWizards() {
return wizards;
}
public void setWizard(Wizard wizard) {
this.wizard = wizard;
public void setWizards(Set<Wizard> wizards) {
this.wizards = wizards;
}
public Set<Spell> getSpells() {
@ -72,6 +70,11 @@ public class Spellbook extends BaseEntity {
public void setSpells(Set<Spell> spells) {
this.spells = spells;
}
public void addSpell(Spell spell) {
spell.setSpellbook(this);
spells.add(spell);
}
@Override
public String toString() {

View File

@ -0,0 +1,5 @@
package com.iluwatar;
public interface SpellbookDao extends Dao<Spellbook> {
}

View File

@ -1,5 +1,5 @@
package com.iluwatar;
public class SpellbookDaoImpl extends DaoBaseImpl<Spellbook> {
public class SpellbookDaoImpl extends DaoBaseImpl<Spellbook> implements SpellbookDao {
}

View File

@ -6,10 +6,9 @@ import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
@Entity
@ -40,7 +39,7 @@ public class Wizard extends BaseEntity {
private String name;
@OneToMany(mappedBy = "wizard", orphanRemoval = true, fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@ManyToMany(cascade = CascadeType.ALL)
private Set<Spellbook> spellbooks;
public String getFirstName() {
@ -59,6 +58,11 @@ public class Wizard extends BaseEntity {
this.spellbooks = spellbooks;
}
public void addSpellbook(Spellbook spellbook) {
spellbook.getWizards().add(this);
spellbooks.add(spellbook);
}
@Override
public String toString() {
return name;

View File

@ -0,0 +1,5 @@
package com.iluwatar;
public interface WizardDao extends Dao<Wizard> {
}

View File

@ -1,5 +1,5 @@
package com.iluwatar;
public class WizardDaoImpl extends DaoBaseImpl<Wizard> {
public class WizardDaoImpl extends DaoBaseImpl<Wizard> implements WizardDao {
}