EclipseLink OneToOne CascadeType.All is not working [message #1716599] |
Sun, 06 December 2015 06:22 |
Vimal Lakshmanan Messages: 1 Registered: December 2015 |
Junior Member |
|
|
I have two domains with bi directional relationship.
While persisting Parent object with cascadeType.ALL, respective child object is not persisting. Parent PK value is not setting automatically to child.
public class EventDomain {
/**
* The unique identifier for an event, which is a significant occurrence or happening.
*
*/
@Column(name = "ID_EVNT", nullable = false)
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private BigInteger eventIdentifier;
@OneToOne(fetch = FetchType.LAZY, mappedBy = "eventDomain", cascade = CascadeType.ALL,
orphanRemoval = true)
@JoinColumns({@JoinColumn(name = "id_evnt", referencedColumnName = "id_evnt", insertable = false,
updatable = false, nullable = false)})
private EventProcessStatusDomain eventProcessStatusDomain;
}
public class EventProcessStatusDomain implements Serializable {
@Id
@Column(name = "ID_EVNT", nullable = false)
private BigInteger eventIdentifier;
@OneToOne(fetch = FetchType.LAZY)
@JoinColumns({@JoinColumn(name = "id_evnt", referencedColumnName = "id_evnt", insertable = false,
updatable = false, nullable = false)})
private EventDomain eventDomain;
}
Persist Method:
`````````````````````````````
eventProcessStatusDomain.setEventDomain(eventDomain);
eventDomain.setEventProcessStatusDomain(eventProcessStatusDomain);
eventDomain = eventDAO.persistEvent(eventDomain);
persistEvent --> SimpleJPARepository --> saveAndFlush()
While saving Parent using generatedType.IDENTITY, the child object is not populated automatically with generated Parent PK value. Due to this am getting exception when it inserts child object with null FK.
Kindly help me to resolve this issue.
|
|
|
|
Re: EclipseLink OneToOne CascadeType.All is not working [message #1781381 is a reply to message #1781228] |
Tue, 06 February 2018 15:54 |
Chris Delahunt Messages: 1389 Registered: July 2009 |
Senior Member |
|
|
Hopefully, as the problem in the code provided is very wrong. This should be a simple bidirectional example but I think is confused because of the foreign key names and mistakenly set both sides up as if they are independent, unidirectional relationships set with their own PK as the foreign key. To avoid a multiple writable mappings error on deployment, they had to add the insertable = false, updatable = false, which tells JPA that the foreign key defined in the join column is read-only, making the application responsible for setting the value. So cascade could never work without manual intervention by the application to pull the field from the parent pk into the child PK. It also has a mappedby indicator as well as the joincolumn, which conflict with each other.
If you have a similar model, I would suggest looking at JPA @MapsId examples, which allow you to specify that the target value from the OneToOne should be used to set the primary key value. For the above example something simple like
public class EventDomain {
@Column(name = "ID_EVNT", nullable = false)
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private BigInteger eventIdentifier;
@OneToOne(fetch = FetchType.LAZY, mappedBy = "eventDomain", cascade = CascadeType.ALL, orphanRemoval = true)
private EventProcessStatusDomain eventProcessStatusDomain;
}
public class EventProcessStatusDomain implements Serializable {
@Id
@Column(name = "ID_EVNT", nullable = false)
private BigInteger eventIdentifier;
@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "ID_EVNT", referencedColumnName = "ID_EVNT", nullable = false)
@MapsId
private EventDomain eventDomain;
}
|
|
|
Powered by
FUDForum. Page generated in 0.24568 seconds