@OneToOne relation PK not filled [message #1781229] |
Sat, 03 February 2018 11:43  |
Eclipse User |
|
|
|
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 #1781396 is a reply to message #1781382] |
Tue, 06 February 2018 12:50  |
Eclipse User |
|
|
|
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
|
|
|
Powered by
FUDForum. Page generated in 0.03983 seconds