ziMtyth
ziMtyth

Reputation: 1048

@OneToMany does not create the join table

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.

Upvotes: 1

Views: 2165

Answers (1)

ziMtyth
ziMtyth

Reputation: 1048

First of all, I have write a misleading title, I will change it to make it more accurate:

Old title : In JPA, is it possible to use @OneToMany without using @ManyToOne?

New title : @OneToMany does not create the join table.

As I said, I'm new to JPA, my problem can appear dumb, I could delete the question, but I decided to keep it in case someone someday will face similar situation, it can help!

The join table of Purchase and Article was created every time I executed the code very normally, but I didn't notice!, I was checking the logs of NetBeans and didn't see the join table, I was misled by those logs, I think that a join table doesn't appear in the logs (I hope that someone can confirm this information and make an edit of this answer).

I have created Purchase and Article in a new schema named: sch_sales. and the join table was created in public schema (PostgreSQL).

So, to make it more correct I added schema to @JoinTable as shown below, like this I will have all my tables in the same schema.

@Entity
@Table(name="purchase", schema = "sch_sales")
public class Purchase implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String name;

    @OneToMany
    @JoinTable(name="join_purchase_article", schema = "sch_sales", joinColumns = @JoinColumn(name="sales_fk"), inverseJoinColumns = @JoinColumn(name="article_fk"))
    private List<Article> listArticle;

 }

UPDATE :

I was having a 3rd table created containing the id of Purchase and Article (a join table) which is obviously not correct.

The normal "behavior" is to have an id_purchase column added in Article, in this page I have find how to have such a result.

To have the desired result, I used the code below:

@Entity
@Table(name="purchase", schema = "sch_sales")
public class Purchase implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String name;

    @OneToMany
    @JoinColumn(name="id_purchase")
    private List<Article> listArticle;

 }

Upvotes: 2

Related Questions