Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 !

like image 809
Roman Sterlin Avatar asked Aug 28 '19 11:08

Roman Sterlin


2 Answers

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.

like image 189
Victor Calatramas Avatar answered Oct 21 '22 16:10

Victor Calatramas


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.

like image 29
Roman Sterlin Avatar answered Oct 21 '22 16:10

Roman Sterlin