I'm new to JPA. Suppose I have these two entities:
//Imports
@Entity
@Table(name="article", schema = "sch_client")
public class Article implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private int price;
private int amount;
//Getters & setters
}
And
@Entity
@Table(name="purchase", schema = "sch_client")
public class Purchase implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
@OneToMany
private List<Article> listArticle;}
I want to have something like a purchase contains many articles.
My question is: is it possible with only @OneToMany in Purchase class that points to Article class to have the desired relationship (a purchase contains many articles). Or to use a @OneToMany annotation I have to add a @ManyToOne on Article class. If so, why is is mandatory to add the @ManyToOne? any explanation please.
Thanks in advance.