Ali Booresh
Ali Booresh

Reputation: 53

Error in jpa hibernate association many to one

This my Product class that extends a BaseClass and I get error in

 @JoinColumn(name = "fk_supplier",referencedColumnName = "supplier_id")
 private Supplier supplier;

and error is 'Many To One' attribute type should not be 'Supplier'

@Table
@Data
public class Product extends BaseEntity {
    @ManyToMany
    private List<Customer> customers = new ArrayList<>();
    @ManyToOne
    @JoinColumn(name = "fk_supplier",referencedColumnName = "supplier_id")
    private Supplier supplier;

}

And this is my Supplier Class

@Table
@Data
public class Supplier extends BaseEntity {
    @Column
    private boolean active;
    @Column
    private Date foundationDate;
    //Enum type to String type in mysql
    @Column
    @Enumerated(EnumType.STRING)
    private Type type;


    @OneToMany(targetEntity = Product.class)
    private List<Product> products = new ArrayList<>();
}

Upvotes: 0

Views: 125

Answers (1)

Midhun Mohan
Midhun Mohan

Reputation: 689

supplier_id column is defined under which entity?, means in the provided snippet for Supplier entity no such field is present. I hope the base entity will only be having columns that are generic and applicable over every entity you will be using. So if you specify referencedColumnName be sure that it align with what you have in the definitions. By default it will the primary key of the referenced entity, here primary key of Supplier table.

Please try to do like this, it may solve the issue i think

@JoinColumn(name = "fk_supplier")
private Supplier supplier;

Upvotes: 1

Related Questions