Skip to main content



      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 11:43 Go to next message
Eclipse UserFriend
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 12:19 Go to previous messageGo to next message
Eclipse UserFriend
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 10:58 Go to previous messageGo to next message
Eclipse UserFriend
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 12:50 Go to previous message
Eclipse UserFriend
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: Mon Apr 14 18:45:51 EDT 2025

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

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

Back to the top