命名查询中的错误

Errors in named queries

我收到此错误消息:

HibernateException: Errors in named queries: History.findByWeekId

我的实体 class 如下所示:

    @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();

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

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

或者如何正确查询@JoinColumn?

WeeklyOverview 实体:

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

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

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