JPA:如何使用OneToMany注释向列表添加新项目 [英] JPA: How do I add new Items to a List with a OneToMany annotation

查看:169
本文介绍了JPA:如何使用OneToMany注释向列表添加新项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2张桌子。一个叫做员工,另一个叫做电话,一个员工可以有多个电话。

I have 2 tables. One is called Employee, and the other is called Phones, and an employee can have multiple Phones.

员工类:

@Entity
@Table(name = "employee")
public class Employee {

    @Id
    @Column(name = "id", unique = true, nullable = false)
    @GeneratedValue(strategy = javax.persistence.GenerationType.IDENTITY)
    private Integer id;

    @Column(name = "name", unique = true, nullable = false, length = 25)
    private String name;

    @OneToMany(mappedBy="owner", fetch= FetchType.EAGER, orphanRemoval=true, cascade={CascadeType.ALL})
    private List<Phone> phones;

电话类:

@Entity
@Table(name = "phone")
public class Phone {

    @Id
    @Column(name = "id", unique = true, nullable = false)
    @GeneratedValue(strategy = javax.persistence.GenerationType.IDENTITY)
    private long id;

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "employee_id")
    private Employee owner;

    @Column(name = "phone_type", nullable = false, length = 25)
    private String phoneType;

    @Column(name = "phone_number", nullable = false, length = 25)
    private String phoneNumber;

假设我想将电话添加到现有员工。我这样做:

Suppose I want to add a phone to an existing employee. I am doing this:

Phone phone = new Phone();
phone.setOwner(employee);
phone.setPhoneNumber("999-555-0001");
phone.setPhoneType("home");
employee.getPhones().add(phone);        
dao.merge(employee); // Is it possible to get this to both persist new phones and update existing phones that were changed?

鉴于phoneSet中的部分手机,我不知道如何合并新手机已经坚持了下来。我必须手动保留每部手机吗?一个例子将不胜感激。我考虑了级联,但我似乎无法让它工作。我收到的错误是:java.lang.IllegalStateException:实体副本已经分配给另一个实体。

I'm not sure how to merge new phones in, given that some of the phones in the phoneSet have already been persisted. Must I persist each phone manually? An example would be greatly appreciated. I looked into cascading but I can't seem to get it to work. The error I'm receiving is: java.lang.IllegalStateException: An entity copy was already assigned to a different entity.

推荐答案

我认为问题可能出在Phone类的连接列注释中。您正在指定 employee_id 的连接列但是在Employee类中,id字段的 @Column 注释被映射到列 id

I think the issue may be in your join column annotation in the Phone class. You are specifying a join column of employee_id yet in the Employee class the @Column annotation for the id field is mapped to the column id.

尝试更改/同步连接列:

Try changing/synchronizing the join columns:

@ManyToOne(fetch = FetchType.EAGER, targetEntity=your.package.here.Employee.class)
@JoinColumn(name = "id")
private Employee owner;

员工。 java

@Id
@Column(name = "employee_id", unique = true, nullable = false)
@GeneratedValue(strategy = javax.persistence.GenerationType.IDENTITY)
private Integer id;

Phone.java

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "employee_id", targetEntity=your.package.here.Employee.class)
private Employee owner;

这篇关于JPA:如何使用OneToMany注释向列表添加新项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆