Advertisement
Guest User

Untitled

a guest
May 31st, 2015
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.19 KB | None | 0 0
  1. public class AbstractDao<T extends Serializable> {
  2.  
  3.     /*
  4.      * http://nsinfra.blogspot.de/2013/03/jpa-20-criteria-query-with-hibernate.html
  5.      * Criteria after JPA-2.0 standard, using entityManagerFactory as opposed to
  6.      * http
  7.      * ://stackoverflow.com/questions/9926363/how-to-write-this-query-using-jpa
  8.      * -criteria-query
  9.      * https://docs.jboss.org/hibernate/entitymanager/3.5/reference
  10.      * /en/html/querycriteria.html
  11.      *
  12.      * Some more links:
  13.      * http://stackoverflow.com/questions/14855181/spring-jpa-hibernate
  14.      * http://what
  15.      * -when-how.com/enterprise-javabeans-3-1/persistence-entitymanager
  16.      * -enterprise-javabeans-3-1/
  17.      */
  18.  
  19.     protected Logger log = Logger.getLogger(getEntityClass());
  20.    
  21.     @Autowired
  22.     protected EntityManagerFactory entityManagerFactory;
  23.  
  24.     @PersistenceContext(type = PersistenceContextType.TRANSACTION)
  25.     protected EntityManager entityManager;
  26.    
  27.     private FullTextEntityManager ftem;
  28.    
  29.     public Class<T> getEntityClass(){
  30.         return (Class) ((ParameterizedType)getClass().getGenericSuperclass()).getActualTypeArguments()[0];
  31.     }
  32.  
  33.     public List<T> findAll() {
  34.         CriteriaBuilder cb = entityManager.getCriteriaBuilder();
  35.         CriteriaQuery<T> criteria = cb.createQuery(getEntityClass());
  36.         Root<T> root = criteria.from(getEntityClass());
  37.         criteria.select(root);
  38.         criteria.orderBy(cb.asc(root.get("id")));
  39.         return entityManager.createQuery(criteria).getResultList();
  40.     }
  41.  
  42.     public T findOne(Integer id) {
  43.         return entityManager.find(getEntityClass(), id);
  44.     }
  45.  
  46.     public void save(T entity) {
  47.         entityManager.persist(entity);
  48.     }
  49.  
  50.     public void update(T entity) {
  51.         entityManager.merge(entity);
  52.     }
  53.  
  54.     public void delete(T entity) {
  55.         entityManager.remove(entity);
  56.     }
  57.    
  58.     public FullTextEntityManager getFullTextEntityManager(){
  59.         ftem = Search.getFullTextEntityManager(entityManager);
  60.         return ftem;
  61.     }
  62.  
  63. }
  64.  
  65. public class ExampleDao extends AbstractDao<Movie> {
  66.  
  67.     public Movie findOne(String id) {
  68.         return entityManager.find(Movie.class, id);
  69.     }
  70.  
  71.     public List<Movie> findSimilar(MovieDto dto){
  72.         CriteriaBuilder cb = entityManager.getCriteriaBuilder();
  73.         CriteriaQuery<Movie> criteria = cb.createQuery(Movie.class);
  74.         Root<Movie> root = criteria.from(Movie.class);
  75.         criteria.select(root);
  76.        
  77.         //select all movies that have the genre + any other attribute in common or any other attribute + any other attribute in common
  78.         //this excludes a range of movies that share only one or several genres unfortunately
  79.         //(genres OR actors OR directors OR writers) AND (actors OR directors OR writers)
  80.         //find movies with similar name for prequel/sequel search
  81.         criteria.where(
  82.                 cb.or(
  83.                     //no future releases please!
  84.                     cb.and(
  85.                             cb.or(getLikeExpressionForList(dto.getGenres(), cb, root, "genres"),
  86.                             getLikeExpressionForList(dto.getDirectors(), cb, root, "directors"),
  87.                             getLikeExpressionForList(dto.getActors(), cb, root, "actors"),
  88.                             getLikeExpressionForList(dto.getWriters(), cb, root, "writers")),
  89.                             cb.or(getLikeExpressionForList(dto.getDirectors(), cb, root, "directors"),
  90.                             getLikeExpressionForList(dto.getActors(), cb, root, "actors"),
  91.                             getLikeExpressionForList(dto.getWriters(), cb, root, "writers")),
  92.                             cb.lt(root.get("year"), Calendar.getInstance().get(Calendar.YEAR)+1)
  93.                                
  94.                         ),
  95.                     cb.like(root.get("name"), "%"+dto.getSimilarName()+"%")
  96.                 )
  97.         );
  98.         criteria.orderBy(cb.asc(root.get("id")));
  99.         return entityManager.createQuery(criteria).getResultList();
  100.     }
  101.  
  102.     /*
  103.      * http://stackoverflow.com/questions/11138118/really-dynamic-jpa-criteriabuilder
  104.      * http://stackoverflow.com/questions/12244799/correct-usage-of-jpa-criteria-api-predicates-and-where-method-of-criteriaquery
  105.      * http://stackoverflow.com/questions/7811703/oracle-text-criteria-query-in-jpa
  106.      */
  107.     private Predicate getLikeExpressionForList(List<String> list, CriteriaBuilder cb, Root<Movie> root, String type) {
  108.         List<Predicate> predicates = new ArrayList<Predicate>();
  109.         if (list == null) {
  110.             return cb.disjunction();
  111.         }
  112.        
  113.         for (String string : list) {
  114.             Predicate p = cb.like(root.get(type), "%" + string.trim() + "%");
  115.             predicates.add(p);
  116.         }
  117.         return cb.or(predicates.toArray(new Predicate[predicates.size()]));
  118.     }
  119.    
  120.    
  121.  
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement