简体   繁体   English

@OneToMany不创建联接表

[英]@OneToMany does not create the join table

I'm new to JPA. 我是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). 我的问题是:是否只有购买类中的@OneToMany指向Article类才能具有所需的关系(一次购买包含许多文章)。 Or to use a @OneToMany annotation I have to add a @ManyToOne on Article class. 或使用@OneToMany批注,我必须在Article类上添加@ManyToOne If so, why is is mandatory to add the @ManyToOne ? 如果是这样,为什么必须添加@ManyToOne呢? any explanation please. 请任何解释。 Thanks in advance. 提前致谢。

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? 旧标题:在JPA中,可以不使用@ManyToOne而使用@OneToMany吗?

New title : @OneToMany does not create the join table. 新标题: @OneToMany不创建联接表。

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! 就像我说的那样,我是JPA的新手,我的问题可能看起来很愚蠢,我可以删除问题,但是我决定保留此问题,以防某人某天遇到类似情况,它可以为您提供帮助!

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). 每次我很正常地执行代码时,都会创建Purchase和Article的联接表,但是我没有注意到!,我正在检查NetBeans的日志,却没有看到联接表,我被那些日志误导了,我认为联接表未出现在日志中(我希望有人可以确认此信息并对此答案进行编辑)。

I have created Purchase and Article in a new schema named: sch_sales. 我已经在名为sch_sales的新模式中创建了Purchase和Article。 and the join table was created in public schema (PostgreSQL). 并且联接表是在公共模式(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. 因此,为使其更正确,我将架构添加到@JoinTable,如下所示,这样,我将所有表都放在同一架构中。

@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. 我在创建第三个表,其中包含购买和商品的ID(联接表),这显然是不正确的。

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. 正常的“行为”是在Article中添加id_purchase列,在此页面中,我找到了如何获得这样的结果。

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;

 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM