Hibernate级联:对象引用未保存的瞬态实例 - 在刷新之前保存瞬态实例 [英] Hibernate cascades : Object references an unsaved transient instance - save the transient instance before flushing

查看:352
本文介绍了Hibernate级联:对象引用未保存的瞬态实例 - 在刷新之前保存瞬态实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  @Entity 
@Table(name =parent)
public final class Parent extends Base {

@OneToOne(cascade = CascadeType.PERSIST,fetch = FetchType.EAGER)
private person person;

以及(除其他外):

 父亲=新父(); 
Person person = new Person();
parent.setPerson(person);
session.save(parent);

我得到上述异常?

我是否需要手动调用session.save(person)?我必须添加级联类型注释到childs类定义(它引用父级)?



或者我错过了其他明显的东西?



我不希望将CascadeType.ALL用作父母被删除时我想保留此人(孩子)。



两个实体/表扩展了一个通用基表:

  @MappedSuperclass()
public abstract class Base {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;

public Integer getId(){
return id;
}

这会影响到哪种级联类型?

解决方案

您还没有提到Hibernate版本,但自从我开始使用它之后,这一点并没有改变。



正如你可以阅读 Hibernate reference ,以获得Java标准 SAVE_UPDATE ,您需要 {CascadeType.PERSIST,CascadeType.MERGE} 在Hibernate中。



编辑:看到更新的信息,你现在正在做的事情导致Hibernate把它当作双向的一对一映射。这基本上意味着对于这两个表中的任何一个中的每个对象,必须是另一个表中具有相同ID的对象。因此,您不能只删除其中的一个,否则您将失去FK完整性。



如果您希望它是单向映射,例如,如果您希望能够删除该人但离开父母 - 必须通过 @JoinColumn 来指定FK,例如
@JoinColumn(name = PERSON_ID,unique = false,nullable = true,insertable = true,updatable = true)


@Entity
@Table(name = "parent")
public final class Parent extends Base {

@OneToOne(cascade = CascadeType.PERSIST, fetch = FetchType.EAGER)
private Person person;

and doing (amongst other things) this :

Parent parent = new Parent();
Person person = new Person();
parent.setPerson(person);
session.save(parent);

I get the mentioned exception ?

Do I manually need to call session.save(person) before ? do I have to add a cascade type annotation to the childs class definition(where it references the parent) ?

Or have I missed something else obvious ?

I don't want to use CascadeType.ALL as when a parent is deleted I want to keep the person(child).

Both entities/tables extend a common Base table :

@MappedSuperclass()
public abstract class Base {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Integer id;

    public Integer getId() {
    return id;
    }

Will this effect which cascade type is required ?

解决方案

You haven't mentioned the Hibernate version, but this hasn't changed since I ever started using it.

As you can read in the Hibernate reference, to get the Java standard SAVE_UPDATE you need {CascadeType.PERSIST, CascadeType.MERGE} in Hibernate.

EDIT: Seeing the updated info, what you're doing now causes Hibernate to treat it as a bi-directional one-to-one mapping. This basically means that for each object in any of those two tables, there has got to be a counterpart in the other table with the same ID. Therefore, you cannot delete only one of them, you would lose FK integrity.

If you want it to be a unidirectional mapping, e.g., if you want to be able to delete the person but leave the parent -- you have to specify a FK, usually via @JoinColumn, like @JoinColumn(name="PERSON_ID", unique=false, nullable=true, insertable=true, updatable=true)

这篇关于Hibernate级联:对象引用未保存的瞬态实例 - 在刷新之前保存瞬态实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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