Uploaded image for project: 'Hibernate ORM'
  1. Hibernate ORM
  2. HHH-8476

Bulk delete doesn't cascade delete on join table

    Details

    • Type: Improvement
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: 4.2.5
    • Fix Version/s: 4.2.6, 4.3.0.Beta5
    • Component/s: hibernate-core
    • Labels:
      None
    • Environment:
      Hibernate 4.2.5.Final, Firebird, Tomcat 7, jdk 1.7, Windows 7
    • Last commented by a user?:
      true

      Description

      HQL delete doesn't cascade delete on join table.

      Person bob = new Person();
      bob.setId(1l);
      bob.setFirstname("bob");
      getCurrentSession().save(bob);

      Cat felix = new Cat();
      felix.setId(1l);
      felix.setName("felix");
      Set<Person> owners = new HashSet<Person>();
      owners.add(bob);
      felix.setOwners(owners);
      getCurrentSession().save(felix);

      Query query = getCurrentSession().createQuery("delete com.company.product.domain.Cat c where c.id=:param_id)");
      query.setParameter("param_id", 1l);
      return query.executeUpdate();

      insert into PERSON (FIRSTNAME, ID) values (?, ?)
      insert into CAT (NAME, ID) values (?, ?)
      insert into CAT_PERSON (ID_CAT, ID_PERSON) values (?, ?)
      delete from CAT where ID=?

      -> ConstraintViolationException: could not execute statement
      violation of FOREIGN KEY constraint "FK_3QS4XQHEUL1KIQILV53S1F0GB" on table "CAT_PERSON"

      Note that if Cat extends Animal with joined-subclass mapping everything is OK.

        Attachments

        1. Cat.hbm.xml
          0.7 kB
        2. Cat.java
          0.6 kB
        3. Person.hbm.xml
          0.5 kB
        4. Person.java
          0.4 kB

          Issue Links

            Activity

            Hide
            aurelien Aurelien DROUARD added a comment -

            Yes Brett, that's exactly the problem.

            Show
            aurelien Aurelien DROUARD added a comment - Yes Brett, that's exactly the problem.
            Hide
            brmeyer Brett Meyer added a comment -

            Made it a PR – appreciate reviews before I push:

            https://github.com/hibernate/hibernate-orm/pull/599

            Show
            brmeyer Brett Meyer added a comment - Made it a PR – appreciate reviews before I push: https://github.com/hibernate/hibernate-orm/pull/599
            Hide
            aurelien Aurelien DROUARD added a comment -

            Brett, will you push the fix before 4.2.6 release ?

            Show
            aurelien Aurelien DROUARD added a comment - Brett, will you push the fix before 4.2.6 release ?
            Hide
            brmeyer Brett Meyer added a comment -

            Aurelien DROUARD, yes, definitely. Just wanted a few eyes on the commit first.

            Show
            brmeyer Brett Meyer added a comment - Aurelien DROUARD , yes, definitely. Just wanted a few eyes on the commit first.
            Hide
            aurelien Aurelien DROUARD added a comment -

            Hello,
            In 4.2.6.Final release the delete from CAT_PERSON is now ok. Thanks Brett.
            But I've got anohter problem with "map" mapping.
            Could you handle this case ?

            Cat.hbm.xml :
            ...
            <map name="map" table="MAP">
            <key column="CAT_KEY" />
            <map-key type="java.lang.Integer">
            <column name="MAP_KEY" />
            </map-key>
            <element column="MAP_VALUE" type="java.lang.Integer" />
            </map>
            ...
            Cat.java :
            public class Cat
            {
            ...
            private Map<Integer, Integer> map;
            ...

            Java test :
            Person bob = new Person();
            bob.setId(1l);
            bob.setFirstname("bob");
            getCurrentSession().save(bob);

            Cat felix = new Cat();
            felix.setId(1l);
            felix.setName("felix");
            Set<Person> owners = new HashSet<Person>();
            owners.add(bob);
            felix.setOwners(owners);

            felix.setMap(new HashMap<Integer, Integer>());
            felix.getMap().put(1, 10);
            felix.getMap().put(2, 10);
            felix.getMap().put(3, 15);
            felix.getMap().put(4, 20);

            getCurrentSession().save(felix);

            Query query = getCurrentSession().createQuery("delete com.company.product.domain.Cat c where c.id=:param_id)");
            query.setParameter("param_id", 1l);
            query.executeUpdate();

            Generated SQL:
            insert into PERSON (FIRSTNAME, ID) values (?, ?)
            insert into CAT (NAME, ID) values (?, ?)
            insert into CAT_PERSON (ID_CAT, ID_PERSON) values (?, ?)
            insert into MAP (CAT_KEY, MAP_KEY, MAP_VALUE) values (?, ?, ?)
            insert into MAP (CAT_KEY, MAP_KEY, MAP_VALUE) values (?, ?, ?)
            insert into MAP (CAT_KEY, MAP_KEY, MAP_VALUE) values (?, ?, ?)
            insert into MAP (CAT_KEY, MAP_KEY, MAP_VALUE) values (?, ?, ?)
            delete from CAT_PERSON where (ID_CAT) in (select ID from CAT where ID=?)
            delete from CAT where ID=?

            -> ConstraintViolationException: could not execute statement
            violation of FOREIGN KEY constraint "FK_DX9BH52S5F00VYW30I1M8F0FM" on table "MAP"

            Show
            aurelien Aurelien DROUARD added a comment - Hello, In 4.2.6.Final release the delete from CAT_PERSON is now ok. Thanks Brett. But I've got anohter problem with "map" mapping. Could you handle this case ? Cat.hbm.xml : ... <map name="map" table="MAP"> <key column="CAT_KEY" /> <map-key type="java.lang.Integer"> <column name="MAP_KEY" /> </map-key> <element column="MAP_VALUE" type="java.lang.Integer" /> </map> ... Cat.java : public class Cat { ... private Map<Integer, Integer> map; ... Java test : Person bob = new Person(); bob.setId(1l); bob.setFirstname("bob"); getCurrentSession().save(bob); Cat felix = new Cat(); felix.setId(1l); felix.setName("felix"); Set<Person> owners = new HashSet<Person>(); owners.add(bob); felix.setOwners(owners); felix.setMap(new HashMap<Integer, Integer>()); felix.getMap().put(1, 10); felix.getMap().put(2, 10); felix.getMap().put(3, 15); felix.getMap().put(4, 20); getCurrentSession().save(felix); Query query = getCurrentSession().createQuery("delete com.company.product.domain.Cat c where c.id=:param_id)"); query.setParameter("param_id", 1l); query.executeUpdate(); Generated SQL: insert into PERSON (FIRSTNAME, ID) values (?, ?) insert into CAT (NAME, ID) values (?, ?) insert into CAT_PERSON (ID_CAT, ID_PERSON) values (?, ?) insert into MAP (CAT_KEY, MAP_KEY, MAP_VALUE) values (?, ?, ?) insert into MAP (CAT_KEY, MAP_KEY, MAP_VALUE) values (?, ?, ?) insert into MAP (CAT_KEY, MAP_KEY, MAP_VALUE) values (?, ?, ?) insert into MAP (CAT_KEY, MAP_KEY, MAP_VALUE) values (?, ?, ?) delete from CAT_PERSON where (ID_CAT) in (select ID from CAT where ID=?) delete from CAT where ID=? -> ConstraintViolationException: could not execute statement violation of FOREIGN KEY constraint "FK_DX9BH52S5F00VYW30I1M8F0FM" on table "MAP"

              People

              • Votes:
                0 Vote for this issue
                Watchers:
                4 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: