How to solve error message saying: must have same number of columns as the referenced primary key

145 views Asked by At

I have some model classes and I believe that two of them have a wrong annotation. However, I do not know how to do it right although I have already been looking for it.

Here is are model class - I want to achieve that one shop can have many products

Shop:

@Entity
@Table
    public class Shop extends AbstractBaseDomain<Long> {

    @Id
    @GeneratedValue
    private long shop_id;

    @NotBlank
    @Size(min = 2, max = 50)
    @Column(name ="name")
    private String name;

    @NotNull
    @ManyToOne(cascade = CascadeType.ALL)
    @JoinColumn(name ="address")
    private Address address;

    @OneToMany(mappedBy = "product_id")
    private List<Product> products;
}

Product:

@Entity
@Table
public class Product extends AbstractBaseDomain<Long> {

    @Id
    @GeneratedValue
    private long product_id;

    @NotBlank
    @Size(min = 2, max = 50)
    @Column(name ="productname")
    private String productname;

    @NotBlank
    @Size(min = 2, max = 50)
    @Column(name ="manufacturer")
    private String manufacturer;

    @NotNull
    @Column(name ="currentPrice")
    private BigDecimal currentPrice;

    @ManyToOne
    private Order order;
}

HistoryPrice

@Entity
@Table
public class HistoryPrice extends AbstractBaseDomain<Long> {

    @NotNull
    @JoinColumn(name ="product_id")
    private Product product;

    @NotNull
    @Column(name ="price")
    private BigDecimal price;

    @NotNull
    @Column(name ="[from]")
    private LocalDateTime from;

When I try to

Shop shop = shopService.findShopById(id);

I always get the error message, saying SQLGrammarException.

enter image description here Now, I implemented a shop_id, and product_id in my model classes and now I get another error message, saying:

product [product_id])) must have same number of columns as the referenced primary key (shop [shop_id,id])

Maybe someone can help me solving this problem.

Thanks in advance

1

There are 1 answers

1
Madhusudana On

Please define product id and shop id attributes, which are primary keys for entities.

Change shop entity configuration as below.

@OneToMany(mappedBy = "productId")
private List<Product> products;