gpt4 book ai didi

hibernate - @Embeddable 和@ManyToOne

转载 作者:行者123 更新时间:2023-12-05 02:17:18 27 4
gpt4 key购买 nike

想象一下下面的类

@Embeddable
class A {
@ManyToOne
public B classB;

...
public State someEnum;
}

@Entity
@Table(name = "TEST")
class B {
public long id;
//... some data


@Embedded
@AttributeOverrides({
@AttributeOverride(
name = "classB.id",
column = @Column(name = "EMBEDDED1_ID")
),
@AttributeOverride(
name = "someEnum",
column = @Column(name = "EMBEDDED1_SOMEENUM")
)
})
public A embedded1;

@Embedded
@AttributeOverrides({
@AttributeOverride(
name = "classB.id",
column = @Column(name = "EMBEDDED2_ID")
),
@AttributeOverride(
name = "someEnum",
column = @Column(name = "EMBEDDED2_SOMEENUM")
)
})
public A embedded2;
}

所以我希望数据库表看起来像

  • 测试
    • 身份证
    • ...
    • EMBEDDED1_ID
    • EMBEDDED1_SOMEE​​NUM
    • EMBEDDED2_ID
    • EMBEDDED2_SOMEE​​NUM

但是如果失败了:

Caused by: org.hibernate.MappingException: Repeated column in mapping for entity: ClassB column: classB_id(should be mapped with insert="false" update="false")

版本:

hibernate :5.2.12.FINALJPA: hibernate-jpa-2.1-api

最佳答案

您不能使用@AttributeOverride 重命名外键列。你必须使用@AssosiactionOverride

@Entity
@Table(name = "TEST")
public class B {

public long id;

@AssociationOverride(name = "classB", joinColumns = @JoinColumn(name = "EMBEDDED1_ID"))
@AttributeOverrides({
@AttributeOverride(
name = "someEnum",
column = @Column(name = "EMBEDDED1_SOMEENUM")
)
})
@Embedded
public A embedded1;

@AssociationOverride(name = "classB", joinColumns = @JoinColumn(name = "EMBEDDED2_ID"))
@AttributeOverrides({
@AttributeOverride(
name = "someEnum",
column = @Column(name = "EMBEDDED2_SOMEENUM")
)
})
@Embedded
public A embedded2;

关于hibernate - @Embeddable 和@ManyToOne,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48205546/

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