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