Added tests for service-layer pattern
This commit is contained in:
		@@ -12,8 +12,37 @@ import javax.persistence.Version;
 | 
			
		||||
 */
 | 
			
		||||
@MappedSuperclass
 | 
			
		||||
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
 | 
			
		||||
public class BaseEntity {
 | 
			
		||||
public abstract class BaseEntity {
 | 
			
		||||
 | 
			
		||||
  @Version
 | 
			
		||||
  private Long version;
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * Indicates the unique id of this entity
 | 
			
		||||
   *
 | 
			
		||||
   * @return The id of the entity, or 'null' when not persisted
 | 
			
		||||
   */
 | 
			
		||||
  public abstract Long getId();
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * Set the id of this entity
 | 
			
		||||
   *
 | 
			
		||||
   * @param id The new id
 | 
			
		||||
   */
 | 
			
		||||
  public abstract void setId(Long id);
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * Get the name of this entity
 | 
			
		||||
   *
 | 
			
		||||
   * @return The name of the entity
 | 
			
		||||
   */
 | 
			
		||||
  public abstract String getName();
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * Set the name of this entity
 | 
			
		||||
   *
 | 
			
		||||
   * @param name The new name
 | 
			
		||||
   */
 | 
			
		||||
  public abstract void setName(final String name);
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -1,41 +1,56 @@
 | 
			
		||||
package com.iluwatar.servicelayer.hibernate;
 | 
			
		||||
 | 
			
		||||
import org.hibernate.SessionFactory;
 | 
			
		||||
import org.hibernate.cfg.Configuration;
 | 
			
		||||
 | 
			
		||||
import com.iluwatar.servicelayer.spell.Spell;
 | 
			
		||||
import com.iluwatar.servicelayer.spellbook.Spellbook;
 | 
			
		||||
import com.iluwatar.servicelayer.wizard.Wizard;
 | 
			
		||||
 | 
			
		||||
import org.hibernate.SessionFactory;
 | 
			
		||||
import org.hibernate.cfg.Configuration;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * 
 | 
			
		||||
 * Produces the Hibernate {@link SessionFactory}.
 | 
			
		||||
 *
 | 
			
		||||
 */
 | 
			
		||||
public class HibernateUtil {
 | 
			
		||||
 | 
			
		||||
  private static final SessionFactory SESSION_FACTORY;
 | 
			
		||||
 | 
			
		||||
  static {
 | 
			
		||||
    try {
 | 
			
		||||
      SESSION_FACTORY =
 | 
			
		||||
          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);
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
  /**
 | 
			
		||||
   * The cached session factory
 | 
			
		||||
   */
 | 
			
		||||
  private static volatile SessionFactory sessionFactory;
 | 
			
		||||
 | 
			
		||||
  private HibernateUtil() {
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  public static SessionFactory getSessionFactory() {
 | 
			
		||||
    return SESSION_FACTORY;
 | 
			
		||||
  /**
 | 
			
		||||
   * Create the current session factory instance, create a new one when there is none yet.
 | 
			
		||||
   *
 | 
			
		||||
   * @return The session factory
 | 
			
		||||
   */
 | 
			
		||||
  public static synchronized SessionFactory getSessionFactory() {
 | 
			
		||||
    if (sessionFactory == null) {
 | 
			
		||||
      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);
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
    return sessionFactory;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * Drop the current connection, resulting in a create-drop clean database next time. This is
 | 
			
		||||
   * mainly used for JUnit testing since one test should not influence the other
 | 
			
		||||
   */
 | 
			
		||||
  public static void dropSession() {
 | 
			
		||||
    getSessionFactory().close();
 | 
			
		||||
    sessionFactory = null;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -1,12 +1,12 @@
 | 
			
		||||
package com.iluwatar.servicelayer.spell;
 | 
			
		||||
 | 
			
		||||
import com.iluwatar.servicelayer.common.DaoBaseImpl;
 | 
			
		||||
 | 
			
		||||
import org.hibernate.Criteria;
 | 
			
		||||
import org.hibernate.Session;
 | 
			
		||||
import org.hibernate.Transaction;
 | 
			
		||||
import org.hibernate.criterion.Restrictions;
 | 
			
		||||
 | 
			
		||||
import com.iluwatar.servicelayer.common.DaoBaseImpl;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * 
 | 
			
		||||
 * SpellDao implementation.
 | 
			
		||||
@@ -24,7 +24,6 @@ public class SpellDaoImpl extends DaoBaseImpl<Spell> implements SpellDao {
 | 
			
		||||
      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) {
 | 
			
		||||
 
 | 
			
		||||
@@ -6,6 +6,7 @@ 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.ManyToMany;
 | 
			
		||||
@@ -50,7 +51,7 @@ public class Spellbook extends BaseEntity {
 | 
			
		||||
 | 
			
		||||
  private String name;
 | 
			
		||||
 | 
			
		||||
  @ManyToMany(mappedBy = "spellbooks")
 | 
			
		||||
  @ManyToMany(mappedBy = "spellbooks", fetch = FetchType.EAGER)
 | 
			
		||||
  private Set<Wizard> wizards;
 | 
			
		||||
 | 
			
		||||
  @OneToMany(mappedBy = "spellbook", orphanRemoval = true, cascade = CascadeType.ALL)
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user