简体   繁体   English

在Hibernate中使用连接时映射实体类

[英]Mapping Entity class when using joins in hibernate

Inside MySQL i created some tables. 在MySQL内部,我创建了一些表。 I have a table user who contains all the items. 我有一个包含所有项目的表用户。 But now i want to make an estimate who has a list of items. 但是现在我想估算一下谁拥有物品清单。 from the tbl_items. 来自tbl_items。

So i MySql i did something like this: 所以我MySql我做了这样的事情:

在此处输入图片说明

But now if i have trouble with the mapping of the list items: 但是现在,如果我在映射列表项时遇到麻烦:

This is my estimate class : 这是我的预估课:

@Entity
@Table(name = "tbl_estimate")
@SecondaryTable(name = "tbl_order_items", pkJoinColumns = {@PrimaryKeyJoinColumn(name = "fk_id_estimate_order", referencedColumnName = "id_estimate")})
public class Estimate {

    //Table estimate
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id_estimate",table = "tbl_estimate")
    private Integer id ;

    @Column(table = "tbl_estimate")
    private Integer fkIdUserEstimate;

    @Column(table = "tbl_estimate")
    private Integer fkIdClientEstimate;


    //table order items
    @Column(table = "tbl_order_items")
    @OneToMany(fetch = FetchType.LAZY, mappedBy = "fk_id_estimate_order", cascade = CascadeType.ALL)
    @Fetch(FetchMode.JOIN)
    private List<Item> itemList = new ArrayList<>();


....

Can someone give me some directions on this ? 有人可以给我一些指导吗?

First off, when using JPA you should FORGET about the design of your database when designing entities. 首先,在使用JPA时,在设计实体时应该忘记数据库的设计。 Mapping foreign key id's is neither necessary or something one should do. 映射外键ID既不是必需的,也不是应该做的事情。 JPA is all about object orientation and hiding the details of the database design. JPA只是关于面向对象和隐藏数据库设计的细节。

If you want a OneToMany from Estimate to Item , the simplest way of doing this is through a JoinColumn : 如果您希望从EstimateItemOneToMany ,则最简单的方法是通过JoinColumn

@Entity
@Table(name = "tbl_estimate")
public class Estimate {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id_estimate")
    private Integer id ;

    @JoinColumn(name = "estimate_id")
    @OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    private List<Item> items = new ArrayList<>();     

    ...
}

Then your item -table needs a foreign column to the estimate table named estimate_id . 然后,您的item table需要在estimate表中有一个外部列,名为estimate_id Alternatively, use a join table: 或者,使用联接表:

@Entity
@Table(name = "tbl_estimate")
public class Estimate {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id_estimate")
    private Integer id ;

    @JoinTable(name = "estimate_item")
    @OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    private List<Item> items = new ArrayList<>();     

    ...
}

This will require a table named estimate_item , with columns estimate_id and items_id , each with a foreign key to one of the tables. 这将需要一个名为estimate_item的表,该表具有estimate_iditems_id列,每个表都具有到其中一个表的外键。 It is possible to override the names of the columns with nested a @JoinColumn with inverseJoinColumn in the @JoinTable annotation. 它可以覆盖列的名称与嵌套@JoinColumninverseJoinColumn@JoinTable注解。

When working with JPA, it's best to start with the design of the entities, and create the tables based on this. 使用JPA时,最好从实体的设计开始,然后基于此创建表。 This approach will usually give the cleanest design. 这种方法通常会提供最简洁的设计。

Also, you use the mappedBy -attribute wrong, it's value is not the foreign key in the database, but rather the variable name on the other side of the entity relationship. 另外,您使用了mappedBy -attribute错误,它的值不是数据库中的外键,而是实体关系另一侧的变量名。

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

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