gpt4 book ai didi

java - 具有多个条件的 Hibernate Join 表,但 @WhereJoinTable 不起作用

转载 作者:行者123 更新时间:2023-11-29 02:49:00 25 4
gpt4 key购买 nike

有两个表A和B:

A(id, b_id, a_other)

B(id, b_other)

A和B对应的类如下:

@Entity
@Table(name = "A")
public class A {
@Id
@Column(name = "id")
private Integer id;

@JoinColumn(name = "b_id", nullable = true)
@OneToOne(fetch = FetchType.EAGER)
@WhereJoinTable(clause = "b_id > 0")
private B b;

@Column(name = "a_other")
private Integer aOther;
}

public class B {
@Id
@Column(name = "id")
private Integer id;

@Column(name = "b_other")
private Integer bOther;
}

我想要的查询是这样的:select * from A left join B on A.b_id = B.id and A.b_id != 0;

您可能会发现痛点是额外的连接条件 A.b_id != 0

我在 A 类中附加了 @WhereJoinTable(clause = "b_id > 0"),但它没有任何意义,因为我发现它根本不起作用。

我将 @WhereJoinTable 更改为 @Where,但我发现没有任何变化,任何人都可以帮助找出缺少的内容吗?

提前致谢。

最佳答案

如果你想要A.b_id != 0,你可以添加下面的注解

@Where(clause = 'b_id > 0')

所以你的代码应该是这样的:

@Entity
@Table(name = "A")
public class A {
@Id
@Column(name = "id")
private Integer id;
    @OneToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "b_id", nullable = true)
@Where(clause = 'b_id > 0')
    private Audience audience;

@Id
@Column(name = "a_other")
private Integer aOther;
}

public class B {
@Id
@Column(name = "id")
private Integer id;

@Id
@Column(name = "b_other")
private Integer bOther;
}

希望对你有帮助。

关于java - 具有多个条件的 Hibernate Join 表,但 @WhereJoinTable 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38112961/

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