A column in a table is referred to by multiple physical column names
Asked Answered
T

7

9

I have a spring boot project using JPA, So I am trying to map two tables into a third one using their Id : for example I have a coupon class, I have a customer class I want to take customer id and coupon id into a third table.

I have coupons:

@Entity
@Table(name = "coupons")
public class Coupon {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)

    private long coup_id;
    private String title;
    private String start;
    private String end; 
    private int amount; 
    private String type;
    private String message; 
    private double price;
    private String image;

    @ManyToMany(mappedBy = "coupons")
    private List<Customer> customers;

enter image description here

I have customers:

@Entity
@Table(name="customers")
public class Customer {

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)

    private int cust_id;

    @Size(min=1,message="is required")
    private String cust_name;

    @Size(min = 1, message = "is required")
    private String password;
     @ManyToMany(cascade = { CascadeType.ALL })
        @JoinTable(
            name = "customer_coupon", 
            joinColumns = { @JoinColumn(name = "cust_id") }, 
            inverseJoinColumns = { @JoinColumn(name = "coup_id") }
        )
    private List<Coupon> coupons;

enter image description here

and I have the connecting table customer_coupon:

enter image description here

This is the error I am getting when starting the project:

Caused by: org.hibernate.DuplicateMappingException: Table [coupons] contains physical column name [coup_id] referred to by multiple physical column names: [coupId], [coup_id]

I have no idea where it comes from, would love if someone could help me !

Tabaret answered 28/8, 2019 at 11:29 Comment(1)
try rename private long coup_id; to private long coupId;Mezoff
T
5

Found the problem...sorry.

Had another class Company that was referring to coupId as well:

     @OneToMany(
            cascade = CascadeType.ALL,
            orphanRemoval = true
        )
 @JoinColumn(name = "coupId")
private List<Coupon> coupons = new ArrayList();

This is from the Company class.

Tabaret answered 28/8, 2019 at 12:25 Comment(4)
Why is this a problem?Nicotinism
Please explain how this can be considered as an answer and how it solves the problemRedwood
it was a long time ago but I believe that I had two Company classes reffering to the same coupId.Tabaret
In my case (rewording to match these variables), I had a variable coupId in two classes. I had the annotation @Column("coup_id") on the variable in class A and no annotation in class B so it was interpreted as coupId. Therefore treated as its own LOGICAL identifier so the application would not start. To fix I added the annotation to the other class the logical identifier was reused for both classesLabyrinth
D
19

To remove ambiguity use the @Column annotation:

@Column(name = "coup_id")
private long coupId;

This way you can name your Java attributes as you like and don't let JPA alone for interpreting them.

Diverticulitis answered 28/8, 2019 at 12:6 Comment(4)
where did you get this private long coupId from? original is private long coup_idTabaret
tried it, still the same, I had @Column on everything before this post, had error with it & error without itTabaret
@RomanSterlin Maybe you should read deeper the OP's problem before downvoting an answer... From the exception: referred to by multiple physical column names: [coupId], ... . I don't think he has named a database column in camelCase, so in some part of the code, he defined the Coupon Id variable as coupId.Diverticulitis
sorry your'e right, edit ur answer alil so I can remove downvote.Tabaret
T
5

Found the problem...sorry.

Had another class Company that was referring to coupId as well:

     @OneToMany(
            cascade = CascadeType.ALL,
            orphanRemoval = true
        )
 @JoinColumn(name = "coupId")
private List<Coupon> coupons = new ArrayList();

This is from the Company class.

Tabaret answered 28/8, 2019 at 12:25 Comment(4)
Why is this a problem?Nicotinism
Please explain how this can be considered as an answer and how it solves the problemRedwood
it was a long time ago but I believe that I had two Company classes reffering to the same coupId.Tabaret
In my case (rewording to match these variables), I had a variable coupId in two classes. I had the annotation @Column("coup_id") on the variable in class A and no annotation in class B so it was interpreted as coupId. Therefore treated as its own LOGICAL identifier so the application would not start. To fix I added the annotation to the other class the logical identifier was reused for both classesLabyrinth
B
2

I had same problem.

@Column(name = "coup_id") private long coupId;

and column in db table called coup_id.

I removed @Column annotation, and workin! Thats all.

Hibernate convert xX to x_x itself.

Bonucci answered 30/6, 2020 at 12:46 Comment(0)
C
1

Another situation where I had this problem is due to case sensitivity. I mentioned a column name as ownerid and ownerId in two classes. As per Error: Table [] contains logical column name [ownerid] referring to multiple physical column names [ownerid] and [owner_id]. I ended up spending a lot of time searching for owner_id.

Carner answered 20/4, 2020 at 12:42 Comment(0)
V
0

I have join table, that is also entity and have ManyToOne relationships to two tables, so it is not exactly this issue, but could help someone.

Adding @MapsId(property name) to @ManyToOne property worked for me.

Vasya answered 6/12, 2020 at 11:53 Comment(0)
T
0

it might be because of miss the mapped by i was have the issue in this code

@OneToMany
@JsonManagedReference
private List<TweetsEntity> userTweets;

but after make it this way (add the mappedBy)

@OneToMany(mappedBy = "user")
@JsonManagedReference
private List<TweetsEntity> userTweets;

its solved for me

Thrower answered 4/12, 2022 at 0:47 Comment(0)
C
0

I had this problem in a join table for a many to many relationship with a composite key, with Victor Calatramas solution, here how it should look like:

JoinTable.java

@Entity
@IdClass(JoinTablePK.class)
@Getter
@Setter
@AllArgsConstructor
@FieldDefaults(level = AccessLevel.PRIVATE)
public class JoinTable {
    @Id
    @Column(name = "a_id") // This was missing
    Integer aId;

    @Id
    @Column(name = "b_id") // This was missing
    String bId;

    @MapsId("aId")
    @ManyToOne
    @JoinColumn(name = "a_id") // This was aId which is wrong
    A a;

    @MapsId("bId")
    @ManyToOne
    @JoinColumn(name = "b_id") // This was bId which is wrong
    B b;

}

JoinTablePK.java

@NoArgsConstructor
@AllArgsConstructor
@FieldDefaults(level = AccessLevel.PRIVATE)
@EqualsAndHashCode
public class JoinTablePK implements Serializable {

    Integer aId;
    String bId;
}
Compassion answered 20/6 at 23:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.