Unidirectional OneToMany Insert [message #1061143] |
Thu, 30 May 2013 08:27 |
pdvmipv Mising name Messages: 12 Registered: October 2010 |
Junior Member |
|
|
I have problem with persist data in unidirectional OneToMany relationion
@Entity
@Table(name = "Response")
public class Response implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "Response_Id")
private Integer responseId;
@JoinColumn(name = "Response_Id", referencedColumnName = "Response_Id")
@OneToMany(cascade = CascadeType.ALL)
@BatchFetch(BatchFetchType.JOIN)
private List<Comment> commentList;
.....
@Entity
@Table(name = "Comment")
public class Comment implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "Comment_Id")
private Integer commentId;
....
To have a unidirectional relation I removed the reference in table Comment of the object Response, the Response_Id is a foreign key of table Comment-Response (primaty key of response_Id).
When I try to insert a comment, after insert a Response with this code:
Response response = new Response();
response.setUserId(userId);
response.setDate(new java.sql.Date(date.getTime()));
response.setReject(false);
response.setText(text);
try {
em.persist(response);
} catch (Exception ex) {
jmipvRC.setError(ex.getMessage());
return -1;
}
After....
Comment comment = new Comment();
comment.setCommentType(commentType);
comment.setUserId(userId);
comment.setDate(date);
comment.setText(text);
response.getCommentList().add(comment);
try {
em.persist(comment);
} catch (Exception ex) {
ex.getMessage();
return;
}
I got this error:
(Eclipse Persistence Services - 2.4.1.v20121003-ad44345): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: java.sql.SQLException: Field Response_Id doesnt have a default value
Error Code: 1364
Call: INSERT INTO Comment (Date, Text, User_Id, CommentType_Id) VALUES (?, ?, ?, ?)
bind => [4 parameters bound]
Query: InsertObjectQuery(jmipv.eventreport.lib.db.Comment[ commentId=null ])
How think because I didn't set the Response_Id in the comment. How I can do it?
How I can set the Response_Id? I don't want to have a bidirectional relation to improve performance.
|
|
|
Re: Unidirectional OneToMany Insert [message #1061200 is a reply to message #1061143] |
Thu, 30 May 2013 13:54 |
Chris Delahunt Messages: 1389 Registered: July 2009 |
Senior Member |
|
|
The problem is that the "Response_Id" field isn't controlled by the Comment entity, and so will not be set when comment is persisted - it instead gets set afterward once the Response.commentList relationship is processed. EclipseLink has a feature request filed to have this behavior changed that you can vote for here: https://bugs.eclipse.org/bugs/show_bug.cgi?id=391279
Currently the only way to have the field inserted with a value when the Comment is inserted is to include a writable mapping for it within the Comment entity - the most common way is to use a standard bidirectional 1:M with a writable 1:1 backpointer. Otherwise, remove the not-null constraint (which I assume is preventing the insert from occurring) or delay constraint processing until the end of the transaction, as EclipseLink will issue an update later on once the Response.commentList relationship is processed.
Best Regards,
Chris
[Updated on: Thu, 30 May 2013 13:54] Report message to a moderator
|
|
|
|
Powered by
FUDForum. Page generated in 0.05447 seconds