0

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

}
Qin
  • 1
  • 3
  • And this is not working? – Rohit Jun 05 '20 at 12:54
  • Transient error will be reported. org.hibernate.TransientPropertyValueException: object references an unsaved transient instance - save the transient instance before flushing : test.spring.business.Parent.child – Qin Jun 05 '20 at 12:57
  • 1
    This exception should be in the question. – Rohit Jun 05 '20 at 12:59
  • If you do not want to persist `child` at time of persisting `Parent` why do you add the `p.child` initialization at all? See also [this](https://stackoverflow.com/questions/2302802/how-to-fix-the-hibernate-object-references-an-unsaved-transient-instance-save). – SternK Jun 05 '20 at 13:03
  • I need to save parent without saving the child so CascadeType.ALL will compromise my objective. – Qin Jun 05 '20 at 13:07
  • Parent is used for spring form. Child value is an optional value for that form. Child will be initialize after parent and accepting input from spring form. Thus I cant keep it null. – Qin Jun 05 '20 at 13:09

1 Answers1

0

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);

}
Rohit
  • 2,132
  • 1
  • 15
  • 24
  • p.child=new Child is needed since spring form will assign value to it. The value will judge whether child need to save to db. – Qin Jun 05 '20 at 13:12
  • The child is designed to be read only column in parent. Child in parent is more toward a placeholder when new Child() is assigned. Therefore I want to save parent without creating or updating child. However, it read data from database too so child in parent cant be set as Transient. – Qin Jun 05 '20 at 13:32
  • Then don't set it and keep it null and persist `Parent`.When in future you have `Child` info read the `Parent` from DB and set the child on parent and persist the `Parent`. – Rohit Jun 05 '20 at 13:46
  • I created a parent object with child and wait for user input for child. If child doesnt exist, spring form cant display and wait for user input. So, child is necessary to retrieve data when parent is created. I am wondering if I can save parent without child in this bidirectional one to one relationship. – Qin Jun 05 '20 at 13:57
  • Child is holding a value before parent persist. The value will decide whether to save child into db. – Qin Jun 05 '20 at 14:00