deplicatemappingException包含物理列名称,该名称由多个逻辑列名在向学生实体添加Passportid上的多个逻辑列名称

发布于 2025-02-11 23:18:43 字数 1248 浏览 0 评论 0原文

该代码在启动上引起以下例外,

Caused by: org.hibernate.DuplicateMappingException: Table [student] contains physical column name [passport_id] referred to by multiple logical column names: [passport_id], [passportId]

我正在使用H2内存数据库。

学生实体:

@Entity
public class Student {

    @Id
    @GeneratedValue
    private Integer id;
    @Column(nullable = false, length = 250)
    private String name;
    private Integer passportId; // adding this will cause DuplicateMappingException

    @OneToOne
    private Passport passport;
    // getters and setters omitted for brevity
}

护照实体:

@Entity
public class Passport {

    @Id
    @GeneratedValue
    private Integer id;
    @Column(nullable = false, length = 250)
    private String number;
    // getters and setters omitted for brevity
}

问题1: org.hibernate.duplicatempappingexception的原因是什么?

问题2:为什么学生实体中的 Passportid 解决问题?

@Column(name = "passport_id", insertable = false, updatable = false)
private Integer passportId; // this resolves DuplicateMappingException

PS:我知道之前已经问过类似的问题,但我无法理解这些其他线程的这两个问题的答案。

This code is causing the following exception on the startup

Caused by: org.hibernate.DuplicateMappingException: Table [student] contains physical column name [passport_id] referred to by multiple logical column names: [passport_id], [passportId]

I'm using H2 in-memory database.

Student entity:

@Entity
public class Student {

    @Id
    @GeneratedValue
    private Integer id;
    @Column(nullable = false, length = 250)
    private String name;
    private Integer passportId; // adding this will cause DuplicateMappingException

    @OneToOne
    private Passport passport;
    // getters and setters omitted for brevity
}

Passport entity:

@Entity
public class Passport {

    @Id
    @GeneratedValue
    private Integer id;
    @Column(nullable = false, length = 250)
    private String number;
    // getters and setters omitted for brevity
}

Question 1: What is the reason for org.hibernate.DuplicateMappingException?

Question 2: Why does adding the following annotation to passportId in the Student entity resolve the issue?

@Column(name = "passport_id", insertable = false, updatable = false)
private Integer passportId; // this resolves DuplicateMappingException

PS: I know similar questions has been asked earlier but I couldn’t understand the answer for these two questions from those other threads.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

奢欲 2025-02-18 23:18:44

答案1

原因是您对同一数据库列有两个可写的映射,并且不允许

答案2

使其中一台映射仅读取解决问题,因为这样只有一个映射将是可写的

Answer 1

The reason is that you have two writable mappings for the same database column and this is not allowed

Answer 2

Making one of the mappings read-only solves the problem because then only one mapping will be writable

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文