jpql

JPQL IN clause: Java-Arrays (or Lists, Sets…)?

独自空忆成欢 提交于 2019-12-17 04:43:09
问题 I would like to load all objects that have a textual tag set to any of a small but arbitrary number of values from our database. The logical way to go about this in SQL would be to build an "IN" clause. JPQL allows for IN, but it seems to require me to specify every single parameter to IN directly (as in, "in (:in1, :in2, :in3)"). Is there some way to specify an array, or a list (or some other container) that should be unrolled to the values of an IN clause? 回答1: I'm not sure for JPA 1.0 but

Adding IN clause List to a JPA Query

*爱你&永不变心* 提交于 2019-12-17 02:59:58
问题 I have built a NamedQuery that looks like this: @NamedQuery(name = "EventLog.viewDatesInclude", query = "SELECT el FROM EventLog el WHERE el.timeMark >= :dateFrom AND " + "el.timeMark <= :dateTo AND " + "el.name IN (:inclList)") What I want to do is fill in the parameter :inclList with a list of items instead of one item. For example if I have a new List<String>() { "a", "b", "c" } how do I get that in the :inclList parameter? It only lets me codify one string. For example: setParameter(

Using group by and having in a JPA query

徘徊边缘 提交于 2019-12-14 03:48:58
问题 How would you do the following using the JPA query language? select * from person where email in (select email from person group by email having count(email)>1) 回答1: Finally found a solution: select p from Person p where p.email in ( select q.email from Person q group by q.email having count(q.email)>1) order by p.email, p.id 回答2: From this link, there's a explanation of the general structure of a JPA SELECT Query: SELECT ... FROM ... [WHERE ...] [GROUP BY ... [HAVING ...]] [ORDER BY ...] 来源:

JPA/Hibernate + HQL/JPQL: select DTO with BigDecimal parameter

独自空忆成欢 提交于 2019-12-14 02:34:08
问题 We are using JPA with hibernate as the implementation. Suppose I have the following DTO: public class SupplierInfoDto{ private String supplierName; private BigDecimal remainingFinances; public SupplierInfoDto(String supplierName, BigDecimal remainingFinances){ this.supplierName = supplierName; this.remainingFinances = remainingFinances; } // getters / setters } I cannot seem to get hibernate to properly find this constructor. I first tried the following query (the model is more complicated

Nice way to select a tuple using JPA

≡放荡痞女 提交于 2019-12-14 00:47:59
问题 final List<Tuple> data = em.createQuery("SELECT p.id AS i, p.membership AS m FROM Player p WHERE p.id IN :ids", Tuple.class) .setParameter("ids", ids) .getResultList(); This gives the error " Cannot create TypedQuery for query with more than one return ". I could work around that by leaving out the type parameter (and using Object[] instead of Tuple, as I later found out): @SuppressWarnings("unchecked") final List<Object[]> data = em.createQuery("SELECT p.id AS i, p.membership AS m FROM

org.hibernate.QueryException: could not resolve property:

▼魔方 西西 提交于 2019-12-13 22:50:25
问题 I have been trying to solve this one since morning. But i am unable to. Query query = em.createQuery("Update ABC p set p.sync_status=14 where p.eventhistoryid in (select c.eventhistoryid from ABC c where c.sync_status=0 and c.receivedtimestamp >= getTimeStampAfterDeductingHours(24)"); ABC entity class contains the below column: @Column(name="sync_status") private short syncStatus = 0; this column is SMALLINT TYPE and NOT NULL. Getting the below error: org.hibernate.QueryException: could not

Counting Null values in JPQL

╄→尐↘猪︶ㄣ 提交于 2019-12-13 22:22:57
问题 i want to count null values in JPQL but count (case when "column" is null then.. end) doesn't work it work only on MYSQL ,i don't want to use count(*) what is the solution ?? String jpql ="select c.commande.user.login ,count(CASE WHEN c.commande.commandeTms is Null THEN 1 else 0 END) AS count1 from Designation c GROUP BY c.commande.user.login"; here my database 回答1: Here you go : I have used CriteriaBuilder to count them ... as a result, you get a List of Tuple containing the user login and

Why does JPA join return too many results?

安稳与你 提交于 2019-12-13 19:12:33
问题 If I query in SQL: select * from Profesor inner join Estudiantes on Profesor.id = Estudiante.id where Profesor.nombre = 'juan' and Estudiante.nombre = 'jose' This query return the profesor and the student. One profesor and one student. Just Profesor Juan with Jose as Student. Then if I Query in JPA: select p from Profesor p inner join p.estudiantes e where p.nombre = juan and e.nombre = jose. JPA will return profesor Juan with all the students not just the one I wanted and profesor

Kundera for Cassandra - Deleting record by row key

大兔子大兔子 提交于 2019-12-13 17:22:13
问题 I'm trying to delete specific record from database by row key. But when I try to execute this query: Query query = em.createQuery( "DELETE FROM User u WHERE u.userId = :u"); query.setParameter("u", userID).executeUpdate(); I got this exception: " Condition = is not suported for query on row key! ". Is there any workaround, or I missing something? 回答1: What you can do as a workaround is: Find using: User u = em.find(User.class, userId) and then, em.delete(u); 回答2: Also, http://groups.google

Escape reserved words in JPQL

若如初见. 提交于 2019-12-13 15:53:15
问题 I want to write JPQL, which returns all my entities. The problem is that entity is called "Case", and this is a restricted word. Does JPQL has some escape characters? How to write this JPQL? SELECT c from Case Thanks in advance for help. 来源: https://stackoverflow.com/questions/33144550/escape-reserved-words-in-jpql