Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » OneToOne + MySQL issue
OneToOne + MySQL issue [message #635332] Tue, 26 October 2010 12:51 Go to next message
Eclipse UserFriend
Originally posted by: karsten.triadiary.com

Hi there,

I have little issue I have three objects:

copcompe => OneToMany => copcomki => OneToOne => coprpref

using the following definition:

copcompe:
//bi-directional many-to-one association to Copcomki
@OneToMany(mappedBy="copcompe", fetch=FetchType.LAZY,
cascade=CascadeType.ALL)
public List<Copcomki> getCopcomkis() {}

copcomki:
// bi-directional many-to-one association to Copcompe
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "COPCOMPE_ID", nullable = false,
referencedColumnName = "ID")
public Copcompe getCopcompe() {
return this.copcompe;
}

//bi-directional one-to-one association to Coprpref
@OneToOne(fetch=FetchType.LAZY, cascade=CascadeType.ALL, optional=false)
@JoinColumn(name="ID", referencedColumnName="COPCOMKI_ID", unique=true,
insertable=false, updatable=false)
public Coprpref getCoprpref() {
if (coprpref == null) {
coprpref = new Coprpref();
coprpref.setCopcomki(this);
}
return this.coprpref;
}

coprpref:
// bi-directional one-to-one association to Copcomki
@OneToOne(mappedBy = "coprpref", optional=false)
public Copcomki getCopcomki() {
return this.copcomki;
}

After creating a new copcompe adding a copcomki and coprpref I call
persist and the database throws the following exception:

javax.persistence.RollbackException: Exception [EclipseLink-4002]
(Eclipse Persistence Services - 2.1.0.v20100614-r7608):
org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: java.sql.BatchUpdateException: Field 'COPCOMKI_ID'
doesn't have a default value
Error Code: 1364
Query: ValueReadQuery(sql="SELECT LAST_INSERT_ID()")

I tested several different mapping definition for the one to one part,
but nothing seems to work. Other associations (OneToMany) in my
datamodel work perfect.

Any hints, how to solve that issue.

Thanks.
Karsten

(Yes, I scanned the news history and used google consulting.)
Re: OneToOne + MySQL issue [message #635638 is a reply to message #635332] Wed, 27 October 2010 14:59 Go to previous message
Chris Delahunt is currently offline Chris DelahuntFriend
Messages: 1039
Registered: July 2009
Senior Member
Hello,

I believe you have mapped the bidirectional relationship between copcomki and coprpref incorrectly. You seem to have a read-only relationship on copcomki ->coprpref specifying the ID in copcomki is the foreign key and the 'COPCOMKI_ID' field in coprpref is the target. I assume it is read-only since any relationship change would cause your ID field to change which is not allowed. The problem is that the coprpref ->copcomki relationship is set to be mappedby the other side - this means that it too is effectively read-only since the other side controls the relationship in the database.

You may want remove the insertable=false and updatable=false, and switch the joinColumn and Mappedby on the two relationships so that coprpref ->copcomki controls it. This will allow the foreign key in coprpref to be set when the relationship is.

Best Regards,
Chris
Previous Topic:Need to figure out how to properly configure EclipseLink
Next Topic:Trouble converting SQL into JPQL with OUTER JOIN
Goto Forum:
  


Current Time: Fri Nov 28 05:58:49 GMT 2014

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

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