3 Replies Latest reply on May 25, 2009 4:49 AM by Emmanuel Rousselle

    Help: Hibernate-Seam persists instantly

    Dang LH Newbie

      Hello,


      I have a problem regarding Hibernate and persistence. In my view I'm using a backing bean called CreateCourse. In that bean 1 Hibernate managed Entity bean Course is processed. The CreateCourse bean looks like kind of this.



      @Scope(ScopeType.CONVERSATION)
      @Name("createCourse")
      public class CreateCourse{
      
              private EntityManager em;
              
              @RequestParameter
              private Integer coursesetup_id;
              private CourseSetup courseSetup;
              
              @RequestParameter
              private Integer course_id;
              private Course course;
              
              public void importCourseFromCourseSetup() {
              ...
              course.setCourseTime(courseSetup.getTime());
              course.setLocation(courseSetup.getLocation());
              ...
      
               return;
              }
      
              @Create
              @Begin(join=true)
              public String initialize() {
                      
                      //get the PersistenceContext
                      this.em = (EntityManager) Component.getInstance("entityManager",true);
                      
                      if(this.course_id != null)
                      {//work with a defined course
                              if(em!=null)
                              {
                                      this.course = em.find(Course.class, this.course_id);
                              }
                                      
                      }else{//work with a new course 
                              this.course = new Course();
                      }
                      
                      if(this.coursesetup_id != null)
                      {
                              if(em!=null)
                              {
                                 this.courseSetup =           
                                          em.find(CourseSetup.class,this.kurssetup_id);
                              }
                                      
                      }else{
                              this.courseSetup = new CourseSetup();
                      }
                      
                      System.out.println("initialised");
                      return "initialised";
              }
      



      the associating view is built as such, that the user can pick a CourseSetup and import an already defined courseSetup to the Course' definition. So when the the user clicks on inport a h:form will be submitted setting Course.coursesetup, and Course.importCourseFromCourseSetup() will be processed, which sets the Course.


      At the end the user shall be able to persist the Course finally, or just persist the edited content he/she's done.


      But now here comes the Problem i dont understand:


      When Course.importCourseFromCourseSetup() Hibernate instantly persist/writes the data into the DB. I don't understand this behavior at all, because i thought that one should clearly specify the persist operation (such as this.em.persist(this.course) or this.em.update(this.course) ... etc.).


      Can someone explain this behavior of Hibernate, or have i done an elemantary mistake somewhere ?


      P.S. the view is like that:



      <h:form id="courseform">
                              
         <rich:panel>
                  <f:facet name="header">course</f:facet>
      
                  <s:decorate id="nameField" template="../layout/edit.xhtml">
                      <ui:define name="label">courseSetup</ui:define>
                      
                      <h:selectOneMenu value="#{createCourse.courseSetup}" required="true">
                                      <s:selectItems var="courseSetup" value="#{courseSetupList.resultList}" label="#{courseSetup.coursename}"></s:selectItems>
                                      <s:convertEntity></s:convertEntity>
                              </h:selectOneMenu>
                              <h:commandButton type="submit"
                                      value="import" action="#{createCourse.importCourseFromCourseSetup()}">
                              </h:commandButton>        
              </s:decorate>
      ...
      <h:commandButton value="save"
                                      action="#{createCourse.save()}"
                         rendered="#{!createCourse.managedInstance}">
              </h:commandButton>
              <h:commandButton value="edit"
                                      action="#{createCourse.edit()}"
                         rendered="#{createCourse.managedInstance}">
              </h:commandButton>
          
      ...
      </h:form>
      



      the problem is that Hibernate persists/updates the data instantly as soon as the form is submitted.


      Thanks for the answers.


      LHDang