7 Replies Latest reply on Dec 20, 2006 8:06 PM by Daniel Young

    Updating instead of deleting

    allan bronson Newbie

      I am having trouble deleting data. I am getting an update on a child when I try and do a remove on its parent. I did a oneToMany mapping in the Parent witha fetch = fetchtype.eager and cascade type = cascade.all.

      I do not use mappedby and the child does not have a parent attribute in it.

      Any ideas as to why hibernate is trying to update the child's primary keys to null and stead of the delete?

      ab

        • 2. Re: Updating instead of deleting
          allan bronson Newbie

          The data I am deleting has a parent - child relationship ( OneToMany ) and will try and give you as much information as possible.

          I am using JBoss 4.0.4GA, EJB3 RC7 with Oracle 9I. I create the db tables myself with the foreign keys and primary keys with a script.

          The Parent uses an embedded primary key class

          ParentPK class
          Long locationId
          String recordId

          @Column( name = locationId, nullable)
          Long getLocationId(){ return locationId; } ( have one for recordId)

          Parent class
          ParentPK parentPk
          Set children
          more attributes

          @EmbeddedId
          ParentPK getPK

          @OneToMany( cascade = CascadeType.ALL, fetch = FetchType=Lazy )
          @JoinColumns(
          @JoinColumn name = "locationId", referencedColumnName="locationId"
          @JoinColumn name = "recordId",referencedColumnName="recordId" )
          public Set getChild(){ return children; }


          The child uses an embeded id as well. It is written the same as above and has a primary key of recordId, locationId and Timestamp timeCreated attribute.

          I do not have a Parent attribute in the child and thus not a ManyToOne relationship between the child and parent. ( IS this bad? I never reference the Parent with the child )


          When I try and delete the Parent and hope it cascades through, I write this code. em = EntityManager, deletePrior is a Timestamp object passed in.

          Query query = em.createQuery("delete from Parent where lastUpdated < :deletePrior").setParameter("deletePrior", deletePrior);
          int updatedEntities = query.executeUpdate();

          When I do this I get:
          integrity constraint (SYSTEM.CHILD_FK) violated - child record found.

          I also find a message in the exception that says:
          AbstractFlushingEventListener] Flushed 0 insertions, 1 updates, 0 deletions to 1 objects


          I also tried to get a list with the above query and delete each object individually.

          em.remove( Parent Obj );

          I get:
          cannot update ("SYSTEM"."CHILD"."LOCATIONID") to NULL;

          This is in the exception as well
          ERROR could not synchronize database state with session


          I hope this helps in explaining what I am doing and if someone could let me know if I am doing something wrong, I would appreciate it.

          allan

          • 3. Re: Updating instead of deleting
            Emmanuel Bernard Master

            bulk operations are not supposed to handle associations

            • 4. Re: Updating instead of deleting
              allan bronson Newbie

              I am not sure what you mean by bulk operations.

              The cascade does not work if I do either of these:

              Query query = em.createQuery("delete from Parent where lastUpdated < :deletePrior").setParameter("deletePrior", deletePrior);
              int updatedEntities = query.executeUpdate();

              or

              if I do

              Query query = em.createQuery("delete from Parent where lastUpdated < :deletePrior").setParameter("deletePrior", deletePrior);
              List list = query.list();

              loop
              {

              em.remove( Parent Obj );
              }

              I get:
              cannot update ("SYSTEM"."CHILD"."LOCATIONID") to NULL;



              allan

              • 5. Re: Updating instead of deleting
                allan bronson Newbie

                 

                "abronson" wrote:
                I am not sure what you mean by bulk operations.

                The cascade does not work if I do either of these:

                Query query = em.createQuery("delete from Parent where lastUpdated < :deletePrior").setParameter("deletePrior", deletePrior);
                int updatedEntities = query.executeUpdate();

                or

                if I do

                Query query = em.createQuery("from Parent where lastUpdated < :deletePrior").setParameter("deletePrior", deletePrior);
                List list = query.list();

                loop
                {

                em.remove( Parent Obj );
                }

                I get:
                cannot update ("SYSTEM"."CHILD"."LOCATIONID") to NULL;



                allan


                • 6. Re: Updating instead of deleting
                  Daniel Young Novice

                  I have the same problem, a uni-directional one to many on a FK with CascadeType ALL.

                  I try to delete a parent entity A, and Hibernate tries to update the FK column on the child table B to null, rather than performing a delete.

                  DEBUG 21-12 10:51:52,375 (Log4JLogger.java:debug:84) -Deleting collection: [A.B#A = [3, TestDataFactory OrgName - EGYM]]
                  DEBUG 21-12 10:51:52,375 (Log4JLogger.java:debug:84) -about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
                  DEBUG 21-12 10:51:52,375 (Log4JLogger.java:debug:84) -update B set msg_id=null where msg_id=?
                  Hibernate: update B set msg_id=null where msg_id=?
                  DEBUG 21-12 10:51:52,375 (Log4JLogger.java:debug:84) -done deleting collection
                  DEBUG 21-12 10:51:52,375 (Log4JLogger.java:debug:84) -Executing batch size: 1
                  DEBUG 21-12 10:51:52,390 (Log4JLogger.java:debug:84) -about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
                  DEBUG 21-12 10:51:52,390 (Log4JLogger.java:debug:84) -skipping aggressive-release due to flush cycle
                  DEBUG 21-12 10:51:52,390 (Log4JLogger.java:debug:89) -Could not execute JDBC batch update [update B set msg_id=null where msg_id=?]
                  




                  • 7. Re: Updating instead of deleting
                    Daniel Young Novice

                    http://www.jboss.com/index.html?module=bb&op=viewtopic&t=79385

                    Setting updatable = false fixed this for me. I'm not entirely sure why, but it's a perfectly sufficient solution for me, as this FK column never would be updated.