Unidirectional OneToMany Insert [message #1061143] |
Thu, 30 May 2013 04:27  |
Eclipse User |
|
|
|
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 #1061340 is a reply to message #1061200] |
Fri, 31 May 2013 05:46  |
Eclipse User |
|
|
|
I modified the class Comment:
@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;
[b] @Basic(optional = false)
@Column(name = "Responset_Id")
private Integer responseId;
[/b]
and the code of the persist:
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.setResponseId(response.getResponseId())
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;
}
With this modification it works, is it right???
|
|
|
Powered by
FUDForum. Page generated in 0.02776 seconds