0 Replies Latest reply on Dec 13, 2007 12:56 AM by gsegura

    how to exclude entities in flush

    gsegura

      Hello, I got into this kind of chicken-egg problem related to entity versioning:

      There is an entity Course which has a collection of CourseNote.
      When adding a CourseNote to the collection I have to obtain a managed instance of Course, whose version field is been incremented implicitly when the flush occurs (because it is been managed).

      And that is the problem because when more than one user are inserting CourseNotes to the collection, the second one will get an optimisticlockexception at the time he tries to merge his instance of the course

      I do need to access the course during CourseNote creation to do some work (so entityManager.getReference is not enough) but that very action is causing the course to get managed and involved in the transaction, which I do not want to happen.


      //validation omited
      Ticket ticket = (Ticket)entityManager
       .createQuery("select t from Ticket t where t.code=#{register.code}")
       .getSingleResult();
      
      CourseNote courseNote = ticket.getCourse().enroll(user) ;
      entityManager.persist(courseNote) ;
      
      //...
      entityManager.flush() ;
      
      



      class Course {
       public void enroll(User user) {
       CourseNote cn = new CourseNote(user,this) ;
       if(!getCourseNotes().contains(c)) {
       getCourseNotes().add(c) ;
       usuario.getCourseNotes().add(c) ;
       return c ;
       }
       throw new IllegalStateException(
       new StringBuilder(50)
       .append("User ")
       .append(user.getId())
       .append(" is already enrolled in the course ")
       .append(this.getId()).toString()) ;
       }
      }
      



      Please, some advice on this will be very very appreciated