jpql

How to do a timestamp comparison with JPA query?

浪子不回头ぞ 提交于 2019-12-04 03:32:33
We need to make sure only results within the last 30 days are returned for a JPQL query. An example follows: Date now = new Date(); Timestamp thirtyDaysAgo = new Timestamp(now.getTime() - 86400000*30); Query query = em.createQuery( "SELECT msg FROM Message msg "+ "WHERE msg.targetTime < CURRENT_TIMESTAMP AND msg.targetTime > {ts, '"+thirtyDaysAgo+"'}"); List result = query.getResultList(); Here is the error we receive: <openjpa-1.2.3-SNAPSHOT-r422266:907835 nonfatal user error> org.apache.openjpa.persistence.ArgumentException: An error occurred while parsing the query filter 'SELECT msg FROM

JPA JPQL: SELECT NEW with COUNT, GROUP BY and ORDER BY

徘徊边缘 提交于 2019-12-04 03:31:45
问题 There are 2 tables / entities: AppleTree and Apples . An apple tree produces 0...n apples. Each apple is an entity / row of the second table and references the apple tree that has produced it ( ManyToOne ). I want to generate a "high score" report on the most productive apple trees. It should be ordered by the COUNT column in descending order: APPLE TREE | COUNT(A) --------------------- 10304 | 1000 72020 | 952 31167 | 800 In order to handle these results, I created a non-entity bean that

JPQL JOINS with nested SELECT

主宰稳场 提交于 2019-12-04 03:31:38
问题 Can I do something like this on JPQL? SELECT NEW com.MyDTO(p.a, p.b, q.c, q.d) FROM (SELECT r.* FROM MyDTO1 r ) p LEFT OUTER JOIN (SELECT s.* FROM MyDTO2 s ) q ON p.x = q.y or similar? (Above query has mixed with native and JPQL, so don't misunderstand) I'm having a problem with this part I think. FROM (SELECT r.* FROM MyDTO1 r ) p When I'm trying to execute I'm getting this error. Exception Description: Syntax error parsing the query [.....], unexpected token [(] Thank you! 回答1: No, you can

How to have multiple conditions in JPQL join

廉价感情. 提交于 2019-12-04 01:17:40
问题 I want to join two tables using JPQL : SELECT * FROM A LEFT JOIN B ON A.ID = B.A_ID AND B.lng = 'en' The important part is AND B.lng = 'en' Is this possible in JPQL ? 回答1: JPA 2.0 does not support an ON clause, but the JPA 2.1 draft does. EclipseLink 2.4 supports an ON clause. See, http://wiki.eclipse.org/EclipseLink/UserGuide/JPA/Basic_JPA_Development/Querying/JPQL#ON 回答2: Yes it is possible, there is a similar question here. However, if your entities are mapped, you should be able to access

RIGHT JOIN in JPQL

做~自己de王妃 提交于 2019-12-04 00:55:36
问题 I have the following JPA entities: @Entity class UserClient{ @Id @GeneratedValue(strategy = GenerationType.AUTO) private long id; } @Entity class UserAccess{ @Id @GeneratedValue(strategy = GenerationType.AUTO) private long id; @ManyToOne(optional = false, cascade = { CascadeType.REFRESH }) private UserClient user; @Temporal(TemporalType.TIMESTAMP) private Date accessTs; } Now I wanted to run a JPQL query to get the list of the users with their last access date. Unfortunately the following

JPQL Like Case Insensitive

纵饮孤独 提交于 2019-12-03 22:21:52
I want to search data in User table by name case insensitive. @Repository public interface UserRepository extends JpaRepository<User, Long> { @Query("select u from User u where lower(u.name) like %lower(?1)%") public List<User> findByNameFree(String name); } I got an error: unexpected token: % . Where should I place '%'? You can use the concat operator: @Query("select u from User u where lower(u.name) like lower(concat('%', ?1,'%'))") public List<User> findByNameFree(String name); or with a named parameter: @Query("select u from User u where lower(u.name) like lower(concat('%', :nameToFind,'%'

JPQL query with WHERE on nested fields

◇◆丶佛笑我妖孽 提交于 2019-12-03 19:59:28
问题 I have a java entity class UserBean with a list of events: @OneToMany private List<EventBean> events; EventBean has Date variable: @Temporal(javax.persistence.TemporalType.TIMESTAMP) private Date eventDate; Now in UserBean I want to create a NamedQuery that returns all dates that fall within a specific range: @NamedQuery(name="User.findEventsWithinDates", query="SELECT u.events FROM UserBean u WHERE u.name = :name AND u.events.eventDate > :startDate AND u.events.eventDate < :endDate") The

JPA concat operator

ε祈祈猫儿з 提交于 2019-12-03 19:43:58
问题 Is there a JPA concat operator for string concatenation? I know there is a JPA CONCAT function, however its ugly to use for concatenating multiple strings. CONCAT(CONCAT(CONCAT(cola,colb),colc),cold) Vendors like Oracle offer || some other like Microsoft offer + . Is there a standard JPA concatenation operator so that I could create a query like cola || colb || colc || cold I tried + using openjpa with SQL Server, however it seems to be invalid JPQL. I couldn't find anything regarding such an

TypedQuery instead of normal Query in JPA

混江龙づ霸主 提交于 2019-12-03 17:47:43
问题 Is it possible to write this Query as a TypedQuery and let the two Long's run into a Object with two public Long fields inside. Query q = em.createQuery( "SELECT c.id, COUNT(t.id) " + "FROM PubText t " + "JOIN t.comm c " + "WHERE c.element = ?1 " + "GROUP BY c.id"); q.setParameter(1, e); List<?> rl = q.getResultList(); Iterator<?> it = rl.iterator(); HashMap<Long, Long> res = new HashMap<Long, Long>(); while (it.hasNext()) { Object[] n = (Object[]) it.next(); res.put((Long)n[0], (Long)n[1]);

join more than one table in spring jparepository

本秂侑毒 提交于 2019-12-03 17:14:23
I am trying to fetch record by doing a join. I am new to spring jparepository. I understand that there is separate repository for each entity(table) where when i implement i need to define the entity and datatype of primary key. Could anyone please suggest how can I fetch record by joining two tables. I have two repo as below: public interface AEntityRepository extends JpaRepository<AEntity, Integer> public interface BEntityRepository extends JpaRepository<BEntity, Integer> I want to join above two entity(AEntity, BEntity). I know I can have custom query using something like below: @Query(