Moving towards Service Layer example.
This commit is contained in:
@ -1,16 +1,15 @@
|
|||||||
package com.iluwatar;
|
package com.iluwatar;
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
|
|
||||||
public class App {
|
public class App {
|
||||||
public static void main( String[] args ) {
|
public static void main( String[] args ) {
|
||||||
WizardDao dao = new WizardDao();
|
WizardDaoImpl dao = new WizardDaoImpl();
|
||||||
persistData(dao);
|
persistData(dao);
|
||||||
queryData(dao);
|
queryData();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void persistData(WizardDao dao) {
|
public static void persistData(WizardDaoImpl dao) {
|
||||||
Spell spell = new Spell("Fireball");
|
Spell spell = new Spell("Fireball");
|
||||||
Spellbook spellbook = new Spellbook("Book of fire");
|
Spellbook spellbook = new Spellbook("Book of fire");
|
||||||
spell.setSpellbook(spellbook);
|
spell.setSpellbook(spellbook);
|
||||||
@ -21,16 +20,10 @@ public class App {
|
|||||||
dao.persist(wizard);
|
dao.persist(wizard);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void queryData(WizardDao dao) {
|
public static void queryData() {
|
||||||
List<Wizard> wizards = dao.findAll();
|
MagicService magicService = new MagicServiceImpl();
|
||||||
for (Wizard w: wizards) {
|
for (Wizard w: magicService.findAllWizards()) {
|
||||||
System.out.println(w);
|
System.out.println(w);
|
||||||
for (Spellbook spellbook: w.getSpellbooks()) {
|
|
||||||
System.out.println(spellbook);
|
|
||||||
for (Spell spell: spellbook.getSpells()) {
|
|
||||||
System.out.println(spell);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
16
dao/src/main/java/com/iluwatar/Dao.java
Normal file
16
dao/src/main/java/com/iluwatar/Dao.java
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
package com.iluwatar;
|
||||||
|
|
||||||
|
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();
|
||||||
|
}
|
@ -10,7 +10,7 @@ import org.hibernate.Transaction;
|
|||||||
import org.hibernate.cfg.Configuration;
|
import org.hibernate.cfg.Configuration;
|
||||||
import org.hibernate.criterion.Restrictions;
|
import org.hibernate.criterion.Restrictions;
|
||||||
|
|
||||||
public abstract class DaoBase<E extends BaseEntity> {
|
public abstract class DaoBaseImpl<E extends BaseEntity> implements Dao<E> {
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
protected Class<E> persistentClass = (Class<E>) ((ParameterizedType) getClass()
|
protected Class<E> persistentClass = (Class<E>) ((ParameterizedType) getClass()
|
||||||
@ -36,7 +36,8 @@ public abstract class DaoBase<E extends BaseEntity> {
|
|||||||
return sessionFactory.openSession();
|
return sessionFactory.openSession();
|
||||||
}
|
}
|
||||||
|
|
||||||
E find(Long id) {
|
@Override
|
||||||
|
public E find(Long id) {
|
||||||
Session session = getSession();
|
Session session = getSession();
|
||||||
Transaction tx = null;
|
Transaction tx = null;
|
||||||
E result = null;
|
E result = null;
|
||||||
@ -57,7 +58,8 @@ public abstract class DaoBase<E extends BaseEntity> {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
void persist(E entity) {
|
@Override
|
||||||
|
public void persist(E entity) {
|
||||||
Session session = getSession();
|
Session session = getSession();
|
||||||
Transaction tx = null;
|
Transaction tx = null;
|
||||||
try {
|
try {
|
||||||
@ -74,7 +76,8 @@ public abstract class DaoBase<E extends BaseEntity> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
E merge(E entity) {
|
@Override
|
||||||
|
public E merge(E entity) {
|
||||||
Session session = getSession();
|
Session session = getSession();
|
||||||
Transaction tx = null;
|
Transaction tx = null;
|
||||||
E result = null;
|
E result = null;
|
||||||
@ -93,7 +96,8 @@ public abstract class DaoBase<E extends BaseEntity> {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
void delete(E entity) {
|
@Override
|
||||||
|
public void delete(E entity) {
|
||||||
Session session = getSession();
|
Session session = getSession();
|
||||||
Transaction tx = null;
|
Transaction tx = null;
|
||||||
try {
|
try {
|
||||||
@ -110,7 +114,8 @@ public abstract class DaoBase<E extends BaseEntity> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
List<E> findAll() {
|
@Override
|
||||||
|
public List<E> findAll() {
|
||||||
Session session = getSession();
|
Session session = getSession();
|
||||||
Transaction tx = null;
|
Transaction tx = null;
|
||||||
List<E> result = null;
|
List<E> result = null;
|
13
dao/src/main/java/com/iluwatar/MagicService.java
Normal file
13
dao/src/main/java/com/iluwatar/MagicService.java
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
package com.iluwatar;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public interface MagicService {
|
||||||
|
|
||||||
|
List<Wizard> findAllWizards();
|
||||||
|
|
||||||
|
List<Spellbook> findAllSpellbooks();
|
||||||
|
|
||||||
|
List<Spell> findAllSpells();
|
||||||
|
|
||||||
|
}
|
21
dao/src/main/java/com/iluwatar/MagicServiceImpl.java
Normal file
21
dao/src/main/java/com/iluwatar/MagicServiceImpl.java
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
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();
|
||||||
|
}
|
||||||
|
}
|
@ -4,8 +4,6 @@ import javax.persistence.Column;
|
|||||||
import javax.persistence.Entity;
|
import javax.persistence.Entity;
|
||||||
import javax.persistence.GeneratedValue;
|
import javax.persistence.GeneratedValue;
|
||||||
import javax.persistence.Id;
|
import javax.persistence.Id;
|
||||||
import javax.persistence.Inheritance;
|
|
||||||
import javax.persistence.InheritanceType;
|
|
||||||
import javax.persistence.JoinColumn;
|
import javax.persistence.JoinColumn;
|
||||||
import javax.persistence.ManyToOne;
|
import javax.persistence.ManyToOne;
|
||||||
import javax.persistence.Table;
|
import javax.persistence.Table;
|
||||||
|
@ -1,5 +0,0 @@
|
|||||||
package com.iluwatar;
|
|
||||||
|
|
||||||
public class SpellDao extends DaoBase<Spell> {
|
|
||||||
|
|
||||||
}
|
|
5
dao/src/main/java/com/iluwatar/SpellDaoImpl.java
Normal file
5
dao/src/main/java/com/iluwatar/SpellDaoImpl.java
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
package com.iluwatar;
|
||||||
|
|
||||||
|
public class SpellDaoImpl extends DaoBaseImpl<Spell> {
|
||||||
|
|
||||||
|
}
|
@ -9,8 +9,6 @@ import javax.persistence.Entity;
|
|||||||
import javax.persistence.FetchType;
|
import javax.persistence.FetchType;
|
||||||
import javax.persistence.GeneratedValue;
|
import javax.persistence.GeneratedValue;
|
||||||
import javax.persistence.Id;
|
import javax.persistence.Id;
|
||||||
import javax.persistence.Inheritance;
|
|
||||||
import javax.persistence.InheritanceType;
|
|
||||||
import javax.persistence.JoinColumn;
|
import javax.persistence.JoinColumn;
|
||||||
import javax.persistence.ManyToOne;
|
import javax.persistence.ManyToOne;
|
||||||
import javax.persistence.OneToMany;
|
import javax.persistence.OneToMany;
|
||||||
@ -48,7 +46,7 @@ public class Spellbook extends BaseEntity {
|
|||||||
@JoinColumn(name="WIZARD_ID_FK", referencedColumnName="WIZARD_ID")
|
@JoinColumn(name="WIZARD_ID_FK", referencedColumnName="WIZARD_ID")
|
||||||
private Wizard wizard;
|
private Wizard wizard;
|
||||||
|
|
||||||
@OneToMany(mappedBy = "spellbook", orphanRemoval = true, fetch = FetchType.EAGER, cascade = CascadeType.ALL)
|
@OneToMany(mappedBy = "spellbook", orphanRemoval = true, fetch = FetchType.LAZY, cascade = CascadeType.ALL)
|
||||||
private Set<Spell> spells;
|
private Set<Spell> spells;
|
||||||
|
|
||||||
public String getName() {
|
public String getName() {
|
||||||
|
@ -1,5 +0,0 @@
|
|||||||
package com.iluwatar;
|
|
||||||
|
|
||||||
public class SpellbookDao extends DaoBase<Spellbook> {
|
|
||||||
|
|
||||||
}
|
|
5
dao/src/main/java/com/iluwatar/SpellbookDaoImpl.java
Normal file
5
dao/src/main/java/com/iluwatar/SpellbookDaoImpl.java
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
package com.iluwatar;
|
||||||
|
|
||||||
|
public class SpellbookDaoImpl extends DaoBaseImpl<Spellbook> {
|
||||||
|
|
||||||
|
}
|
@ -9,8 +9,6 @@ import javax.persistence.Entity;
|
|||||||
import javax.persistence.FetchType;
|
import javax.persistence.FetchType;
|
||||||
import javax.persistence.GeneratedValue;
|
import javax.persistence.GeneratedValue;
|
||||||
import javax.persistence.Id;
|
import javax.persistence.Id;
|
||||||
import javax.persistence.Inheritance;
|
|
||||||
import javax.persistence.InheritanceType;
|
|
||||||
import javax.persistence.OneToMany;
|
import javax.persistence.OneToMany;
|
||||||
import javax.persistence.Table;
|
import javax.persistence.Table;
|
||||||
|
|
||||||
@ -42,7 +40,7 @@ public class Wizard extends BaseEntity {
|
|||||||
|
|
||||||
private String name;
|
private String name;
|
||||||
|
|
||||||
@OneToMany(mappedBy = "wizard", orphanRemoval = true, fetch = FetchType.EAGER, cascade = CascadeType.ALL)
|
@OneToMany(mappedBy = "wizard", orphanRemoval = true, fetch = FetchType.LAZY, cascade = CascadeType.ALL)
|
||||||
private Set<Spellbook> spellbooks;
|
private Set<Spellbook> spellbooks;
|
||||||
|
|
||||||
public String getFirstName() {
|
public String getFirstName() {
|
||||||
|
@ -1,5 +0,0 @@
|
|||||||
package com.iluwatar;
|
|
||||||
|
|
||||||
public class WizardDao extends DaoBase<Wizard> {
|
|
||||||
|
|
||||||
}
|
|
5
dao/src/main/java/com/iluwatar/WizardDaoImpl.java
Normal file
5
dao/src/main/java/com/iluwatar/WizardDaoImpl.java
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
package com.iluwatar;
|
||||||
|
|
||||||
|
public class WizardDaoImpl extends DaoBaseImpl<Wizard> {
|
||||||
|
|
||||||
|
}
|
Reference in New Issue
Block a user