How to prevent saving child object when persist parent with JPA?TransientPropertyValueException reported

huangapple 未分类评论49阅读模式
英文:

How to prevent saving child object when persist parent with JPA?TransientPropertyValueException reported

问题

我有一个父实体-Parent和一个子实体-Child,它们之间是一对一的关系。
我正在使用双向映射来处理实体之间的关系。

如何在不保存子实体的情况下保存父实体,因为子实体被设计为只读列?
在持久化父对象时会报告临时错误。org.hibernate.TransientPropertyValueException: object references an unsaved transient instance - save the transient instance before flushing : test.spring.business.Parent.child

我不能使用transient,因为我需要从数据库获取子实体。

@Entity
@Table(name = "parent")
@SequenceGenerator(name = "SEQUENCE_FACTORY", sequenceName = "SEQ_ID", schema = "REQ", allocationSize = 1)
public class Parent implements Serializable {

    @Id
    @Column(name="id")
    @GeneratedValue(strategy = GenerationType.AUTO, generator = "SEQUENCE_FACTORY")
    private Long id;

    @OneToOne(mappedBy="parent")
    public Child child;

    // ...
}

@Entity
@Table(name = "child")
public class Child implements Serializable {

    @Id
    @OneToOne(optional = false,fetch = FetchType.LAZY)
    @JoinColumn(name="parent_id",insertable=false,updatable=false)
    private Parent parent;

    // ...
    private Long checkData;
}

@Transactional
public void testParent() {
    Parent p = new Parent();
    p.child = new Child();
    // ...
    // 获取p.child的输入...
    // ...
    entityManager.persist(p);

    if (p.child.checkData > n) {
        p.child.setParent(p);
        entityManager.persist(p.child);
    }
}
英文:

I have a parent entity-Parent and a child entitiy-Child with one to one relationship.
I am using bidirectional mapping for the entity.

How to save parent without saving the child since child is designed to be a read only column?
Transient error will be reported when persist parent object. org.hibernate.TransientPropertyValueException: object references an unsaved transient instance - save the transient instance before flushing : test.spring.business.Parent.child

I can't use transient because I need child from database.

@Entity
@Table(name = "parent")
@SequenceGenerator(name = "SEQUENCE_FACTORY", sequenceName = "SEQ_ID", schema = "REQ", allocationSize = 1)
public class Parent implements Serializable {

    @Id
    @Column(name="id")
    @GeneratedValue(strategy = GenerationType.AUTO, generator = "SEQUENCE_FACTORY")
    private Long id;

    @OneToOne(mappedBy="parent")
    public Child child;

    // ...
}


@Entity
@Table(name = "child")
public class Child implements Serializable{

    @Id
    @OneToOne(optional = false,fetch = FetchType.LAZY)
    @JoinColumn(name="parent_id",insertable=false,updatable=false)
    private Parent parent;
    // ...
    private Long checkData;

}


@Transactional
public void testParent()
{
	Parent p=new Parent();
	p.child= new Child();
    // ...
	//p.child get input...
    //...
    entityManager.persist(p);

    if(p.child.checkData>n)
	{
		p.child.setParent(p);
		entityManager.persist(p.child);
    }
	
}

</details>


# 答案1
**得分**: 0

由于`Child`实例是[`new/transient`][1],所以您遇到了这个错误。它表明 Hibernate 不知道在数据库中将父项关联到哪个子项。

由于子项是只读的,请将测试更改为

```java
@Transactional
public void testParent()
{
    Parent p = new Parent();
    p.child = entityManager.find(Child.class, 100L);
    entityManager.persist(p);
}
英文:

Since the Child instance is new/transient you encounter this error. It indicates that hibernate does not know which child the parent is associated to in DB.

Since the child is read only, change the test to

@Transactional
public void testParent()
{
    Parent p=new Parent();
    p.child= entityManager.find(Child.class, 100L);
    entityManager.persist(p);

}

huangapple
  • 本文由 发表于 2020年6月5日 20:37:26
  • 转载请务必保留本文链接:https://java.coder-hub.com/62215532.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定