0 Replies Latest reply on Dec 30, 2007 1:35 PM by trouby

    OnetoMany - cascade all

    trouby

      Hey,

      I think I have a very basic confusion regarding cascade,
      I have the following simple relationship:

      public class message {
      
      private Set<Comment> comments;
      
      @OneToMany(mappedBy = "message", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
      public Set<MessageComment> getComments() {
       return comments;
      }
      
      //set...
      }
      



      And
      private Message message;
      
      @ManyToOne(optional=false)
      public Message getMessage() {
      return message;
      }
      
      //setter
      



      I'm trying to persist a new comment by:

      //assuming a message with id 1 exists in db
      Message m = entityManager.find(Message.class,new Long(1));
      Comment c = new Comment();
      c.setMessage(m);
      m.getComments.add(c);
      entityManager.merge(m);
      



      I don't want to persist the comment directly but through the message,

      if 'getComments' is cascaded with ALL, shouldn't the new comments get persisted when em.merge(m) is invoked?




      Thanks,

      Asaf.