Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » EclipseLink OneToOne CascadeType.All is not working(EclipseLink OneToOne CascadeType.All is not working in BiDirection mapping)
icon5.gif  EclipseLink OneToOne CascadeType.All is not working [message #1716599] Sun, 06 December 2015 06:22 Go to next message
Vimal Lakshmanan is currently offline Vimal LakshmananFriend
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 #1781228 is a reply to message #1716599] Sat, 03 February 2018 16:30 Go to previous messageGo to next message
Arco van der Velden is currently offline Arco van der VeldenFriend
Messages: 6
Registered: October 2017
Junior Member
Did you ever solve this ?
Re: EclipseLink OneToOne CascadeType.All is not working [message #1781381 is a reply to message #1781228] Tue, 06 February 2018 15:54 Go to previous message
Chris Delahunt is currently offline Chris DelahuntFriend
Messages: 1296
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;
}
Previous Topic:Unable to retrieve timestampz from postgresql db
Next Topic:@OneToOne relation PK not filled
Goto Forum:
  


Current Time: Sat May 26 02:38:48 GMT 2018

Powered by FUDForum. Page generated in 0.01878 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top