sql - Get the Latest record for each person using Criteria Builder -


i know how generate query using criteria query, able jpql need jpa criteria query. query :

    select user_id, user_name,  borrowing_date     books_record b      (user_id, borrowing_time) in (     select br.user_id, max(br.borrowing_time)        books_record br        group br.user_id     ) 

following criteria should work, have not tested it

//sub query detachedcriteria subquery = detachedcriteria.forclass(booksrecord.class, "bk1"); projectionlist proj1 = projections.projectionlist(); proj1.add((projections.property("user_id").as("user_id")); proj1.add(projections.max("borrowing_time").as("borrowing_time")); proj1.add(projections.groupproperty("user_id")); subquery.setprojection(proj1);  //main query criteria mainquerycriteria = getsession().createcriteria(booksrecord.class, "bk2"); mainquerycriteria.add(restrictions.eqproperty("bk2.user_id", "bk1.user_id")); mainquerycriteria.add(restrictions.eqproperty("bk2.borrowing_time", "bk1.borrowing_time")); projectionlist proj2 = projections.projectionlist(); proj2.add((projections.property("user_id").as("user_id")); proj2.add(projections.property("user_name").as("user_name")); proj2.add(projections.property("borrowing_date").as("borrowing_date")); mainquerycriteria.setprojection(proj2); mainquerycriteria.setresulttransformer(transformers.aliastobean(booksrecord.class));  list<booksrecord> records = mainquerycriteria.list(); 

Comments

Popular posts from this blog

user interface - How to replace the Python logo in a Tkinter-based Python GUI app? -

objective c - Greedy NSProgressIndicator Allocation -

how to set an OCR language in Google Drive -