1
SELECT * 
FROM 
  CUSTOMER_MASTER t0, 
  (SELECT t1.COMMNUMBER, t1.COMMTYPE FROM FOLLOWUP_GUIDES t1 WHERE (t1.STAGE_ID = '5')) t2
WHERE (t0.FOLL_UP_NUM = t2.COMMNUMBER 
and T0.FOLL_UP_TYPE=t2.COMMTYPE AND (t0.COMPANY_ID = 'C001'))

In the above oracle query i have to got the follupnum and folluptype in the combination commnumber and commtype. But is it possible to write this query in JPA?

4

3 回答 3

1

JPA 完全支持 SQL 查询。您可以使用 createNativeQuery(sql) 或 @NamedNativeQuery

一旦点击此链接

于 2013-02-21T06:31:58.173 回答
1

JPA 完全支持 SQL 查询。您可以使用 createNativeQuery(sql) 或 @NamedNativeQuery。

您可能还可以根据 JPQL 改写您的查询。

如果您使用的是 EclipseLink,它确实支持 JPQL 的 FROM 子句中的子选择,

http://wiki.eclipse.org/EclipseLink/UserGuide/JPA/Basic_JPA_Development/Querying/JPQL#Sub-selects_in_FROM_clause

于 2012-10-31T14:08:23.030 回答
0

假设您在 CUSTOMER_MASTER 和 FOLLOWUP_GUIDES 之间有外键

即假设

CUSTOMER_MASTER.FOLL_UP_NUM    is FK to   FOLLOWUP_GUIDES.COMMNUMBER 
CUSTOMER_MASTER.FOLL_UP_TYPE   is FK to   FOLLOWUP_GUIDES.COMMTYPE

然后你的查询

SELECT * 
FROM 
  CUSTOMER_MASTER t0, 
  (SELECT t1.COMMNUMBER, t1.COMMTYPE FROM FOLLOWUP_GUIDES t1 
   WHERE (t1.STAGE_ID = '5')) t2
WHERE (t0.FOLL_UP_NUM = t2.COMMNUMBER 
and T0.FOLL_UP_TYPE=t2.COMMTYPE AND (t0.COMPANY_ID = 'C001'))

相当于

SELECT t0.*
FROM CUSTOMER_MASTER t0 JOIN FOLLOWUP_GUIDES t1
WHERE t0.COMPANY_ID = 'C001'
AND   t1.STAGE_ID = '5'

这可以在 JPA 中很容易地完成,并且看起来与此 SQL 非常相似(仅使用对象名称和字段名称而不是表名称和属性名称)。

JPA JPQL 查询将如下所示:

SELECT t0
FROM CustomerMaster t0 JOIN FollowupGuides t1
WHERE t0.companyId = "C001"
AND   t1.stageId = "5"

也可以使用 JPA 子查询:

SELECT t0
FROM CustomerMaster t0 
WHERE t0.companyId = "C001"
AND   EXISTS (SELECT 1
              FROM FollowupGuides t1
              WHERE t1.commnumber = t0.follUpNum
              AND   t1.commtype = t0.FollUpType
              AND   t1.stageId = "5")

=B)

于 2012-11-02T03:52:45.303 回答