title: Cannot delete or update a parent row: a foreign key constraint fails (hibernate xml mapping)
body: <p>I would like to delete all the groups among which a user is an owner but it does not work at the moment. I think that there is something which is lacking at the level of the mapping User.hbm.xml or Group.hbm.xml but I do not know. Error is <strong>"Cannot delete or update a parent row: a foreign key constraint fails (<code>sharedmap</code>.<code>groupe</code>, CONSTRAINT <code>FK_gq7win10rtxufsxu1n5istm2p</code> FOREIGN KEY (<code>user_id</code>) REFERENCES <code>user</code> (<code>id</code>))"</strong></p>
  
  <p>Here are the classes and the files xml concerned:</p>
  
  <p><strong>User.java</strong></p>
  
  <pre><code>public class User {
  
      /** Attributs */ 
  
  
      @XmlTransient
      private Set&lt;Group&gt; proprietaire;
  
      /** Constructeur */ 
      public User() {
      }
  
      public User(String telephone, String pseudo, String email) {
          super();
          this.pseudo = pseudo;
          this.telephone = telephone;
          this.email = email;
      }
  
  
      public Set&lt;Group&gt; getProprietaire() {
          return proprietaire;
      }
  
      public void setProprietaire(Set&lt;Group&gt; proprietaire) {
          this.proprietaire = proprietaire;
      }
  
      ...
  
  }
  </code></pre>
  
  <p><strong>UserDao</strong></p>
  
  <pre><code>public class UserDao {
  
      private static SessionFactory sessionFactory = SessionConfiguration.getFactory();
  
      /**
       * Method to ADD a user in the database
       * 
       * @param user
       * @return user_id
       */
      public static Integer addUser(User user) {
          Integer userID = null;
          Session session = sessionFactory.openSession();
          Transaction tx = null;
  
          try {
              tx = session.beginTransaction();
              userID = (Integer) session.save(user);          
              tx.commit();
          } catch (HibernateException e) {
              if (tx != null)
                  tx.rollback();
              e.printStackTrace();
          } finally {
              session.close();
          }
          return userID;
      }
  
      /**
       * 
       * Method to DELETE a user from the records
       * 
       * @param telephone
       */
      public static void deleteUser(String telephone) {
          Session session = sessionFactory.openSession();
          Transaction tx = null;
          String query = "select u from User u where u.telephone = :telephone";
          User user = (User) session.createQuery(query)
                  .setString("telephone", telephone).uniqueResult();
          try {
              tx = session.beginTransaction();
              session.delete(user);
              tx.commit();
          } catch (HibernateException e) {
              if (tx != null)
                  tx.rollback();
              e.printStackTrace();
          } finally {
              session.close();
          }
      }
  }
  </code></pre>
  
  <p><strong>User.hbm.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;!DOCTYPE hibernate-mapping PUBLIC 
   "-//Hibernate/Hibernate Mapping DTD//EN"
   "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"&gt; 
  
  &lt;hibernate-mapping&gt;
     &lt;class name="modele.User" table="user"&gt;
        &lt;meta attribute="class-description"&gt;
           This class contains the user detail. 
        &lt;/meta&gt;
        &lt;id name="id" type="int" column="id"&gt;
           &lt;generator class="native"/&gt;
        &lt;/id&gt;
        &lt;property name="pseudo" column="pseudo" type="string"/&gt;
        &lt;property name="telephone" column="telephone" type="string" not-null="true" unique="true"/&gt;
        &lt;property name="email" column="email" type="string"/&gt;
  
        &lt;!--  Mapping Set&lt;Demande&gt; demandes --&gt;
        &lt;set name="demandes" cascade="save-update,delete" lazy="false"&gt;
           &lt;key column="demandeur_id"/&gt;        
           &lt;one-to-many class="modele.Demande"/&gt;
        &lt;/set&gt;
  
        &lt;!--  Mapping Set&lt;Invitation&gt; aInvite --&gt;
        &lt;set name="aInvite" cascade="save-update,delete" lazy="false"&gt;
           &lt;key column="inviteur_id"/&gt;
           &lt;one-to-many class="modele.Invitation"/&gt;
        &lt;/set&gt;
  
        &lt;set name="notifications" cascade="save-update,delete" lazy="false"&gt;
           &lt;key column="user_id"/&gt;
           &lt;one-to-many class="modele.Notification"/&gt;
        &lt;/set&gt;
  
  
        &lt;set name="groups"  table="participation" lazy="false" inverse="true"&gt;
           &lt;key column="user_id"/&gt;
           &lt;many-to-many column="group_id" class="modele.Group"/&gt;
        &lt;/set&gt;
  
       &lt;!--  Mapping Set&lt;Group&gt; proprietaire --&gt;
        &lt;set name="proprietaire" cascade="save-update,delete" lazy="false"&gt;
           &lt;key column="user_id"/&gt;
           &lt;one-to-many class="modele.Group"/&gt;
        &lt;/set&gt;      
  
     &lt;/class&gt;
  &lt;/hibernate-mapping&gt;
  </code></pre>
  
  <p><strong>Group.hbm.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;!DOCTYPE hibernate-mapping PUBLIC 
   "-//Hibernate/Hibernate Mapping DTD//EN"
   "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"&gt; 
  
  &lt;hibernate-mapping&gt;
     &lt;class name="modele.Group" table="groupe"&gt;
        &lt;meta attribute="class-description"&gt;
           This class contains the publicEvent detail. 
        &lt;/meta&gt;
        &lt;id name="id" type="int" column="id"&gt;
           &lt;generator class="native"/&gt;
        &lt;/id&gt;
        &lt;property name="description" column="description" type="string"/&gt;
        &lt;property name="hashtag" column="hashtag" type="string"/&gt;
        &lt;property name="password" column="password" type="string"/&gt;
        &lt;!--  Mapping ArrayList&lt;Marqueur&gt; marqueurs --&gt;
  
        &lt;set name="marqueurs" cascade="save-update,delete" lazy="false"&gt;
           &lt;key column="group_id"/&gt;
           &lt;one-to-many class="modele.Marqueur"/&gt;
        &lt;/set&gt;
        &lt;!--  Mapping Set&lt;Invitation&gt; invitations --&gt;
        &lt;set name="invitations" cascade="save-update,delete" lazy="false"&gt;
           &lt;key column="group_id"/&gt;
           &lt;one-to-many class="modele.Invitation"/&gt;
        &lt;/set&gt;
  
        &lt;!--  Mapping Set&lt;Demande&gt; demandes --&gt;
        &lt;set name="demandes" cascade="save-update,delete" lazy="false"&gt;
           &lt;key column="group_id"/&gt;
           &lt;one-to-many class="modele.Demande"/&gt;
        &lt;/set&gt;
  
        &lt;!--  Mapping User proprietaire --&gt;
        &lt;many-to-one name="proprietaire" class="modele.User" column="user_id" not-null="true"/&gt;
  
        &lt;!--  Mapping ArrayList&lt;User&gt; invites --&gt;
        &lt;set name="invites" table="participation" lazy="false"&gt;
           &lt;key column="group_id"/&gt;
           &lt;many-to-many column="user_id" class="modele.User"/&gt;
        &lt;/set&gt;
  
     &lt;/class&gt;
  &lt;/hibernate-mapping&gt;
  </code></pre>
  
  <p><strong>UserDaoTest.java</strong></p>
  
  <pre><code>public class UserDaoTest {
  
      private final String tel = "0601020304";
      private final String pseudo = "pseudo";
      private final String pseudoModified = "pseudo2";
      private final String email = "email@email.com";
  
      @Test
      public void addUserTest(){
  
          User initialUser = new User(tel, pseudo, email);
  
          // Add user in database
          UserDao.addUser(initialUser);
  
          User addedUser = UserDao.getUser(tel);
  
          // Add Group in User
          Group group = new Group();
          group.setDescription("description");
          group.setHashtag("hashtag");
          group.setPassword("password");
          //Set group proprietaire
          group.setProprietaire(addedUser);               
  
          //Add group in database
          GroupDao.addGroup(group);
  
          // Add Group in User
          Set&lt;Group&gt; groups = new HashSet&lt;Group&gt;();
          groups.add(group);
          addedUser.setGroups(groups);
  
          Assert.assertEquals(addedUser.getTelephone(), initialUser.getTelephone());
          Assert.assertEquals(addedUser.getPseudo(), initialUser.getPseudo());
          Assert.assertEquals(addedUser.getEmail(), initialUser.getEmail());
      }
  
  
      @Test
      public void deleteUserTest(){
          User user = UserDao.getUser(tel);
  
          UserDao.deleteUser(user.getTelephone());
  
          List&lt;User&gt; listUsers = UserDao.listUser();
  
          Assert.assertFalse(listUsers.contains(user));
      }
  }
  </code></pre>