Reformat rest of the design patterns - Issue #224
This commit is contained in:
		| @@ -16,161 +16,163 @@ import com.iluwatar.servicelayer.wizard.WizardDaoImpl; | ||||
|  | ||||
|  | ||||
| /** | ||||
|  * Service layer defines an application's boundary with a layer of services that establishes  | ||||
|  * a set of available operations and coordinates the application's response in each operation. | ||||
|  * Service layer defines an application's boundary with a layer of services that establishes a set | ||||
|  * of available operations and coordinates the application's response in each operation. | ||||
|  * <p> | ||||
|  * Enterprise applications typically require different kinds of interfaces to the data  | ||||
|  * they store and the logic they implement: data loaders, user interfaces, integration gateways,  | ||||
|  * and others. Despite their different purposes, these interfaces often need common interactions  | ||||
|  * with the application to access and manipulate its data and invoke its business logic. The  | ||||
|  * interactions may be complex, involving transactions across multiple resources and the  | ||||
|  * coordination of several responses to an action. Encoding the logic of the interactions  | ||||
|  * separately in each interface causes a lot of duplication. | ||||
|  * Enterprise applications typically require different kinds of interfaces to the data they store | ||||
|  * and the logic they implement: data loaders, user interfaces, integration gateways, and others. | ||||
|  * Despite their different purposes, these interfaces often need common interactions with the | ||||
|  * application to access and manipulate its data and invoke its business logic. The interactions may | ||||
|  * be complex, involving transactions across multiple resources and the coordination of several | ||||
|  * responses to an action. Encoding the logic of the interactions separately in each interface | ||||
|  * causes a lot of duplication. | ||||
|  * <p> | ||||
|  * The example application demonstrates interactions between a client ({@link App}) and a service  | ||||
|  * ({@link MagicService}). The service is implemented with 3-layer architecture (entity, dao, service). | ||||
|  * For persistence the example uses in-memory H2 database which is populated on each application | ||||
|  * startup. | ||||
|  * The example application demonstrates interactions between a client ({@link App}) and a service ( | ||||
|  * {@link MagicService}). The service is implemented with 3-layer architecture (entity, dao, | ||||
|  * service). For persistence the example uses in-memory H2 database which is populated on each | ||||
|  * application startup. | ||||
|  * | ||||
|  */ | ||||
| public class App { | ||||
| 	 | ||||
| 	/** | ||||
| 	 * Program entry point | ||||
| 	 * @param args command line args | ||||
| 	 */ | ||||
|     public static void main( String[] args ) {    | ||||
|     	// populate the in-memory database | ||||
|     	initData(); | ||||
|     	// query the data using the service | ||||
|     	queryData(); | ||||
|  | ||||
|   /** | ||||
|    * Program entry point | ||||
|    *  | ||||
|    * @param args command line args | ||||
|    */ | ||||
|   public static void main(String[] args) { | ||||
|     // populate the in-memory database | ||||
|     initData(); | ||||
|     // query the data using the service | ||||
|     queryData(); | ||||
|   } | ||||
|  | ||||
|   public static void initData() { | ||||
|     // spells | ||||
|     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); | ||||
|  | ||||
|     // spellbooks | ||||
|     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); | ||||
|  | ||||
|     // wizards | ||||
|     WizardDao wizardDao = new WizardDaoImpl(); | ||||
|     Wizard wizard1 = new Wizard("Aderlard Boud"); | ||||
|     wizardDao.persist(wizard1); | ||||
|     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(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(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(spellbookDao.findByName("Book of Kihione")); | ||||
|     wizardDao.merge(wizard4); | ||||
|   } | ||||
|  | ||||
|   public static void queryData() { | ||||
|     MagicService service = | ||||
|         new MagicServiceImpl(new WizardDaoImpl(), new SpellbookDaoImpl(), new SpellDaoImpl()); | ||||
|     System.out.println("Enumerating all wizards"); | ||||
|     for (Wizard w : service.findAllWizards()) { | ||||
|       System.out.println(w.getName()); | ||||
|     } | ||||
|      | ||||
|     public static void initData() { | ||||
|     	// spells | ||||
|     	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); | ||||
|     	 | ||||
|     	// spellbooks | ||||
|     	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); | ||||
|     	 | ||||
|     	// wizards | ||||
|     	WizardDao wizardDao = new WizardDaoImpl(); | ||||
|     	Wizard wizard1 = new Wizard("Aderlard Boud"); | ||||
|     	wizardDao.persist(wizard1); | ||||
|     	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(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(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(spellbookDao.findByName("Book of Kihione")); | ||||
|     	wizardDao.merge(wizard4); | ||||
|     System.out.println("Enumerating all spellbooks"); | ||||
|     for (Spellbook s : service.findAllSpellbooks()) { | ||||
|       System.out.println(s.getName()); | ||||
|     } | ||||
|      | ||||
|     public static void queryData() { | ||||
|     	MagicService service = new MagicServiceImpl(new WizardDaoImpl(), new SpellbookDaoImpl(), new SpellDaoImpl()); | ||||
|     	System.out.println("Enumerating all wizards"); | ||||
|     	for (Wizard w: service.findAllWizards()) { | ||||
|     		System.out.println(w.getName()); | ||||
|     	} | ||||
|     	System.out.println("Enumerating all spellbooks"); | ||||
|     	for (Spellbook s: service.findAllSpellbooks()) { | ||||
|     		System.out.println(s.getName()); | ||||
|     	} | ||||
|     	System.out.println("Enumerating all spells"); | ||||
|     	for (Spell s: service.findAllSpells()) { | ||||
|     		System.out.println(s.getName()); | ||||
|     	} | ||||
|     	System.out.println("Find wizards with spellbook 'Book of Idores'"); | ||||
|     	List<Wizard> wizardsWithSpellbook = service.findWizardsWithSpellbook("Book of Idores"); | ||||
|     	for (Wizard w: wizardsWithSpellbook) { | ||||
|     		System.out.println(String.format("%s has 'Book of Idores'", w.getName())); | ||||
|     	} | ||||
|     	System.out.println("Find wizards with spell 'Fireball'"); | ||||
|     	List<Wizard> wizardsWithSpell = service.findWizardsWithSpell("Fireball"); | ||||
|     	for (Wizard w: wizardsWithSpell) { | ||||
|     		System.out.println(String.format("%s has 'Fireball'", w.getName())); | ||||
|     	} | ||||
|     System.out.println("Enumerating all spells"); | ||||
|     for (Spell s : service.findAllSpells()) { | ||||
|       System.out.println(s.getName()); | ||||
|     } | ||||
|     System.out.println("Find wizards with spellbook 'Book of Idores'"); | ||||
|     List<Wizard> wizardsWithSpellbook = service.findWizardsWithSpellbook("Book of Idores"); | ||||
|     for (Wizard w : wizardsWithSpellbook) { | ||||
|       System.out.println(String.format("%s has 'Book of Idores'", w.getName())); | ||||
|     } | ||||
|     System.out.println("Find wizards with spell 'Fireball'"); | ||||
|     List<Wizard> wizardsWithSpell = service.findWizardsWithSpell("Fireball"); | ||||
|     for (Wizard w : wizardsWithSpell) { | ||||
|       System.out.println(String.format("%s has 'Fireball'", w.getName())); | ||||
|     } | ||||
|   } | ||||
| } | ||||
|   | ||||
| @@ -11,9 +11,9 @@ import javax.persistence.Version; | ||||
|  * | ||||
|  */ | ||||
| @MappedSuperclass | ||||
| @Inheritance(strategy=InheritanceType.TABLE_PER_CLASS) | ||||
| @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) | ||||
| public class BaseEntity { | ||||
| 	 | ||||
| 	@Version | ||||
| 	private Long version; | ||||
|  | ||||
|   @Version | ||||
|   private Long version; | ||||
| } | ||||
|   | ||||
| @@ -11,13 +11,13 @@ import java.util.List; | ||||
|  */ | ||||
| public interface Dao<E extends BaseEntity> { | ||||
|  | ||||
| 	E find(Long id); | ||||
| 	 | ||||
| 	void persist(E entity); | ||||
| 	 | ||||
| 	E merge(E entity); | ||||
| 	 | ||||
| 	void delete(E entity); | ||||
| 	 | ||||
| 	List<E> findAll(); | ||||
|   E find(Long id); | ||||
|  | ||||
|   void persist(E entity); | ||||
|  | ||||
|   E merge(E entity); | ||||
|  | ||||
|   void delete(E entity); | ||||
|  | ||||
|   List<E> findAll(); | ||||
| } | ||||
|   | ||||
| @@ -18,110 +18,105 @@ import com.iluwatar.servicelayer.hibernate.HibernateUtil; | ||||
|  *  | ||||
|  */ | ||||
| public abstract class DaoBaseImpl<E extends BaseEntity> implements Dao<E> { | ||||
| 	 | ||||
| 	@SuppressWarnings("unchecked") | ||||
| 	protected Class<E> persistentClass = (Class<E>) ((ParameterizedType) getClass() | ||||
|             .getGenericSuperclass()).getActualTypeArguments()[0]; | ||||
|  | ||||
| 	protected Session getSession() { | ||||
| 		return HibernateUtil.getSessionFactory().openSession(); | ||||
| 	} | ||||
| 	 | ||||
| 	@Override | ||||
| 	public E find(Long id) { | ||||
| 		Session session = getSession(); | ||||
| 		Transaction tx = null; | ||||
| 		E result = null; | ||||
| 		try { | ||||
| 			tx = session.beginTransaction(); | ||||
| 			Criteria criteria = session.createCriteria(persistentClass); | ||||
| 			criteria.add(Restrictions.idEq(id)); | ||||
| 			result = (E) criteria.uniqueResult(); | ||||
| 			tx.commit(); | ||||
| 		} | ||||
| 		catch (Exception e) { | ||||
| 			if (tx!=null) tx.rollback(); | ||||
| 			throw e; | ||||
| 		} | ||||
| 		finally { | ||||
| 			session.close(); | ||||
| 		}		 | ||||
| 		return result; | ||||
| 	} | ||||
| 	 | ||||
| 	@Override | ||||
| 	public void persist(E entity) { | ||||
| 		Session session = getSession(); | ||||
| 		Transaction tx = null; | ||||
| 		try { | ||||
| 			tx = session.beginTransaction(); | ||||
| 			session.persist(entity); | ||||
| 			tx.commit(); | ||||
| 		} | ||||
| 		catch (Exception e) { | ||||
| 			if (tx!=null) tx.rollback(); | ||||
| 			throw e; | ||||
| 		} | ||||
| 		finally { | ||||
| 			session.close(); | ||||
| 		}		 | ||||
| 	} | ||||
| 	 | ||||
| 	@Override | ||||
| 	public E merge(E entity) { | ||||
| 		Session session = getSession(); | ||||
| 		Transaction tx = null; | ||||
| 		E result = null; | ||||
| 		try { | ||||
| 			tx = session.beginTransaction(); | ||||
| 			result = (E) session.merge(entity); | ||||
| 			tx.commit(); | ||||
| 		} | ||||
| 		catch (Exception e) { | ||||
| 			if (tx!=null) tx.rollback(); | ||||
| 			throw e; | ||||
| 		} | ||||
| 		finally { | ||||
| 			session.close(); | ||||
| 		}		 | ||||
| 		return result; | ||||
| 	} | ||||
| 	 | ||||
| 	@Override | ||||
| 	public void delete(E entity) { | ||||
| 		Session session = getSession(); | ||||
| 		Transaction tx = null; | ||||
| 		try { | ||||
| 			tx = session.beginTransaction(); | ||||
| 			session.delete(entity); | ||||
| 			tx.commit(); | ||||
| 		} | ||||
| 		catch (Exception e) { | ||||
| 			if (tx!=null) tx.rollback(); | ||||
| 			throw e; | ||||
| 		} | ||||
| 		finally { | ||||
| 			session.close(); | ||||
| 		}		 | ||||
| 	} | ||||
| 	 | ||||
| 	@Override | ||||
| 	public List<E> findAll() { | ||||
| 		Session session = getSession(); | ||||
| 		Transaction tx = null; | ||||
| 		List<E> result = null; | ||||
| 		try { | ||||
| 			tx = session.beginTransaction(); | ||||
| 			Criteria criteria = session.createCriteria(persistentClass); | ||||
| 			result = criteria.list(); | ||||
| 		} | ||||
| 		catch (Exception e) { | ||||
| 			if (tx!=null) tx.rollback(); | ||||
| 			throw e; | ||||
| 		} | ||||
| 		finally { | ||||
| 			session.close(); | ||||
| 		}		 | ||||
| 		return result; | ||||
| 	} | ||||
|   @SuppressWarnings("unchecked") | ||||
|   protected Class<E> persistentClass = (Class<E>) ((ParameterizedType) getClass() | ||||
|       .getGenericSuperclass()).getActualTypeArguments()[0]; | ||||
|  | ||||
|   protected Session getSession() { | ||||
|     return HibernateUtil.getSessionFactory().openSession(); | ||||
|   } | ||||
|  | ||||
|   @Override | ||||
|   public E find(Long id) { | ||||
|     Session session = getSession(); | ||||
|     Transaction tx = null; | ||||
|     E result = null; | ||||
|     try { | ||||
|       tx = session.beginTransaction(); | ||||
|       Criteria criteria = session.createCriteria(persistentClass); | ||||
|       criteria.add(Restrictions.idEq(id)); | ||||
|       result = (E) criteria.uniqueResult(); | ||||
|       tx.commit(); | ||||
|     } catch (Exception e) { | ||||
|       if (tx != null) | ||||
|         tx.rollback(); | ||||
|       throw e; | ||||
|     } finally { | ||||
|       session.close(); | ||||
|     } | ||||
|     return result; | ||||
|   } | ||||
|  | ||||
|   @Override | ||||
|   public void persist(E entity) { | ||||
|     Session session = getSession(); | ||||
|     Transaction tx = null; | ||||
|     try { | ||||
|       tx = session.beginTransaction(); | ||||
|       session.persist(entity); | ||||
|       tx.commit(); | ||||
|     } catch (Exception e) { | ||||
|       if (tx != null) | ||||
|         tx.rollback(); | ||||
|       throw e; | ||||
|     } finally { | ||||
|       session.close(); | ||||
|     } | ||||
|   } | ||||
|  | ||||
|   @Override | ||||
|   public E merge(E entity) { | ||||
|     Session session = getSession(); | ||||
|     Transaction tx = null; | ||||
|     E result = null; | ||||
|     try { | ||||
|       tx = session.beginTransaction(); | ||||
|       result = (E) session.merge(entity); | ||||
|       tx.commit(); | ||||
|     } catch (Exception e) { | ||||
|       if (tx != null) | ||||
|         tx.rollback(); | ||||
|       throw e; | ||||
|     } finally { | ||||
|       session.close(); | ||||
|     } | ||||
|     return result; | ||||
|   } | ||||
|  | ||||
|   @Override | ||||
|   public void delete(E entity) { | ||||
|     Session session = getSession(); | ||||
|     Transaction tx = null; | ||||
|     try { | ||||
|       tx = session.beginTransaction(); | ||||
|       session.delete(entity); | ||||
|       tx.commit(); | ||||
|     } catch (Exception e) { | ||||
|       if (tx != null) | ||||
|         tx.rollback(); | ||||
|       throw e; | ||||
|     } finally { | ||||
|       session.close(); | ||||
|     } | ||||
|   } | ||||
|  | ||||
|   @Override | ||||
|   public List<E> findAll() { | ||||
|     Session session = getSession(); | ||||
|     Transaction tx = null; | ||||
|     List<E> result = null; | ||||
|     try { | ||||
|       tx = session.beginTransaction(); | ||||
|       Criteria criteria = session.createCriteria(persistentClass); | ||||
|       result = criteria.list(); | ||||
|     } catch (Exception e) { | ||||
|       if (tx != null) | ||||
|         tx.rollback(); | ||||
|       throw e; | ||||
|     } finally { | ||||
|       session.close(); | ||||
|     } | ||||
|     return result; | ||||
|   } | ||||
| } | ||||
|   | ||||
| @@ -14,27 +14,25 @@ import com.iluwatar.servicelayer.wizard.Wizard; | ||||
|  */ | ||||
| public class HibernateUtil { | ||||
|  | ||||
| 	private static final SessionFactory sessionFactory; | ||||
|   private static final SessionFactory sessionFactory; | ||||
|  | ||||
| 	static { | ||||
| 		try { | ||||
| 	    	sessionFactory = new Configuration() | ||||
| 	  	  		.addAnnotatedClass(Wizard.class) | ||||
| 	  	  		.addAnnotatedClass(Spellbook.class) | ||||
| 	  	  		.addAnnotatedClass(Spell.class) | ||||
| 	  	  		.setProperty("hibernate.dialect", "org.hibernate.dialect.H2Dialect") | ||||
| 	  	  		.setProperty("hibernate.connection.url", "jdbc:h2:mem:test;DB_CLOSE_DELAY=-1") | ||||
| 	  	  		.setProperty("hibernate.current_session_context_class", "thread") | ||||
| 	  	  		.setProperty("hibernate.show_sql", "true") | ||||
| 	  	  		.setProperty("hibernate.hbm2ddl.auto", "create-drop") | ||||
| 	  	  		.buildSessionFactory(); | ||||
| 		} catch (Throwable ex) { | ||||
| 			System.err.println("Initial SessionFactory creation failed." + ex); | ||||
| 			throw new ExceptionInInitializerError(ex); | ||||
| 		} | ||||
| 	} | ||||
|   static { | ||||
|     try { | ||||
|       sessionFactory = | ||||
|           new Configuration().addAnnotatedClass(Wizard.class).addAnnotatedClass(Spellbook.class) | ||||
|               .addAnnotatedClass(Spell.class) | ||||
|               .setProperty("hibernate.dialect", "org.hibernate.dialect.H2Dialect") | ||||
|               .setProperty("hibernate.connection.url", "jdbc:h2:mem:test;DB_CLOSE_DELAY=-1") | ||||
|               .setProperty("hibernate.current_session_context_class", "thread") | ||||
|               .setProperty("hibernate.show_sql", "true") | ||||
|               .setProperty("hibernate.hbm2ddl.auto", "create-drop").buildSessionFactory(); | ||||
|     } catch (Throwable ex) { | ||||
|       System.err.println("Initial SessionFactory creation failed." + ex); | ||||
|       throw new ExceptionInInitializerError(ex); | ||||
|     } | ||||
|   } | ||||
|  | ||||
| 	public static SessionFactory getSessionFactory() { | ||||
| 		return sessionFactory; | ||||
| 	} | ||||
|   public static SessionFactory getSessionFactory() { | ||||
|     return sessionFactory; | ||||
|   } | ||||
| } | ||||
|   | ||||
| @@ -14,13 +14,13 @@ import com.iluwatar.servicelayer.wizard.Wizard; | ||||
|  */ | ||||
| public interface MagicService { | ||||
|  | ||||
| 	List<Wizard> findAllWizards(); | ||||
|   List<Wizard> findAllWizards(); | ||||
|  | ||||
| 	List<Spellbook> findAllSpellbooks(); | ||||
| 	 | ||||
| 	List<Spell> findAllSpells(); | ||||
|   List<Spellbook> findAllSpellbooks(); | ||||
|  | ||||
| 	List<Wizard> findWizardsWithSpellbook(String name); | ||||
|   List<Spell> findAllSpells(); | ||||
|  | ||||
| 	List<Wizard> findWizardsWithSpell(String name); | ||||
|   List<Wizard> findWizardsWithSpellbook(String name); | ||||
|  | ||||
|   List<Wizard> findWizardsWithSpell(String name); | ||||
| } | ||||
|   | ||||
| @@ -16,42 +16,42 @@ import com.iluwatar.servicelayer.wizard.WizardDao; | ||||
|  * | ||||
|  */ | ||||
| public class MagicServiceImpl implements MagicService { | ||||
| 	 | ||||
| 	private WizardDao wizardDao; | ||||
| 	private SpellbookDao spellbookDao; | ||||
| 	private SpellDao spellDao; | ||||
|  | ||||
| 	public MagicServiceImpl(WizardDao wizardDao, SpellbookDao spellbookDao, SpellDao spellDao) { | ||||
| 		this.wizardDao = wizardDao; | ||||
| 		this.spellbookDao = spellbookDao; | ||||
| 		this.spellDao = spellDao; | ||||
| 	} | ||||
|   private WizardDao wizardDao; | ||||
|   private SpellbookDao spellbookDao; | ||||
|   private SpellDao spellDao; | ||||
|  | ||||
| 	@Override | ||||
| 	public List<Wizard> findAllWizards() { | ||||
| 		return wizardDao.findAll(); | ||||
| 	} | ||||
|   public MagicServiceImpl(WizardDao wizardDao, SpellbookDao spellbookDao, SpellDao spellDao) { | ||||
|     this.wizardDao = wizardDao; | ||||
|     this.spellbookDao = spellbookDao; | ||||
|     this.spellDao = spellDao; | ||||
|   } | ||||
|  | ||||
| 	@Override | ||||
| 	public List<Spellbook> findAllSpellbooks() { | ||||
| 		return spellbookDao.findAll(); | ||||
| 	} | ||||
|   @Override | ||||
|   public List<Wizard> findAllWizards() { | ||||
|     return wizardDao.findAll(); | ||||
|   } | ||||
|  | ||||
| 	@Override | ||||
| 	public List<Spell> findAllSpells() { | ||||
| 		return spellDao.findAll(); | ||||
| 	} | ||||
|   @Override | ||||
|   public List<Spellbook> findAllSpellbooks() { | ||||
|     return spellbookDao.findAll(); | ||||
|   } | ||||
|  | ||||
| 	@Override | ||||
| 	public List<Wizard> findWizardsWithSpellbook(String name) { | ||||
| 		Spellbook spellbook = spellbookDao.findByName(name); | ||||
| 		return new ArrayList<Wizard>(spellbook.getWizards()); | ||||
| 	} | ||||
|   @Override | ||||
|   public List<Spell> findAllSpells() { | ||||
|     return spellDao.findAll(); | ||||
|   } | ||||
|  | ||||
| 	@Override | ||||
| 	public List<Wizard> findWizardsWithSpell(String name) { | ||||
| 		Spell spell = spellDao.findByName(name); | ||||
| 		Spellbook spellbook = spell.getSpellbook(); | ||||
| 		return new ArrayList<Wizard>(spellbook.getWizards()); | ||||
| 	} | ||||
|   @Override | ||||
|   public List<Wizard> findWizardsWithSpellbook(String name) { | ||||
|     Spellbook spellbook = spellbookDao.findByName(name); | ||||
|     return new ArrayList<Wizard>(spellbook.getWizards()); | ||||
|   } | ||||
|  | ||||
|   @Override | ||||
|   public List<Wizard> findWizardsWithSpell(String name) { | ||||
|     Spell spell = spellDao.findByName(name); | ||||
|     Spellbook spellbook = spell.getSpellbook(); | ||||
|     return new ArrayList<Wizard>(spellbook.getWizards()); | ||||
|   } | ||||
| } | ||||
|   | ||||
| @@ -17,54 +17,53 @@ import com.iluwatar.servicelayer.spellbook.Spellbook; | ||||
|  * | ||||
|  */ | ||||
| @Entity | ||||
| @Table(name="SPELL") | ||||
| @Table(name = "SPELL") | ||||
| public class Spell extends BaseEntity { | ||||
| 	 | ||||
| 	private String name; | ||||
| 	 | ||||
| 	public Spell() { | ||||
| 	} | ||||
| 	 | ||||
| 	public Spell(String name) { | ||||
| 		this(); | ||||
| 		this.name = name; | ||||
| 	} | ||||
|  | ||||
| 	@Id | ||||
| 	@GeneratedValue | ||||
| 	@Column(name = "SPELL_ID") | ||||
| 	private Long id; | ||||
|   private String name; | ||||
|  | ||||
| 	public Long getId() { | ||||
| 		return id; | ||||
| 	} | ||||
|   public Spell() {} | ||||
|  | ||||
| 	public void setId(Long id) { | ||||
| 		this.id = id; | ||||
| 	} | ||||
| 	 | ||||
| 	@ManyToOne | ||||
| 	@JoinColumn(name="SPELLBOOK_ID_FK", referencedColumnName="SPELLBOOK_ID") | ||||
| 	private Spellbook spellbook; | ||||
| 	 | ||||
| 	public String getName() { | ||||
| 		return name; | ||||
| 	} | ||||
|   public Spell(String name) { | ||||
|     this(); | ||||
|     this.name = name; | ||||
|   } | ||||
|  | ||||
| 	public void setName(String name) { | ||||
| 		this.name = name; | ||||
| 	} | ||||
| 	 | ||||
| 	public Spellbook getSpellbook() { | ||||
| 		return spellbook; | ||||
| 	} | ||||
|   @Id | ||||
|   @GeneratedValue | ||||
|   @Column(name = "SPELL_ID") | ||||
|   private Long id; | ||||
|  | ||||
| 	public void setSpellbook(Spellbook spellbook) { | ||||
| 		this.spellbook = spellbook; | ||||
| 	} | ||||
|   public Long getId() { | ||||
|     return id; | ||||
|   } | ||||
|  | ||||
| 	@Override | ||||
| 	public String toString() { | ||||
| 		return name; | ||||
| 	}	 | ||||
|   public void setId(Long id) { | ||||
|     this.id = id; | ||||
|   } | ||||
|  | ||||
|   @ManyToOne | ||||
|   @JoinColumn(name = "SPELLBOOK_ID_FK", referencedColumnName = "SPELLBOOK_ID") | ||||
|   private Spellbook spellbook; | ||||
|  | ||||
|   public String getName() { | ||||
|     return name; | ||||
|   } | ||||
|  | ||||
|   public void setName(String name) { | ||||
|     this.name = name; | ||||
|   } | ||||
|  | ||||
|   public Spellbook getSpellbook() { | ||||
|     return spellbook; | ||||
|   } | ||||
|  | ||||
|   public void setSpellbook(Spellbook spellbook) { | ||||
|     this.spellbook = spellbook; | ||||
|   } | ||||
|  | ||||
|   @Override | ||||
|   public String toString() { | ||||
|     return name; | ||||
|   } | ||||
| } | ||||
|   | ||||
| @@ -8,7 +8,7 @@ import com.iluwatar.servicelayer.common.Dao; | ||||
|  * | ||||
|  */ | ||||
| public interface SpellDao extends Dao<Spell> { | ||||
| 	 | ||||
| 	Spell findByName(String name); | ||||
|  | ||||
|   Spell findByName(String name); | ||||
|  | ||||
| } | ||||
|   | ||||
| @@ -14,26 +14,25 @@ import com.iluwatar.servicelayer.common.DaoBaseImpl; | ||||
|  */ | ||||
| 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(Restrictions.eq("name", name)); | ||||
| 			result = (Spell) criteria.uniqueResult(); | ||||
| 			result.getSpellbook().getWizards().size(); | ||||
| 			tx.commit(); | ||||
| 		} | ||||
| 		catch (Exception e) { | ||||
| 			if (tx!=null) tx.rollback(); | ||||
| 			throw e; | ||||
| 		} | ||||
| 		finally { | ||||
| 			session.close(); | ||||
| 		}		 | ||||
| 		return result; | ||||
| 	} | ||||
|   @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(Restrictions.eq("name", name)); | ||||
|       result = (Spell) criteria.uniqueResult(); | ||||
|       result.getSpellbook().getWizards().size(); | ||||
|       tx.commit(); | ||||
|     } catch (Exception e) { | ||||
|       if (tx != null) | ||||
|         tx.rollback(); | ||||
|       throw e; | ||||
|     } finally { | ||||
|       session.close(); | ||||
|     } | ||||
|     return result; | ||||
|   } | ||||
| } | ||||
|   | ||||
| @@ -22,71 +22,71 @@ import com.iluwatar.servicelayer.wizard.Wizard; | ||||
|  * | ||||
|  */ | ||||
| @Entity | ||||
| @Table(name="SPELLBOOK") | ||||
| @Table(name = "SPELLBOOK") | ||||
| public class Spellbook extends BaseEntity { | ||||
| 	 | ||||
| 	public Spellbook() { | ||||
| 		spells = new HashSet<Spell>(); | ||||
| 		wizards = new HashSet<Wizard>(); | ||||
| 	} | ||||
| 	 | ||||
| 	public Spellbook(String name) { | ||||
| 		this(); | ||||
| 		this.name = name; | ||||
| 	} | ||||
|  | ||||
| 	@Id | ||||
| 	@GeneratedValue | ||||
| 	@Column(name = "SPELLBOOK_ID") | ||||
| 	private Long id; | ||||
|   public Spellbook() { | ||||
|     spells = new HashSet<Spell>(); | ||||
|     wizards = new HashSet<Wizard>(); | ||||
|   } | ||||
|  | ||||
| 	public Long getId() { | ||||
| 		return id; | ||||
| 	} | ||||
|   public Spellbook(String name) { | ||||
|     this(); | ||||
|     this.name = name; | ||||
|   } | ||||
|  | ||||
| 	public void setId(Long id) { | ||||
| 		this.id = id; | ||||
| 	} | ||||
| 	 | ||||
| 	private String name; | ||||
|   @Id | ||||
|   @GeneratedValue | ||||
|   @Column(name = "SPELLBOOK_ID") | ||||
|   private Long id; | ||||
|  | ||||
| 	@ManyToMany(mappedBy = "spellbooks") | ||||
| 	private Set<Wizard> wizards; | ||||
|   public Long getId() { | ||||
|     return id; | ||||
|   } | ||||
|  | ||||
| 	@OneToMany(mappedBy = "spellbook", orphanRemoval = true, cascade = CascadeType.ALL) | ||||
| 	private Set<Spell> spells; | ||||
| 	 | ||||
| 	public String getName() { | ||||
| 		return name; | ||||
| 	} | ||||
|   public void setId(Long id) { | ||||
|     this.id = id; | ||||
|   } | ||||
|  | ||||
| 	public void setName(String name) { | ||||
| 		this.name = name; | ||||
| 	} | ||||
|   private String name; | ||||
|  | ||||
| 	public Set<Wizard> getWizards() { | ||||
| 		return wizards; | ||||
| 	} | ||||
|   @ManyToMany(mappedBy = "spellbooks") | ||||
|   private Set<Wizard> wizards; | ||||
|  | ||||
| 	public void setWizards(Set<Wizard> wizards) { | ||||
| 		this.wizards = wizards; | ||||
| 	} | ||||
|   @OneToMany(mappedBy = "spellbook", orphanRemoval = true, cascade = CascadeType.ALL) | ||||
|   private Set<Spell> spells; | ||||
|  | ||||
| 	public Set<Spell> getSpells() { | ||||
| 		return spells; | ||||
| 	} | ||||
|   public String getName() { | ||||
|     return name; | ||||
|   } | ||||
|  | ||||
| 	public void setSpells(Set<Spell> spells) { | ||||
| 		this.spells = spells; | ||||
| 	} | ||||
|   public void setName(String name) { | ||||
|     this.name = name; | ||||
|   } | ||||
|  | ||||
| 	public void addSpell(Spell spell) { | ||||
| 		spell.setSpellbook(this); | ||||
| 		spells.add(spell); | ||||
| 	} | ||||
| 	 | ||||
| 	@Override | ||||
| 	public String toString() { | ||||
| 		return name; | ||||
| 	}	 | ||||
|   public Set<Wizard> getWizards() { | ||||
|     return wizards; | ||||
|   } | ||||
|  | ||||
|   public void setWizards(Set<Wizard> wizards) { | ||||
|     this.wizards = wizards; | ||||
|   } | ||||
|  | ||||
|   public Set<Spell> getSpells() { | ||||
|     return spells; | ||||
|   } | ||||
|  | ||||
|   public void setSpells(Set<Spell> spells) { | ||||
|     this.spells = spells; | ||||
|   } | ||||
|  | ||||
|   public void addSpell(Spell spell) { | ||||
|     spell.setSpellbook(this); | ||||
|     spells.add(spell); | ||||
|   } | ||||
|  | ||||
|   @Override | ||||
|   public String toString() { | ||||
|     return name; | ||||
|   } | ||||
| } | ||||
|   | ||||
| @@ -8,7 +8,7 @@ import com.iluwatar.servicelayer.common.Dao; | ||||
|  * | ||||
|  */ | ||||
| public interface SpellbookDao extends Dao<Spellbook> { | ||||
| 	 | ||||
| 	Spellbook findByName(String name); | ||||
|  | ||||
|   Spellbook findByName(String name); | ||||
|  | ||||
| } | ||||
|   | ||||
| @@ -14,28 +14,27 @@ import com.iluwatar.servicelayer.common.DaoBaseImpl; | ||||
|  */ | ||||
| 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(Restrictions.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; | ||||
| 	} | ||||
|   @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(Restrictions.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; | ||||
|   } | ||||
|  | ||||
| } | ||||
|   | ||||
| @@ -20,59 +20,59 @@ import com.iluwatar.servicelayer.spellbook.Spellbook; | ||||
|  * | ||||
|  */ | ||||
| @Entity | ||||
| @Table(name="WIZARD") | ||||
| @Table(name = "WIZARD") | ||||
| public class Wizard extends BaseEntity { | ||||
| 	 | ||||
| 	public Wizard() { | ||||
| 		spellbooks = new HashSet<Spellbook>(); | ||||
| 	} | ||||
| 	 | ||||
| 	public Wizard(String name) { | ||||
| 		this(); | ||||
| 		this.name = name; | ||||
| 	} | ||||
|  | ||||
| 	@Id | ||||
| 	@GeneratedValue | ||||
| 	@Column(name = "WIZARD_ID") | ||||
| 	private Long id; | ||||
|   public Wizard() { | ||||
|     spellbooks = new HashSet<Spellbook>(); | ||||
|   } | ||||
|  | ||||
| 	public Long getId() { | ||||
| 		return id; | ||||
| 	} | ||||
|   public Wizard(String name) { | ||||
|     this(); | ||||
|     this.name = name; | ||||
|   } | ||||
|  | ||||
| 	public void setId(Long id) { | ||||
| 		this.id = id; | ||||
| 	} | ||||
| 	 | ||||
| 	private String name; | ||||
|   @Id | ||||
|   @GeneratedValue | ||||
|   @Column(name = "WIZARD_ID") | ||||
|   private Long id; | ||||
|  | ||||
| 	@ManyToMany(cascade = CascadeType.ALL) | ||||
| 	private Set<Spellbook> spellbooks; | ||||
| 	 | ||||
| 	public String getName() { | ||||
| 		return name; | ||||
| 	} | ||||
|   public Long getId() { | ||||
|     return id; | ||||
|   } | ||||
|  | ||||
| 	public void setName(String name) { | ||||
| 		this.name = name; | ||||
| 	} | ||||
|   public void setId(Long id) { | ||||
|     this.id = id; | ||||
|   } | ||||
|  | ||||
| 	public Set<Spellbook> getSpellbooks() { | ||||
| 		return spellbooks; | ||||
| 	} | ||||
|   private String name; | ||||
|  | ||||
| 	public void setSpellbooks(Set<Spellbook> spellbooks) { | ||||
| 		this.spellbooks = spellbooks; | ||||
| 	} | ||||
|   @ManyToMany(cascade = CascadeType.ALL) | ||||
|   private Set<Spellbook> spellbooks; | ||||
|  | ||||
| 	public void addSpellbook(Spellbook spellbook) { | ||||
| 		spellbook.getWizards().add(this); | ||||
| 		spellbooks.add(spellbook); | ||||
| 	} | ||||
| 	 | ||||
| 	@Override | ||||
| 	public String toString() { | ||||
| 		return name; | ||||
| 	}	 | ||||
|   public String getName() { | ||||
|     return name; | ||||
|   } | ||||
|  | ||||
|   public void setName(String name) { | ||||
|     this.name = name; | ||||
|   } | ||||
|  | ||||
|   public Set<Spellbook> getSpellbooks() { | ||||
|     return spellbooks; | ||||
|   } | ||||
|  | ||||
|   public void setSpellbooks(Set<Spellbook> spellbooks) { | ||||
|     this.spellbooks = spellbooks; | ||||
|   } | ||||
|  | ||||
|   public void addSpellbook(Spellbook spellbook) { | ||||
|     spellbook.getWizards().add(this); | ||||
|     spellbooks.add(spellbook); | ||||
|   } | ||||
|  | ||||
|   @Override | ||||
|   public String toString() { | ||||
|     return name; | ||||
|   } | ||||
| } | ||||
|   | ||||
| @@ -8,7 +8,7 @@ import com.iluwatar.servicelayer.common.Dao; | ||||
|  * | ||||
|  */ | ||||
| public interface WizardDao extends Dao<Wizard> { | ||||
| 	 | ||||
| 	Wizard findByName(String name); | ||||
|  | ||||
|   Wizard findByName(String name); | ||||
|  | ||||
| } | ||||
|   | ||||
| @@ -15,28 +15,27 @@ import com.iluwatar.servicelayer.spellbook.Spellbook; | ||||
|  */ | ||||
| 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(Restrictions.eq("name", name)); | ||||
| 			result = (Wizard) criteria.uniqueResult(); | ||||
| 			for (Spellbook s: result.getSpellbooks()) { | ||||
| 				s.getSpells().size(); | ||||
| 			} | ||||
| 			tx.commit(); | ||||
| 		} | ||||
| 		catch (Exception e) { | ||||
| 			if (tx!=null) tx.rollback(); | ||||
| 			throw e; | ||||
| 		} | ||||
| 		finally { | ||||
| 			session.close(); | ||||
| 		}		 | ||||
| 		return result; | ||||
| 	} | ||||
|   @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(Restrictions.eq("name", name)); | ||||
|       result = (Wizard) criteria.uniqueResult(); | ||||
|       for (Spellbook s : result.getSpellbooks()) { | ||||
|         s.getSpells().size(); | ||||
|       } | ||||
|       tx.commit(); | ||||
|     } catch (Exception e) { | ||||
|       if (tx != null) | ||||
|         tx.rollback(); | ||||
|       throw e; | ||||
|     } finally { | ||||
|       session.close(); | ||||
|     } | ||||
|     return result; | ||||
|   } | ||||
| } | ||||
|   | ||||
| @@ -10,10 +10,10 @@ import com.iluwatar.servicelayer.app.App; | ||||
|  * | ||||
|  */ | ||||
| public class AppTest { | ||||
| 	 | ||||
| 	@Test | ||||
| 	public void test() { | ||||
| 		String[] args = {}; | ||||
| 		App.main(args); | ||||
| 	} | ||||
|  | ||||
|   @Test | ||||
|   public void test() { | ||||
|     String[] args = {}; | ||||
|     App.main(args); | ||||
|   } | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user