4 Replies Latest reply on Nov 22, 2008 12:41 AM by visumagic

    Need Help on JPA,Seam mutiple entity persistance

    visumagic
      Hi
      I'm working on MySQL,Seam 2.1 with 2 entities course and branch..  (course and branch tables got Auto incremental ids)

      Im using JPA,Hibernate with seam.
      I have a form where i can submit details of a course and multiple branches.

      ` entityManager.persist(course);
        for (Branch branch : branchs) {
               if(branch.getTitle()!=null&&!"".equals(branch.getTitle()))
                      {
                                      branch.setCourse(course);
                                      entityManager.persist(branch);
                      }
                             
              }


      But here i'm getting sql error



      3:30:54,812 INFO  =RegCourse= RegCourse.regCourse() action called with:
      13:30:54,812 INFO  [STDOUT] Hibernate:
          insert
          into
              engweb1.course
              (duration, title, id)
          values
              (?, ?, ?)
      13:30:54,828 INFO  [STDOUT] Hibernate:
          insert
          into
              engweb1.branch
              (title, Course_id, acro, id)
          values
              (?, ?, ?, ?)
      13:30:54,828 WARN  =JDBCExceptionReporter= SQL Error: 1452, SQLState: 23000
      13:30:54,828 ERROR =JDBCExceptionReporter= Duplicate key or integrity constraint violation message from server: "Cannot add or update a child row: a foreign key constraint fails (`engweb1/branch`, CONSTRAINT `fk_Branch_Course` FOREIGN KEY (`Course_id`) REFERENCES `course` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION)"
      13:30:54,828 ERROR [AbstractFlushingEventListener] Could not synchronize database state with session
      org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
              at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:71)
              at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
              at org.hibernate.jdbc

        • 1. Re: Need Help on JPA,Seam mutiple entity persistance
          visumagic

          As per my analysis , jpa is performing jdbc batch update, here it's trying to persist branches (which are linked to course) , but course is still not updated in db so it's not able to sync with db in the middle of transaction.
          Is there any way to split control this batch update.


          please help me..
          thanks
          raghu

          • 2. Re: Need Help on JPA,Seam mutiple entity persistance
            visumagic

            the problem is


            How do you retrieve an auto increment key after persisting to the database ?


            if we use auto-increment id ,the id is not sync with database after persisting.


            How to retrieve actual object is the problem.  This is interesting scenario ,i never faced ,
            if any one have similar experience , plz help me ..


            thanks
            raghu 

            • 3. Re: Need Help on JPA,Seam mutiple entity persistance
              visumagic
              Seam>Hibernate is not creating hibernate_sequences[sequence_next_hi_value, sequence_name]
              table , because of this @GeneratedValue(strategy=GenerationType.TABLE) also not working in Seam.

              please help me..

              raghu
              • 4. Re: Need Help on JPA,Seam mutiple entity persistance
                visumagic
                Amazing discovery my me ..

                Finally after a n number of frustrating hours,i^10 number of trails , I found the reason why it's failing ...
                Even the reason is simple but,took much time ..

                I'm not sure what i'm lagging - off , but simple point but pinch

                Changes I have made :
                1)Not removing auto_increment on primary key of two tables
                otherwise:It will file with "Field 'id' doesn't have a default value"
                2)moving annotation @GeneratedValue from id declaration to getter method
                otherwise:Duplicate key or integrity constraint violation
                3) For now I'm taking RESOURCE_LOCAL rather JTA in In persistance.xml

                I felt that , I took much bcoz I'm really lacking of enough experience. In such cases I know only Trail and error method and getting help from forums .. 

                I feel happy if my post helps others,such that they wont wast time ..


                thanks