Home > Net >  DuplicateMappingException contains physical column name referred to by multiple logical column names
DuplicateMappingException contains physical column name referred to by multiple logical column names

Time:07-01

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.

CodePudding user response:

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

  • Related