具有INNER JOIN条件的JPA命名查询失败

问题描述 投票:0回答:1

我正在尝试使用Entity类中的内部联接条件创建一个命名查询。但是,应用程序无法以异常启动。

实体:

@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
java hibernate jpa spring-data-jpa named-query
1个回答
0
投票

Don't use inner join On,如果未使用@ManyToOne @OnToMany等关系映射来定义实体之间的关联,请使用休眠模式最好使用本机SQL查询。

[由马克·克拉克(Mark Clarke)引自post

如果您没有在对象之间定义关系,则需要告诉休眠加入什么]

© www.soinside.com 2019 - 2024. All rights reserved.