命名查询中的错误

时间:2018-08-07 09:48:04

标签: hibernate foreign-keys hql named-query

我收到此错误消息:

HibernateException: Errors in named queries: History.findByWeekId

我的实体类如下:

    @Entity
        @Table(name = "xyz_History")
        @NamedQueries({
            @NamedQuery(name = "History.findByWeekId",
                        query = "SELECT h FROM History h WHERE h.weekId = :weekId")
        })

        public class History extends BaseEntity {

        @ManyToOne( fetch = FetchType.EAGER, cascade = CascadeType.ALL )
        @JoinColumn(name = "weekId", nullable = false )
        @ForeignKey(name = "FK_History_WeeklyOverview")
        private WeeklyOverview weeklyOverview;
...

问题似乎出在weekId上。如果我尝试使用它代替命名查询:

        ...createQuery( "SELECT ah FROM History ah WHERE ah.weekId = :weekId " )
        .setParameter("weekId", weekId)
        .getResultList();

我收到错误消息: 无法解析属性:com.john.doe.History的weekId

作为参数传递的weekId是Long。 DB字段是一个int。有问题吗?

或者如何正确查询@JoinColumn?

WeeklyOverview实体:

@Entity
@Table(name = "xyz_WeeklyOverview")
public class WeeklyOverview {
@Id
@GeneratedValue
private Long id;
...
}

1 个答案:

答案 0 :(得分:1)

发生此错误是因为您在“历史记录”实体中没有名为weekId的字段。 您需要的字段是weeklyOverview,查询内容如下:

@NamedQuery(name = "History.findByWeekId",
           query = "select h from History h where h.weeklyOverview.id = :weekId")