Hibernate JPA一对一保存子类实体 [英] Hibernate JPA one-to-one saving child class entity

查看:635
本文介绍了Hibernate JPA一对一保存子类实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一对一的关系,使用 PrimaryKeyJoinColumn 在父级注释。例如,我有 Employee 和<$ c ,所以我想自己保存子实体。 $ c> EmpInfo 作为子实体,我需要保存 EmpInfo (当然在设置父项的id属性后)。但是,如果使用这种安排,我会得到下面列出的例外......

  org.springframework.dao.InvalidDataAccessApiUsageException:分离的实体传递给持久

为什么hibernate不允许这样做?为了更加清楚,我的代码在下面...



ParentEntity:

  public class Employee {
private Long id;
私人字符串名称;
私人EmployeeInfo信息;
私人整数enumId;

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


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

@PrimaryKeyJoinColumn
@OneToOne(级联= CascadeType.REMOVE)
公共EmployeeInfo的getInfo(){
返回信息;


ChildEntity

  @Table(name =EMP_INFO)
@Entity
public class EmployeeInfo {
private长ID;
私人字符串电子邮件;

@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;
}
}

我试图保存它的方式是...

  Employee emp = new Employee(); 
emp.setEnumId(SimpleEnum.COMPLETE);
emp.setName(Shreyas);
EmployeeInfo info = new EmployeeInfo();
info.setEmail(Sh @ gmail);
concreteDAO.save(emp); //使用Spring JpaDaoSupport提供的JPATemplate

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

任何指针都会非常感谢,我该如何尝试保存子实体? p>

解决方案

这里的问题是 @ code> @Id > EmployeeInfo 被声明为自动生成,因此您不应该手动设置它(Hibernate查看传递给 persist 的实体并且假设它已经在数据库中,因为填充了 @Id 字段)。



换句话说, @GeneratedValue EmployeeInfo 上如果您想手动设置PK。



请注意,Hibernate通过自定义扩展使用JPA 1.0中的共享主键提供对 OneToOne 关联的支持。见:



在JPA 2.0中,衍生标识符得到很好的支持,可以注释 OneToOne ManyToOne @Id 的关联。请参阅:


I have a one-to-one relationship using PrimaryKeyJoinColumn annotated on the parent side. 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). 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? To be more clear, the code I have is below...

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:

@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).

In other words, remove the @GeneratedValue on EmployeeInfo if you want to set the PK manually.

Note that Hibernate provides support for OneToOne association using a shared primary key in JPA 1.0 through a custom extension. See:

In JPA 2.0, derived identifiers are well supported and you can annotate OneToOne and ManyToOne associations with @Id. See:

这篇关于Hibernate JPA一对一保存子类实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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