package com.emp.dao.respository;
import java.util.Collection;
import com.emp.entity.GenericEntity;
public interface GenericRespository {
public void persist(A e) throws Exception;
public void remove(A e) throws Exception;
public void update(A e) throws Exception;
public A find(String id) throws Exception;
public Collection findAll() throws Exception;
}
-----------------------------------------
package com.emp.dao.respository;
//All the methods going inside the EmployeeRespository
public interface GenericEmployeeRespository {
}
-----------------------------------------
package com.emp.dao.respository;
import java.util.Collection;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import javax.persistence.PersistenceException;
import javax.persistence.Query;
import com.emp.entity.GenericEntity;
import com.emp.entity.MyEntityManager;
public class Respository implements GenericRespository
{
protected EntityManager em;
private Class persistentClass;
public Respository(EntityManager em, Class persistentClass)
{
MyEntityManager e= new MyEntityManager();
this.em = e.getEm();
this.persistentClass = persistentClass;
}
@Override
public final void persist(A e) throws Exception
{
em.persist(e);
}
@Override
public final void remove(A e) throws Exception{
em.remove(e);
}
@Override
public final void update(A e) throws Exception{
}
@Override
public final A find(String id) throws Exception{
em.find(persistentClass, id);
return null;
}
@Override
public final Collection findAll() throws Exception{
Query query = em.createQuery("SELECT emp FROM "+ persistentClass.getSimpleName() + " AS emp");
Collection result = query.getResultList();
return result;
}
public Class getPersistentClass() {
return persistentClass;
}
public void setPersistentClass(Class persistentClass) {
this.persistentClass = persistentClass;
}
}
-----------------------------------------------------------
package com.emp.dao.respository;
import javax.persistence.EntityManager;
import com.emp.entity.Employee;
public class EmployeeRespository extends Respository
{
public EmployeeRespository(EntityManager entityManager)
{
super(entityManager, Employee.class);
}
}
------------------------------------------------------------
package com.emp.dao.respository;
//DaoRespository types to be created
public enum DaoRespositoryTypes {
EMPLOYEE,MANAGER,DEPARTMENT;
}
-----------------------------------------
package com.emp.dao.respository;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import com.emp.entity.GenericEntity;
//this is singleton object to create respositories....So entity manager becomes singleton as well
public class DaoRespositoryFactory {
private EntityManager em;
public Respository createDaoImpl(DaoRespositoryTypes type) throws Exception
{
Respository instance = null;
EntityManagerFactory factoty = Persistence.createEntityManagerFactory("myp");
this.em = factoty.createEntityManager();
switch (type) {
case EMPLOYEE:
instance = new EmployeeRespository(em);
break;
case MANAGER:
instance = new MangerRespository(em);
break;
case DEPARTMENT:
instance = new DepartmentRespository(em);
break;
default:
throw new Exception("There is no dao type assign...");
}
return instance;
}
}
No comments:
Post a Comment