简体   繁体   English

Hibernate JPA一对一保存子类实体

[英]Hibernate JPA one-to-one saving child class entity

I have a one-to-one relationship using PrimaryKeyJoinColumn annotated on the parent side. 我使用在父端注释的PrimaryKeyJoinColumn一对一的关系。 And now I want to save the child entity by itself. 现在我想自己保存子实体。

For example, I have Employee and EmpInfo as the child entity, I need to save EmpInfo (of course after setting the id property of the parent to it). 例如,我有EmployeeEmpInfo作为子实体,我需要保存EmpInfo (当然在设置父项的id属性之后)。 However, when such an arrangement is used, I get an exception listed below... 但是,当使用这样的安排时,我会得到下面列出的例外情况......

org.springframework.dao.InvalidDataAccessApiUsageException: detached entity passed to persist

Any ideas why hibernate does not allow this? 有什么想法为什么hibernate不允许这个? To be more clear, the code I have is below... 为了更清楚,我的代码如下......

ParentEntity: ParentEntity:

public class Employee {
    private Long id;
    private String name;
    private EmployeeInfo info;
    private Integer enumId;

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    public Long getId() {
        return id;
    }

    @Column(name="EMP_NAME")
    public String getName() {
        return name;
    }

    @PrimaryKeyJoinColumn
    @OneToOne(cascade = CascadeType.REMOVE)
    public EmployeeInfo getInfo() {
        return info;
    }
  }

ChildEntity: ChildEntity:

@Table(name="EMP_INFO")
@Entity
public class EmployeeInfo {
    private Long id;
    private String email;

    @Column(name="EMPLOYEE_EMAIL")
    public String getEmail() {
        return email;
    }

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name = "emp_id", nullable = false)
    public Long getId() {
        return id;
    }
}

The way I try to save it is... 我试图保存它的方式是......

Employee emp = new Employee();
emp.setEnumId(SimpleEnum.COMPLETE);
emp.setName("Shreyas");
EmployeeInfo info = new EmployeeInfo();
info.setEmail("Sh@gmail");
concreteDAO.save(emp);   // This uses the JPATemplate provided by Spring JpaDaoSupport


info.setId(emp.getId());
concreteDAO.saveEmpInfo(info);

Any pointers would be much appreciated, as to how can I try to save the child entity? 任何指针都会非常感激,我怎样才能尝试保存子实体?

The problem here is that the @Id of EmployeeInfo is declared as being auto-generated and you're thus not supposed to set it manually (Hibernate looks at the Entity passed to persist and assumes it is already in the database because the @Id field is populated). 这里的问题是EmployeeInfo@Id被声明为自动生成,因此你不应该手动设置它(Hibernate查看传递给persist的实体并假设它已经在数据库中,因为@Id字段已填充)。

In other words, remove the @GeneratedValue on EmployeeInfo if you want to set the PK manually. 换句话说,如果要手动设置PK,请删除EmployeeInfo上的@GeneratedValue

Note that Hibernate provides support for OneToOne association using a shared primary key in JPA 1.0 through a custom extension. 请注意,Hibernate通过自定义扩展在JPA 1.0中使用共享主键提供对OneToOne关联的支持。 See: 看到:

In JPA 2.0, derived identifiers are well supported and you can annotate OneToOne and ManyToOne associations with @Id . 在JPA 2.0中,派生标识符得到很好的支持,您可以使用@Id注释OneToOneManyToOne关联。 See: 看到:

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

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