jpql

JPA Query MONTH/YEAR functions

社会主义新天地 提交于 2019-12-17 18:51:31
问题 How can I write a JPA query using MONTH function just like sql query? @NamedQuery(name="querybymonth", query="select t from table1 t where MONTH(c_Date) = 5") When I use the above pattern for query, I get an error: unexpected token - MONTH . 回答1: If you are using EclipseLink (2.1) you can use the FUNC() function to call any database function that is not defined in the JPA JPQL spec. i.e. FUNC('MONTH', c_Date) In JPA 2.1 (EclipseLink 2.5) the FUNCTION syntax becomes part of the specification

Recursive JPA query?

丶灬走出姿态 提交于 2019-12-17 18:33:42
问题 Does JPA 2 have any mechanism for running recursive queries? Here's my situation: I have an entity E, which contains an integer field x. It also may have children of type E, mapped via @OneToMany. What I'd like to do is find an E by primary key, and get its value of x, along with the x values of all its descendants. Is there any way to do this in a single query? I'm using Hibernate 3.5.3, but I'd prefer not to have any explicit dependencies on Hibernate APIs. EDIT: According to this item,

How do I do a “deep” fetch join in JPQL?

醉酒当歌 提交于 2019-12-17 16:44:10
问题 I don't think I will ever fully understand fetch joins. I have a query where I'm attempting to eagerly "inflate" references down two levels. That is, my A has an optional Collection of B s, and each B has either 0 or 1 C . The size of the B collection is known to be small (10-20 tops). I'd like to prefetch this graph. A 's B relationship is marked as FetchType.LAZY and is optional. B 's relationship to C is also optional and FetchType.LAZY . I was hoping I could do: SELECT a FROM A a LEFT

Selecting where an entity contains a list thats a subset of another list

对着背影说爱祢 提交于 2019-12-17 16:11:02
问题 I am writing a JPQL query and i have the following scenario. I have a Question entity which contains a list of Tags. I would like to select all Questions that contains a given List of tags. How do i do this with JPA? I would like to do something like SELECT x FROM Question x WHERE x.tags 'contains all' :tags 回答1: [This searches for ANY not ALL; please refer other correct answers.] You can set list as a parameter. SELECT x FROM Question x WHERE x.tags IN :tags Also try using (:tags), as it

Spring Data optional parameter in query method

╄→尐↘猪︶ㄣ 提交于 2019-12-17 15:46:40
问题 I want to write some query methods in repository layer. This method must ignore null parameters. For example: List<Foo> findByBarAndGoo(Bar barParam, @optional Goo gooParam); This method must be return Foo by this condition: bar == barParam && goo == gooParam; if gooParam not null. if gooParam was null then condition change to: bar == barParam; Is there any solution? Can someone help me? 回答1: Too late to answer. Not sure about relationship between Bar and Goo . Check if Example can helps you.

Using JPA 2.0 Criteria API and cast causes generated JPQL to fail in Hibernate

允我心安 提交于 2019-12-17 14:47:05
问题 I am a first time user of the new JPA 2.0 Criteria API and I 'm running into a problem when I need to cast a number field to String to compare it with a String parameter. Reason is that I want to search for partial numbers, so I use a 'like' on the CriteriaBuilder. Here's a code sample: CriteriaBuilder cb = getEntityManager().getCriteriaBuilder(); CriteriaQuery<ParcelDO> cq = cb.createQuery(ParcelDO.class); Root<ParcelDO> parcelDO = cq.from(ParcelDO.class); cq.select(parcelDO); String

JPQL SELECT between date statement [closed]

为君一笑 提交于 2019-12-17 11:17:35
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 3 years ago . I would like to convert this SQL statement to a JPQL equivalent. SELECT * FROM events WHERE events_date BETWEEN '2011-01-01' AND '2011-03-31'; This correctly retrieves the information from the table events . In my Events entity @Column(name = "events_date") @Temporal(TemporalType.DATE) private Date eventsDate;

JPA: JOIN in JPQL

怎甘沉沦 提交于 2019-12-17 09:20:45
问题 I thought I know how to use JOIN in JPQL but apparently not. Can anyone help me? select b.fname, b.lname from Users b JOIN Groups c where c.groupName = :groupName This give me Exception org.eclipse.persistence.exceptions.JPQLException Exception Description: Syntax error parsing the query Internal Exception: org.eclipse.persistence.internal.libraries.antlr.runtime.EarlyExitException Users have a OneToMany relationship with Groups . Users.java @Entity public class Users implements Serializable{

Limit number of results in JPQL

橙三吉。 提交于 2019-12-17 07:26:40
问题 How it is possible to limit the number of results retrieved from a database? select e from Entity e /* I need only 10 results for instance */ 回答1: You can try like this giving 10 results to be fetched explicitly. entityManager.createQuery(JPQL_QUERY) .setParameter(arg0, arg1) .setMaxResults(10) .getResultList(); It will automatically create native query in back-end to retrieve specific number of results, if the backend supports it, and otherwise do the limit in memory after getting all

IN-clause in HQL or Java Persistence Query Language

时间秒杀一切 提交于 2019-12-17 06:25:35
问题 I have the following parametrised JPA, or Hibernate, query: SELECT entity FROM Entity entity WHERE name IN (?) I want to pass the parameter as an ArrayList<String>, is this possible? Hibernate current tells me, that java.lang.ClassCastException: java.util.ArrayList cannot be cast to java.lang.String Is this possible at all? ANSWER : Collections as parameters only work with named parameters like " :name ", not with JDBC style parameters like " ? ". 回答1: Are you using Hibernate's Query object,