0

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

Bernd
  • 593
  • 2
  • 8
  • 31
  • Do you have more error message, for example the call stack trace? – Yu Jiaao Mar 29 '19 at 00:57
  • This is my stacktrace: Caused by: org.hibernate.AnnotationException: **@OneToOne** or **@ManyToOne** on Shop.products references an unknown entity: java.util.List – Bernd Mar 29 '19 at 01:03
  • This is a stack trace element only. Yu Jiaao asked you to edit your question and add the **full stack trace**. This beside exact version information helps a lot debugging issues. – Selaron Mar 29 '19 at 10:29

1 Answers1

0

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;
Madhusudana
  • 302
  • 3
  • 12
  • Sorry for my late answer: I did as you suggested. I added private long product_id; and private long shop_id; but now I get the error message: Caused by: org.hibernate.AnnotationException: A Foreign key refering Product from HistoryPrice has the wrong number of column. I will update my question. – Bernd Mar 30 '19 at 09:18