0 Replies Latest reply on May 7, 2010 10:57 PM by sburgula1

    inserting a new row into a list hibernate query

      Hi

      I have a object A which has one many relationship with object B. A->Student, B->ConcentrationPlan

      now if I add a new concentration to the list.

      I do the following in terms of code
      selectedStudent.getConcentrationPlan.add(concentrationPlan);
      studentDao.flush();
      loadConcentrationPlans();

      selectedStudent is of type Student.
      It will get the list of Concentrations for a Student.
      And adds the new one to the list.

      After I add the new object to the list, and flush it into DB, I notice hibernate query generated for this statement.

      Hibernate generates a insert query as expected.

      But what surprises me is that it generates a update query as well.

      Please let me know why this is happening.

      @OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
      @JoinColumn(name = "SUBJECT_ID", referencedColumnName = "SUBJECT_ID")
      @OrderBy(clause = "course_no")
      @Cascade(org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
      public List<ConcentrationPlan> getConcentrationPlans() {
      return this.concentrationPlans;

      public void setConcentrationPlans(List<ConcentrationPlan> concentrationPlans) {
      this.concentrationPlans = concentrationPlans;
      }

      @ManyToOne(fetch = FetchType.LAZY)
      @JoinColumn(name = "SUBJECT_ID", referencedColumnName = "SUBJECT_ID", nullable = false, insertable = false, updatable = false)
      public Student getStudent() {
      return student;
      }

      public void setStudent(Student student) {
      this.student = student;
      }

      The One to many from Student to ConcentrationPlan is given above. Also the ManytoOne in ConcentrationPlan is also given.

      I will also put in the hibernate query I see in the console.

      insert
      into
      KARYN.CONCENTRATION_PLAN
      (CONC_AREA_CODE, COURSE_TITLE, DATE_ENTERED, DATE_UPDATED, ENTERED_BY, TAKEN, UNIT, UPDATED_BY, COURSE_NO, SUBJECT_ID)
      values
      (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)

      16:48:33,209 INFO [STDOUT] Hibernate:
      update
      KARYN.CONCENTRATION_PLAN
      set
      SUBJECT_ID=?
      where
      COURSE_NO=?
      and SUBJECT_ID=?
      }


      The update query is the one that does not make sense.

      One more thing I have to mention.

      I do call the method loadConcentrationPlans if you notice above in the code.

      public void loadConcentrationPlans() {

      concentrationList = selectedStudent.getConcentrationPlans();
      /*Set<ConcentrationPlan> concentrationSet = selectedStudent.getConcentrationPlans();
      concentrationList = new ArrayList<ConcentrationPlan>(concentrationSet);*/

      if (concentrationList.size() == 0) {
      mode = "add";
      concPlan = new ConcentrationPlan();

      }else{
      mode = "view";
      }

      }

      This method basically calls the view page to display the list by querying the DB for a particular student.



      Please let me know what could be happening.

      thanks
      Sai