Data fixture now working.
This commit is contained in:
parent
7df4774d8f
commit
63b22c1749
@ -1,6 +1,5 @@
|
||||
package com.iluwatar;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
*
|
||||
@ -94,22 +93,22 @@ public class App {
|
||||
WizardDao wizardDao = new WizardDaoImpl();
|
||||
Wizard wizard1 = new Wizard("Aderlard Boud");
|
||||
wizardDao.persist(wizard1);
|
||||
// wizard1.addSpellbook(spellbook1);
|
||||
// wizard1.addSpellbook(spellbook2);
|
||||
wizard1.addSpellbook(spellbookDao.findByName("Book of Orgymon"));
|
||||
wizard1.addSpellbook(spellbookDao.findByName("Book of Aras"));
|
||||
wizardDao.merge(wizard1);
|
||||
Wizard wizard2 = new Wizard("Anaxis Bajraktari");
|
||||
wizardDao.persist(wizard2);
|
||||
// wizard2.addSpellbook(spellbook3);
|
||||
// wizard2.addSpellbook(spellbook4);
|
||||
wizard2.addSpellbook(spellbookDao.findByName("Book of Kritior"));
|
||||
wizard2.addSpellbook(spellbookDao.findByName("Book of Tamaex"));
|
||||
wizardDao.merge(wizard2);
|
||||
Wizard wizard3 = new Wizard("Xuban Munoa");
|
||||
wizardDao.persist(wizard3);
|
||||
// wizard3.addSpellbook(spellbook5);
|
||||
// wizard3.addSpellbook(spellbook6);
|
||||
wizard3.addSpellbook(spellbookDao.findByName("Book of Idores"));
|
||||
wizard3.addSpellbook(spellbookDao.findByName("Book of Opaen"));
|
||||
wizardDao.merge(wizard3);
|
||||
Wizard wizard4 = new Wizard("Blasius Dehooge");
|
||||
wizardDao.persist(wizard4);
|
||||
// wizard4.addSpellbook(spellbook7);
|
||||
wizard4.addSpellbook(spellbookDao.findByName("Book of Kihione"));
|
||||
wizardDao.merge(wizard4);
|
||||
}
|
||||
|
||||
|
@ -5,9 +5,7 @@ import java.util.List;
|
||||
|
||||
import org.hibernate.Criteria;
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.SessionFactory;
|
||||
import org.hibernate.Transaction;
|
||||
import org.hibernate.cfg.Configuration;
|
||||
import org.hibernate.criterion.Restrictions;
|
||||
|
||||
public abstract class DaoBaseImpl<E extends BaseEntity> implements Dao<E> {
|
||||
@ -16,7 +14,7 @@ public abstract class DaoBaseImpl<E extends BaseEntity> implements Dao<E> {
|
||||
protected Class<E> persistentClass = (Class<E>) ((ParameterizedType) getClass()
|
||||
.getGenericSuperclass()).getActualTypeArguments()[0];
|
||||
|
||||
private Session getSession() {
|
||||
protected Session getSession() {
|
||||
return HibernateUtil.getSessionFactory().openSession();
|
||||
}
|
||||
|
||||
|
@ -1,5 +1,7 @@
|
||||
package com.iluwatar;
|
||||
|
||||
public interface SpellDao extends Dao<Spell> {
|
||||
|
||||
Spell findByName(String name);
|
||||
|
||||
}
|
||||
|
@ -1,5 +1,31 @@
|
||||
package com.iluwatar;
|
||||
|
||||
import org.hibernate.Criteria;
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.Transaction;
|
||||
import org.hibernate.criterion.Expression;
|
||||
|
||||
public class SpellDaoImpl extends DaoBaseImpl<Spell> implements SpellDao {
|
||||
|
||||
@Override
|
||||
public Spell findByName(String name) {
|
||||
Session session = getSession();
|
||||
Transaction tx = null;
|
||||
Spell result = null;
|
||||
try {
|
||||
tx = session.beginTransaction();
|
||||
Criteria criteria = session.createCriteria(persistentClass);
|
||||
criteria.add(Expression.eq("name", name));
|
||||
result = (Spell) criteria.uniqueResult();
|
||||
tx.commit();
|
||||
}
|
||||
catch (Exception e) {
|
||||
if (tx!=null) tx.rollback();
|
||||
throw e;
|
||||
}
|
||||
finally {
|
||||
session.close();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,7 @@
|
||||
package com.iluwatar;
|
||||
|
||||
public interface SpellbookDao extends Dao<Spellbook> {
|
||||
|
||||
Spellbook findByName(String name);
|
||||
|
||||
}
|
||||
|
@ -1,5 +1,34 @@
|
||||
package com.iluwatar;
|
||||
|
||||
import org.hibernate.Criteria;
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.Transaction;
|
||||
import org.hibernate.criterion.Expression;
|
||||
|
||||
public class SpellbookDaoImpl extends DaoBaseImpl<Spellbook> implements SpellbookDao {
|
||||
|
||||
@Override
|
||||
public Spellbook findByName(String name) {
|
||||
Session session = getSession();
|
||||
Transaction tx = null;
|
||||
Spellbook result = null;
|
||||
try {
|
||||
tx = session.beginTransaction();
|
||||
Criteria criteria = session.createCriteria(persistentClass);
|
||||
criteria.add(Expression.eq("name", name));
|
||||
result = (Spellbook) criteria.uniqueResult();
|
||||
result.getSpells().size();
|
||||
result.getWizards().size();
|
||||
tx.commit();
|
||||
}
|
||||
catch (Exception e) {
|
||||
if (tx!=null) tx.rollback();
|
||||
throw e;
|
||||
}
|
||||
finally {
|
||||
session.close();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,5 +1,7 @@
|
||||
package com.iluwatar;
|
||||
|
||||
public interface WizardDao extends Dao<Wizard> {
|
||||
|
||||
Wizard findByName(String name);
|
||||
|
||||
}
|
||||
|
@ -1,5 +1,32 @@
|
||||
package com.iluwatar;
|
||||
|
||||
import org.hibernate.Criteria;
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.Transaction;
|
||||
import org.hibernate.criterion.Expression;
|
||||
|
||||
public class WizardDaoImpl extends DaoBaseImpl<Wizard> implements WizardDao {
|
||||
|
||||
@Override
|
||||
public Wizard findByName(String name) {
|
||||
Session session = getSession();
|
||||
Transaction tx = null;
|
||||
Wizard result = null;
|
||||
try {
|
||||
tx = session.beginTransaction();
|
||||
Criteria criteria = session.createCriteria(persistentClass);
|
||||
criteria.add(Expression.eq("name", name));
|
||||
result = (Wizard) criteria.uniqueResult();
|
||||
result.getSpellbooks().size();
|
||||
tx.commit();
|
||||
}
|
||||
catch (Exception e) {
|
||||
if (tx!=null) tx.rollback();
|
||||
throw e;
|
||||
}
|
||||
finally {
|
||||
session.close();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user