在Spring Data中通过非主键进行一对一关联的任何方法

发布于 2025-01-19 11:53:50 字数 1031 浏览 0 评论 0原文

我有一种情况,两个实体要在非主键列上连接。我有以下情况:

@Embeddable
Entity EmbeddedKey{
    private String apple;
    private String banana;
    private int cat;
}
@Entity
public class ParentEntity{

    @EmbeddedId
    private EmbeddedKey embeddedKey;

    @Column(name = "dog")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private BigInteger dog;

    @OneToOne(mappedBy = "parentEntity")
    @JoinColumn(name = "dog", referencedColumnName = "dog")
    private ChildEntity childEntitty;
}
@Entity
public class ChildEntity{

    @OneToOne
    @PrimaryKeyJoinColumn(name = "dog", referencedColumnName = "dog")
    private ParentEntity parentEntity;

    @Id
    @Column(name = "dog")
    private BigInteger dog;
}

当我部署代码时,我得到

Caused by: org.hibernate.MappingException: broken column mapping for: ParentEntity.id of: x.y.z.ChildEntity. 

按照我的说法,hibernate 正在尝试将父表的主键与我的设计不需要的子表的主键连接起来。由于不可避免的原因,列狗是唯一的,但不是主键。

I have a situation where two entities are to be joined on non-primary key column. I have following situation:

@Embeddable
Entity EmbeddedKey{
    private String apple;
    private String banana;
    private int cat;
}
@Entity
public class ParentEntity{

    @EmbeddedId
    private EmbeddedKey embeddedKey;

    @Column(name = "dog")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private BigInteger dog;

    @OneToOne(mappedBy = "parentEntity")
    @JoinColumn(name = "dog", referencedColumnName = "dog")
    private ChildEntity childEntitty;
}
@Entity
public class ChildEntity{

    @OneToOne
    @PrimaryKeyJoinColumn(name = "dog", referencedColumnName = "dog")
    private ParentEntity parentEntity;

    @Id
    @Column(name = "dog")
    private BigInteger dog;
}

When I deploy code, i get

Caused by: org.hibernate.MappingException: broken column mapping for: ParentEntity.id of: x.y.z.ChildEntity. 

As per me, hibernate is trying to join primary key of parent table with primary-key of child table which my design doesn't want. column dog is unique but not primary key due to unavoidable reason.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文