Last active
August 28, 2023 09:48
-
-
Save pveeckhout/8570701 to your computer and use it in GitHub Desktop.
Generic implementation of JPA DAOs and Services with Spring 4.0.0 requiring no additional code written for basic CRUD.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.pietervaneeckhout.dao.impl; | |
import com.pietervaneeckhout.dao.GenericDao; | |
import java.util.List; | |
import javax.persistence.EntityManager; | |
import javax.persistence.PersistenceContext; | |
import javax.persistence.criteria.CriteriaBuilder; | |
import javax.persistence.criteria.CriteriaQuery; | |
import javax.persistence.criteria.Root; | |
/** | |
* | |
* @author pveeckhout | |
*/ | |
public abstract class AbstractGenericDAOImpl<T> implements GenericDao<T> { | |
private final Class<T> type; | |
@PersistenceContext | |
private EntityManager entityManager; | |
public AbstractGenericDAOImpl(Class<T> type) { | |
super(); | |
this.type = type; | |
} | |
@Override | |
public List<T> findAll() { | |
CriteriaBuilder builder = this.entityManager.getCriteriaBuilder(); | |
CriteriaQuery<T> cq = builder.createQuery(type); | |
Root<T> root = cq.from(type); | |
cq.select(root); | |
return this.entityManager.createQuery(cq).getResultList(); | |
} | |
@Override | |
public T update(T object) { | |
return this.entityManager.merge(object); | |
} | |
@Override | |
public T get(Long id) { | |
return this.entityManager.find(type, id); | |
} | |
@Override | |
public void delete(T object) { | |
this.entityManager.remove(this.entityManager.merge(object)); | |
} | |
@Override | |
public void insert(T object) { | |
this.entityManager.persist(object); | |
} | |
@Override | |
public boolean exists(Long id) { | |
T entity = this.entityManager.find(this.type, id); | |
return entity != null; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.pietervaneeckhout.service.impl; | |
import com.pietervaneeckhout.dao.impl.AbstractGenericDAOImpl; | |
import com.pietervaneeckhout.service.GenericManager; | |
import java.util.List; | |
import javax.transaction.Transactional; | |
import org.springframework.beans.factory.annotation.Autowired; | |
/** | |
* | |
* @author pveeckhout | |
*/ | |
public abstract class GenericManagerImpl<T, T_DAO extends GenericDAOImpl<T>> implements GenericManager<T> { | |
@Autowired | |
private T_DAO tDAO; | |
@Override | |
public List<T> findAll() { | |
return tDAO.findAll(); | |
} | |
@Override | |
@Transactional | |
public T update(T object) { | |
return tDAO.update(object); | |
} | |
@Override | |
public T get(Long id) { | |
return tDAO.get(id); | |
} | |
@Override | |
@Transactional | |
public void delete(T object) { | |
tDAO.delete(object); | |
} | |
@Override | |
@Transactional | |
public void insert(T object) { | |
tDAO.insert(object); | |
} | |
@Override | |
public boolean exists(Long id) { | |
return tDAO.exists(id); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DAO: | |
AbstractGenericDAOImpl implemets GenericDAO interface | |
classDAO interface extends GenericDAO interface | |
classDAOImpl extends AbstractGenericDAOImpl implements classDAO interface | |
Services: | |
AbstractGenericManagerImpl implemets GenericManager interface | |
classManager interface extends GenericManager interface | |
classManagerImpl extends AbstractGenericmanagerImpl implements classManager interface |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* | |
* Using the Post class as example | |
* | |
* @author pveeckhout | |
*/ | |
public interface postDAO extends GenericDao<Post> { | |
} | |
@Repository | |
public class PostDAOImpl extends AbstractGenericDAOImpl<Post> implements postDAO { | |
public PostDAOImpl() { | |
super(Post.class); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.pietervaneeckhout.service; | |
import java.util.List; | |
/** | |
* | |
* @author pveeckhout | |
*/ | |
public interface GenericManager<T> { | |
public List<T> findAll(); | |
public T update(T object); | |
public T get(Long id); | |
public void delete(T object); | |
public void insert(T object); | |
public boolean exists(Long id); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.pietervaneeckhout.dao; | |
import java.util.List; | |
/** | |
* | |
* @author pveeckhout | |
* @param <T> The class to be persisted | |
*/ | |
public interface GenericDao<T> { | |
public List<T> findAll(); | |
public T update(T object); | |
public T get(Long id); | |
public void delete(T object); | |
public void insert(T object); | |
public boolean exists(Long id); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* | |
* Using the Post class as example | |
* | |
* @author pveeckhout | |
*/ | |
public interface PostManager extends GenericManager<Post> { | |
} | |
@Service | |
public class PostManagerImpl extends GenericManagerImpl<Post, PostDAOImpl> implements PostManager { | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment