jpql

How can I count the number of result groups in QueryDSL?

故事扮演 提交于 2019-12-07 20:58:48
问题 How can I implement a count-of-groups in QueryDSL (in Java)? Background I'm implementing a paged search on a table, where I want to group the results before returning them. In addition to the usual LIMIT x OFFSET y query, I also want to receive the total row count. This is the (simplified) SQL query for one page: SELECT x, y, MAX(z) FROM tasks WHERE y > 10 GROUP BY x, y LIMIT 10 OFFSET 0 To retrieve the number of rows, I tried to use a naive COUNT(*) instead of the x, y, MAX(z) like this:

JPQL (JPA) Find Object if list have intersection

十年热恋 提交于 2019-12-07 16:17:18
问题 I have two Classes each of which hold a list of Labels. Now I want to find every object of ClassA that holds any item of the list of ClassB . Is this possible with JPQL? Or using a single query? public class ClassA { private List<Label> labels; } public class ClassB { private List<Label> labels; } @Repository public interface ClassARepository extends JpaRepository<ClassA, Long> { @Query("SELECT c FROM ClassA c WHERE :labels ____ c.labels") public List<ClassA> findAllByLabels(@Param("labels")

optional parameters jpa 2.1

感情迁移 提交于 2019-12-07 15:26:14
问题 I have this class, @Entity public class Message { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private long id; @Column( nullable = false ) private String mobile; @Column( nullable = false ) private String message; @Column( nullable = false ) private Lang lang; @Column( nullable = false ) private int status; @Column( nullable = false ) private Calendar creationDate; ... } and I would like to be able to query the table with optional parameters from a form. I'm using JPA 2.1 and

Why “SELECT c” instead of “SELECT * ” in JPQL?

随声附和 提交于 2019-12-07 13:46:52
问题 Example query in JPQL looks like this: SELECT c FROM Customer c; I read JPA specification, books and I can't find what this c means. In SQL we simply write: SELECT * FROM customer; I know we can use this c as alias i.e. in WHERE clause like this: SELECT c FROM Customer c WHERE c.name = ... but I still don't understand what this c actually is, how to call it (alias? object?) and why it has to be just after SELECT instead of *. 回答1: SQL returns rows, and rows contain columns. JPQL is a bit more

Automatically Add criteria on each Spring Jpa Repository call

无人久伴 提交于 2019-12-07 11:37:35
问题 I'm writing a library that wraps the JpaRepository interface of Spring Data Jpa because I want to add the same criteria automatically to all JpaRepository DB calls (something like and where t > something ). For example findById function of JpaRepository under the hood will be translated to find by id and where t > something Is it possible? and if so, how do I do it? Thanks! 回答1: A long time ago this was planned, but the team came to the conclusion that it really doesn't seem possible to do it

JPA / JPQL - bulk update

喜夏-厌秋 提交于 2019-12-07 11:12:47
问题 I have to perform a bulk update on a table. Making a fast example : UPDATE Book b SET b.amount = b.amount + 1 WHERE b IN ( :books ) The problem is that b.amount can be or a NULL value or an int, and if there is a NULL value it should behave as b.amount would be equal to 1. Is there any "cast" in JPA/JPQL or any other way to work-around this problem, Thank you in advance, Regards, P 回答1: You should be able to use COALESCE : UPDATE Book b SET b.amount = COALESCE(b.amount, 1) + 1 WHERE b IN (

Hibernate JPQL throws Update/delete queries cannot be typed

我只是一个虾纸丫 提交于 2019-12-07 09:45:53
问题 I'm trying to delete an entity using HQL and it's failing. TypedQuery<Seller> query = Seller.entityManager().createQuery( "DELETE FROM Seller AS o WHERE o.company=:company AND o.id=:id", Seller.class); query.setParameter("company", company); query.setParameter("id", id); int result = query.executeUpdate(); The stacktrace I'm getting: Update/delete queries cannot be typed; nested exception is java.lang.IllegalArgumentException: Update/delete queries cannot be typed at org.springframework.orm

How to filter child collection in JPQL query?

戏子无情 提交于 2019-12-07 08:04:10
问题 I've the following DB model: Category -< ProductCategory >- Product -< Variant ( Category has many-to-many relationship with Product and Product has one-to-many relationship with Variant ) Now I need to get all Category records that have product with active variants. I'm getting these objects via the following JPQL query: @Query("select distinct c from Category c join c.products as p join p.variants as pv where pv.active = true") It works well - returns categories accurately - however every

JPA: how do you get/print the JPQL query string behind a (typed) query after parameters have been set?

谁都会走 提交于 2019-12-07 04:30:51
问题 How do you get/print the JPQL query string behind a (typed) query, that is after parameters have been set? (e.g. for debugging purposes) A simple toString() doesn't seem to do the trick... Thanks 回答1: There is no such thing as "the final JPQL that ultimately gets translated to the final SQL". How a JPA implementation generates the SQL is down to it, and parameters in general will never be substituted into any String. SQL is generated from expression trees etc not a String. If you want param

How to set collection items for in-clause in jpql?

北慕城南 提交于 2019-12-07 02:02:25
问题 Is there a possiblity in JPA 2.0 to set a collection for in-clause in jpql-query? (I'm using EclipseLink) The next example fails: TypedQuery<Person> q = em.createQuery("select p from Person p where p.name in (?1)", Person.class); List<String> names = Arrays.asList(new String[] { "Bill Gates", "Steve Jobs" }); // THIS FAILS q.setParameter(1, names); List<Person> persons = q.getResultList(); for (Person p: persons) { System.out.println(p.getName()); } Is there another way to do it? 回答1: Here is