5 Replies Latest reply on Dec 18, 2007 2:27 PM by pmuir

    question about components and persistence

    gsegura

      Hello, I'm having problems to get same during method invocation, I mean:
      there is the class User which has a collection of items.
      - One instance of user is maintained during session,
      - then there is this action method which adds an item to the user's collection
      - there is also another method which ask question about items in the users collection,

      the problem is that the user instance is not the same in those methods, so the collection has not the same data. I've tried using merge at the beginning of those methods but an OptimisticLockException is thrown. here is my code so far:

      @Scope(ScopeType.CONVERSATION)
      @Name("exam")
      @Restrict("#{identity.loggedIn}")
      public class ExamAction implements Evaluacion {
       //injected from user in session
       @In(required = true)
       @Out
       private User user;
      
      
       @End
       public void calificate() {
       //... calculate calification, then:
       Calification cal = user.setCalificacion(course, points) ;
      
       Calification mergedcal = entityManager.merge(cal) ;
       entityManager.persist(cal) ;
       }
      
       public boolean isExamDone() {
       // this throws OptimisticLockException, casued by StaleObjectStateException
       //but without this the user (hashcode) is different than the one injected in method calificate
       //so I'm confused :S
      
       User mergedUser = entityManager.merge(user) ;
       Calification cal = mergedUser.findCalificacion(course) ;
       return cal.getCalificacionEvaluacion()!=null ;
       }
      }
      

      @Entity
      @Table(name = "user", catalog = "test1")
      public class User implements java.io.Serializable {
      
       @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "user")
       public Set<Calification> getCalificacions() {
       return this.calificacions;
       }
      
       public void setCalificacions(Set<Calification> calificacions) {
       this.calificacions = calificacions;
       }
      
       public Calification findCalificacion(Course course) {
       for(Calification c : getCalificacions())
       if(c.getId().getIdCourse()==course.getId())
       return c ;
       throw new NoSuchElementException(
       new StringBuilder(52)
       .append("course ")
       .append(course.getId())
       .append(", not found for user ")
       .append(getId()).toString()) ;
       }
      
       public Calification setCalificacion(Course course, int points) {
       Calification c = null ;
       try {
       c = findCalificacion(course) ;
       } catch(NoSuchElementException e) {
       c = new Calification(this, course, 0) ;
       getCalificacions().add(c) ;
       }
      
       c.setPoints(points) ;
       return c ;
       }
      }


      so the question is, how can I modify the user's collection of items and access same collection every time (I tried outjecting user but no effect) or how can I synchronize those objects before using them?
      please, I really appreciate some advice here
      regards