Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class AbstractDao<T extends Serializable> {
- /*
- * http://nsinfra.blogspot.de/2013/03/jpa-20-criteria-query-with-hibernate.html
- * Criteria after JPA-2.0 standard, using entityManagerFactory as opposed to
- * http
- * ://stackoverflow.com/questions/9926363/how-to-write-this-query-using-jpa
- * -criteria-query
- * https://docs.jboss.org/hibernate/entitymanager/3.5/reference
- * /en/html/querycriteria.html
- *
- * Some more links:
- * http://stackoverflow.com/questions/14855181/spring-jpa-hibernate
- * http://what
- * -when-how.com/enterprise-javabeans-3-1/persistence-entitymanager
- * -enterprise-javabeans-3-1/
- */
- protected Logger log = Logger.getLogger(getEntityClass());
- @Autowired
- protected EntityManagerFactory entityManagerFactory;
- @PersistenceContext(type = PersistenceContextType.TRANSACTION)
- protected EntityManager entityManager;
- private FullTextEntityManager ftem;
- public Class<T> getEntityClass(){
- return (Class) ((ParameterizedType)getClass().getGenericSuperclass()).getActualTypeArguments()[0];
- }
- public List<T> findAll() {
- CriteriaBuilder cb = entityManager.getCriteriaBuilder();
- CriteriaQuery<T> criteria = cb.createQuery(getEntityClass());
- Root<T> root = criteria.from(getEntityClass());
- criteria.select(root);
- criteria.orderBy(cb.asc(root.get("id")));
- return entityManager.createQuery(criteria).getResultList();
- }
- public T findOne(Integer id) {
- return entityManager.find(getEntityClass(), id);
- }
- public void save(T entity) {
- entityManager.persist(entity);
- }
- public void update(T entity) {
- entityManager.merge(entity);
- }
- public void delete(T entity) {
- entityManager.remove(entity);
- }
- public FullTextEntityManager getFullTextEntityManager(){
- ftem = Search.getFullTextEntityManager(entityManager);
- return ftem;
- }
- }
- public class ExampleDao extends AbstractDao<Movie> {
- public Movie findOne(String id) {
- return entityManager.find(Movie.class, id);
- }
- public List<Movie> findSimilar(MovieDto dto){
- CriteriaBuilder cb = entityManager.getCriteriaBuilder();
- CriteriaQuery<Movie> criteria = cb.createQuery(Movie.class);
- Root<Movie> root = criteria.from(Movie.class);
- criteria.select(root);
- //select all movies that have the genre + any other attribute in common or any other attribute + any other attribute in common
- //this excludes a range of movies that share only one or several genres unfortunately
- //(genres OR actors OR directors OR writers) AND (actors OR directors OR writers)
- //find movies with similar name for prequel/sequel search
- criteria.where(
- cb.or(
- //no future releases please!
- cb.and(
- cb.or(getLikeExpressionForList(dto.getGenres(), cb, root, "genres"),
- getLikeExpressionForList(dto.getDirectors(), cb, root, "directors"),
- getLikeExpressionForList(dto.getActors(), cb, root, "actors"),
- getLikeExpressionForList(dto.getWriters(), cb, root, "writers")),
- cb.or(getLikeExpressionForList(dto.getDirectors(), cb, root, "directors"),
- getLikeExpressionForList(dto.getActors(), cb, root, "actors"),
- getLikeExpressionForList(dto.getWriters(), cb, root, "writers")),
- cb.lt(root.get("year"), Calendar.getInstance().get(Calendar.YEAR)+1)
- ),
- cb.like(root.get("name"), "%"+dto.getSimilarName()+"%")
- )
- );
- criteria.orderBy(cb.asc(root.get("id")));
- return entityManager.createQuery(criteria).getResultList();
- }
- /*
- * http://stackoverflow.com/questions/11138118/really-dynamic-jpa-criteriabuilder
- * http://stackoverflow.com/questions/12244799/correct-usage-of-jpa-criteria-api-predicates-and-where-method-of-criteriaquery
- * http://stackoverflow.com/questions/7811703/oracle-text-criteria-query-in-jpa
- */
- private Predicate getLikeExpressionForList(List<String> list, CriteriaBuilder cb, Root<Movie> root, String type) {
- List<Predicate> predicates = new ArrayList<Predicate>();
- if (list == null) {
- return cb.disjunction();
- }
- for (String string : list) {
- Predicate p = cb.like(root.get(type), "%" + string.trim() + "%");
- predicates.add(p);
- }
- return cb.or(predicates.toArray(new Predicate[predicates.size()]));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement