简体   繁体   English

OneToMany双向映射未保存其外键

[英]OneToMany bidirectional mapping is not saving its foreign key

I have one-to-many relationship between Employee & Phone entities like this: 我在“员工与电话”实体之间存在一对多关系,如下所示:

@Entity
public class Employee {
    @Id
    @Column(name = "EMP_ID")
    private long id;

    private String name;

    @OneToMany(mappedBy = "owner")
    private List<Phone> phones = new ArrayList<Phone>();
}

@Entity
public class Phone {
    @Id
    private long id;
    private String phoneNumber;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "OWNER_ID")
    private Employee owner;
}

In my code if I save phone first then employee next: 在我的代码中,如果我先保存电话,然后再保存员工,则:

session.save(phone);
session.save(employee);

Then I am seeing and insert query to save Employee, then insert query to save Phone and then update query to update owner_id in Phone table. 然后,我看到并插入查询以保存Employee,然后插入查询以保存Phone,然后更新查询以更新Phone表中的owner_id。

So to avoid insert and update on Phone table, I have added below mapping in my Phone entity: 因此,为避免在Phone表上进行插入和更新,我在Phone实体中添加了以下映射:

@Entity
public class Phone {
    ...
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "OWNER_ID", insertable=false, updatable=false)
    private Employee owner;
}

Now in this case, I see insert on Employee then insert on Phone, but the owner_id is null. 现在在这种情况下,我看到在Employee上插入,然后在Phone上插入,但是owner_id为null。

How to make sure that the owner_id is not null when I save the entities? 保存实体时,如何确保owner_id不为null? I want to save Phone then Employee in my code not the other way. 我想用我的代码保存Phone然后Employee,而不是其他方式。

As phone is a child entity, it will need reference of employee for saving. 由于电话是儿童实体,因此需要职员的参考才能保存。 By annotating Phone with cascadeType, your phone will be saved with your employee entity. 通过用层叠类型注释Phone,您的电话将与您的员工实体一起保存。 Your modified entities will be like this : 您修改后的实体将如下所示:

@Entity
public class Employee {
    @Id
    @Column(name = "EMP_ID")
    private long id;

    private String name;

    @OneToMany(mappedBy = "owner",cascade = CascadeType.ALL)
    private List<Phone> phones = new ArrayList<Phone>();

  /*getters and setters*/
}

@Entity
public class Phone {
    @Id
    private long id;
    private String phoneNumber;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "OWNER_ID", insertable=false, updatable=false,nullable=false)
    private Employee owner;
    /*getters and setters*/

}

And have to modify your saving logic as below : 并且必须修改您的保存逻辑,如下所示:

Employee emp = new Employee();
private List<Phone> phones = new ArrayList<Phone>();
Phone phone = new Phone();
phone.setOwner(emp);

phones.add(phone);

emp.setPhones(phones);

session.save(emp);

According to your requirement, no need to use bidirectional. 根据您的要求,无需使用双向。 Try the following steps 请尝试以下步骤

  1. Only need to declare @ManyToOne in Phone entity. 只需要在Phone实体中声明@ManyToOne So remove @OneToMany declaration from Employee entity. 因此,从Employee实体中删除@OneToMany声明。
  2. Hibernate session.save(obj) returns the id of Object. Hibernate session.save(obj)返回Object的ID。 So just save Phone first and catch id then save employee.See sample 因此,只需先保存电话并捕获id然后保存员工即可。请参阅示例

      session.save(phone); long phoneOneId = phone.getId(); Phone phoneOne = new Phone(); phoneOne.setId(phoneOneId); // You can also add another phone like phoneTwo, phoneThree, ... List<Phone> phones = new ArrayList<Phone>(); phones.add(phoneOne); employee.setPhones(phones); 

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

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