簡體   English   中英

具有 INNER JOIN 條件的 JPA 命名查詢失敗

[英]JPA Named query with INNER JOIN condition is failing

我正在嘗試使用實體類中的內部聯接條件創建命名查詢。 但是,應用程序無法啟動並出現異常。

實體:

@Entity
@Table(name = "fooentry")
@NamedQuery(name="query1", query="SELECT  foo.id, foo.name, foo.type, foo.action, foo.createdDateTime FROM FooEntity foo " +
        "INNER JOIN (SELECT  name, type, MAX(createdDateTime) AS recenttime FROM FooEntity GROUP BY  name, type) recententry " +
        "ON (foo.name = recententry.name AND foo.type = recententry.type) AND createdDateTime = recenttime AND isExported = false",
        lockMode=PESSIMISTIC_WRITE)
public class FooEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Long id;

    @Column(name = "name")
    @NotNull
    private String msisdn;

    @Column(name = "type")
    @NotNull
    private String iccid;

    @Column(name = "action")
    @NotNull
    private Long inventoryActionId;

    @NotNull
    @Column(name = "is_exported")
    private Boolean isExported;

    @Column(name = "created_date_time")
    @NotNull
    private LocalDateTime createdDateTime;

    //setter and getter

}

異常:應用程序無法啟動,出現以下異常。

Caused by: javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.HibernateException: Errors in named queries: 
query1 failed because of: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: ( near line 1, column 161

您可以嘗試如下查詢。 它應該返回預期的結果

SELECT foo.id, foo.name, foo.type, foo.action, foo.createdDateTime 
FROM FooEntity foo 
WHERE  
    foo.isExported = false AND
    EXISTS(
        SELECT recententry.name, recententry.type, MAX(recententry.createdDateTime) AS recenttime 
        FROM FooEntity recententry 
        WHERE recententry.name = foo.name AND recententry.type = foo.type 
        GROUP BY recententry.name, recententry.type 
        HAVING recententry.recenttime = foo.createdDateTime
    )

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM