Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » @OneToOne relation PK not filled(Pk of child entity not filled on persist)
@OneToOne relation PK not filled [message #1781229] Sat, 03 February 2018 16:43 Go to next message
Arco van der Velden is currently offline Arco van der VeldenFriend
Messages: 6
Registered: October 2017
Junior Member
I have two entities with a @OneToOne relation the problem is that the PK isn't filled automatically with the @GeneratedValue(strategy = GenerationType.IDENTITY) of the other entity.

I have the following definitions

public class Relatie implements Serializable
{

private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "relatienummer", nullable = false)
private Integer relatienummer;
@OneToOne(cascade = CascadeType.ALL, mappedBy = "relatie", fetch = FetchType.LAZY)
private Persoon persoon;
}

public class Persoon implements Serializable
{

private static final long serialVersionUID = 1L;
@Id
@Basic(optional = false)
@NotNull
@Column(name = "relatienummer", nullable = false)
private Integer relatienummer;
@JoinColumn(name = "relatienummer", referencedColumnName = "relatienummer", nullable = false, insertable = false, updatable = false)
@OneToOne(optional = false, fetch = FetchType.LAZY)
private Relatie relatie;
}

When persisting Relatie with Persoon set with entitymanger.persist the following SQL is executed on commit.

Fine: INSERT INTO relatie (notitie, relatietype, taalcode) VALUES (?, ?, ?)
bind => [null, P, DE]
Fine: SELECT LAST_INSERT_ID()
Fine: INSERT INTO persoon (relatienummer, achternaam, initialen, tussenvoegsel, voornaam) VALUES (?, ?, ?, ?, ?)
bind => [null, null, asd, null, null]
Fine: SELECT 1
Warning: Local Exception Stack:
Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.6.4.qualifier): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'relatienummer' cannot be null

Can anybody help me out with this ?




Re: @OneToOne relation PK not filled [message #1781294 is a reply to message #1781229] Mon, 05 February 2018 17:19 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
Nobody ?
Can someone at least tell me if this should work according to the EclipseLink specifications ?
Re: @OneToOne relation PK not filled [message #1781382 is a reply to message #1781294] Tue, 06 February 2018 15:58 Go to previous messageGo to next message
Chris Delahunt is currently offline Chris DelahuntFriend
Messages: 1389
Registered: July 2009
Senior Member
It should not work automatically as you have specified the join column to be read-only. insertable = false, updatable = false specify that you are setting the foreign key field some other way, so for this to work, the application would need to persist the parent (and likely flush) to obtain the primary key value and manually set the Childs primery key value with it as well as the relationship.

See JPA 2.0 derived ID and MapsId examples for how you might do this differently if you want JPA to set the value for you.
Re: @OneToOne relation PK not filled [message #1781396 is a reply to message #1781382] Tue, 06 February 2018 17:50 Go to previous message
Arco van der Velden is currently offline Arco van der VeldenFriend
Messages: 6
Registered: October 2017
Junior Member
Thnx for your response I've got it to work by changing the OneToOne to:

@JoinColumn(name = "relatienummer", referencedColumnName = "relatienummer", nullable = false, insertable = true, updatable = true)
@OneToOne(optional = false, fetch = FetchType.LAZY)
@MapsId
private Relatie relatie;

In my Java code I also had to fillup the relatie attribute with the relatie object.
Is this the way to do it or is there a way to do it without the coding in Java ?

Thnx in advance
Previous Topic:EclipseLink OneToOne CascadeType.All is not working
Next Topic:ManyToMany @BatchFetch parameters
Goto Forum:
  


Current Time: Tue Mar 19 09:04:57 GMT 2024

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

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

Back to the top