[英]OneToMany relationship in hibernate creates 4 tables
i got a problem. 我有一个问题。 I have two classes with the same @OneToMany relation.
我有两个类具有相同的@OneToMany关系。 Hibernate creates 4 tables : product, product_categorie, categorie, categorie_product.
Hibernate创建4个表:product,product_categorie,categorie,categorie_product。
In my case i need only 3 tables: product, categorie and product_categorie. 就我而言,我只需要3个表:product,categorie和product_categorie。
This is my class diagram: 这是我的类图:
The code i wrote in Java: 我用Java编写的代码:
@Entity
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int product_id;
private String naam, omschrijving;
private double prijs;
@OneToMany(mappedBy = "product_m")
private List<Aanbieding> aanbiedingen;
@OneToMany
private List<Categorie> categories;
}
@Entity
public class Categorie {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int categorie_id;
private String naam, omschrijving;
@OneToMany
private List<Product> producten;
}
In my case i need to archieve the following : 就我而言,我需要归档以下内容:
One Product belongs to 1 or more categories 一种产品属于1个或更多类别
One Categorie contains 0 or more products 一个类别包含0个或更多产品
Did i do somthing wrong in code? 我在代码中做错了吗?
It's my first time using hibernate, hope you understand. 这是我第一次使用休眠模式,希望您能理解。
Adam, 亚当,
What you need is a ManyToMany relationship, not OneToMany. 您需要的是ManyToMany关系,而不是OneToMany。 Together with a JoinTable to map the relationship between product and categories.
与JoinTable一起映射产品和类别之间的关系。
@Entity
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int product_id;
private String naam;
private String omschrijving;
private double prijs;
@OneToMany(mappedBy = "product_m")
private List<Aanbieding> aanbiedingen;
@ManyToMany(cascade = { CascadeType.ALL })
@JoinTable(
name = "product_categories",
joinColumns = { @JoinColumn(name = "product_id") },
inverseJoinColumns = { @JoinColumn(name = "categorie_id") }
)
private List<Categorie> categories;
}
@Entity
public class Categorie {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int categorie_id;
private String naam;
private String omschrijving;
@ManyToMany(mappedBy = "categories")
private List<Product> producten;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.