gpt4 book ai didi

java - 无法加入@JoinColumn(即@Id)

转载 作者:行者123 更新时间:2023-12-01 05:25:14 25 4
gpt4 key购买 nike

我有一个 @Entity 类,它有多个 ID 列,并且所有列都是外键。类代码如下所示:

@Entity
@Immutable
@Table(name = "AnyNews")
public class AnyNews implements Serializable {
private static final long serialVersionUID = 43876852L;

@Id
@Column(name = "id")
private int id;

@Id
@OneToOne
@JoinColumn(name = "news_id")
private News news;

.....
}

我想通过 News 实体的 date 列在 Criteria 上添加 Restriction。所以我制定了这样的标准:

Criteria criteria = sessionFactory.getCurrentSession()
.createCriteria(AnyNews.class, "any")
.createCriteria("news", "news");
.add(Restrictions.eq("news.id", id))
.add(Restrictions.ge("news.date", from.getTime()));

代码看起来不错,但会出现如下错误:

org.hibernate.exception.SQLGrammarException: could not execute query
...

该问题是由于缺少 Join 语句引起的。查询中没有 Join 语句:

select
this_.id as news4_5_0_,
this_.news_id as keyword1_5_0_,
this_.board_no as board2_5_0_,
this_.seq as seq5_0_
from
AnyNews this_
where
this_.news_id=?
and news1_.date>=?

为什么Hibernate会进行这样的查询。我不能在同一列上使用 @Id 和 @JoinColumn 注释吗?

最佳答案

您需要一个 @OneToOne(fetch=FetchType.EAGER) 注释来执行该连接。

或者,也许可以执行 .setFetchMode("news", FetchMode.JOIN)

关于java - 无法加入@JoinColumn(即@Id),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9780459/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com