Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » Unidirectional OneToMany Insert
Unidirectional OneToMany Insert [message #1061143] Thu, 30 May 2013 08:27 Go to next message
pdvmipv Mising name is currently offline pdvmipv Mising nameFriend
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 Go to previous messageGo to next message
Chris Delahunt is currently offline Chris DelahuntFriend
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

Re: Unidirectional OneToMany Insert [message #1061340 is a reply to message #1061200] Fri, 31 May 2013 09:46 Go to previous message
pdvmipv Mising name is currently offline pdvmipv Mising nameFriend
Messages: 12
Registered: October 2010
Junior Member
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???
Previous Topic:Modify schema using persistence layer
Next Topic:Object: is not a known entity type.
Goto Forum:
  


Current Time: Fri Mar 29 11:27:58 GMT 2024

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

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

Back to the top