Cannot delete or update a parent row: a foreign key constraint fails (hibernate xml mapping)

StackOverflow https://stackoverflow.com/questions/21852012

  •  13-10-2022
  •  | 
  •  

Question

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 "Cannot delete or update a parent row: a foreign key constraint fails (sharedmap.groupe, CONSTRAINT FK_gq7win10rtxufsxu1n5istm2p FOREIGN KEY (user_id) REFERENCES user (id))"

Here are the classes and the files xml concerned:

User.java

public class User {

    /** Attributs */ 


    @XmlTransient
    private Set<Group> proprietaire;

    /** Constructeur */ 
    public User() {
    }

    public User(String telephone, String pseudo, String email) {
        super();
        this.pseudo = pseudo;
        this.telephone = telephone;
        this.email = email;
    }


    public Set<Group> getProprietaire() {
        return proprietaire;
    }

    public void setProprietaire(Set<Group> proprietaire) {
        this.proprietaire = proprietaire;
    }

    ...

}

UserDao

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();
        }
    }
}

User.hbm.xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
 "-//Hibernate/Hibernate Mapping DTD//EN"
 "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 

<hibernate-mapping>
   <class name="modele.User" table="user">
      <meta attribute="class-description">
         This class contains the user detail. 
      </meta>
      <id name="id" type="int" column="id">
         <generator class="native"/>
      </id>
      <property name="pseudo" column="pseudo" type="string"/>
      <property name="telephone" column="telephone" type="string" not-null="true" unique="true"/>
      <property name="email" column="email" type="string"/>

      <!--  Mapping Set<Demande> demandes -->
      <set name="demandes" cascade="save-update,delete" lazy="false">
         <key column="demandeur_id"/>        
         <one-to-many class="modele.Demande"/>
      </set>

      <!--  Mapping Set<Invitation> aInvite -->
      <set name="aInvite" cascade="save-update,delete" lazy="false">
         <key column="inviteur_id"/>
         <one-to-many class="modele.Invitation"/>
      </set>

      <set name="notifications" cascade="save-update,delete" lazy="false">
         <key column="user_id"/>
         <one-to-many class="modele.Notification"/>
      </set>


      <set name="groups"  table="participation" lazy="false" inverse="true">
         <key column="user_id"/>
         <many-to-many column="group_id" class="modele.Group"/>
      </set>

     <!--  Mapping Set<Group> proprietaire -->
      <set name="proprietaire" cascade="save-update,delete" lazy="false">
         <key column="user_id"/>
         <one-to-many class="modele.Group"/>
      </set>      

   </class>
</hibernate-mapping>

Group.hbm.xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
 "-//Hibernate/Hibernate Mapping DTD//EN"
 "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 

<hibernate-mapping>
   <class name="modele.Group" table="groupe">
      <meta attribute="class-description">
         This class contains the publicEvent detail. 
      </meta>
      <id name="id" type="int" column="id">
         <generator class="native"/>
      </id>
      <property name="description" column="description" type="string"/>
      <property name="hashtag" column="hashtag" type="string"/>
      <property name="password" column="password" type="string"/>
      <!--  Mapping ArrayList<Marqueur> marqueurs -->

      <set name="marqueurs" cascade="save-update,delete" lazy="false">
         <key column="group_id"/>
         <one-to-many class="modele.Marqueur"/>
      </set>
      <!--  Mapping Set<Invitation> invitations -->
      <set name="invitations" cascade="save-update,delete" lazy="false">
         <key column="group_id"/>
         <one-to-many class="modele.Invitation"/>
      </set>

      <!--  Mapping Set<Demande> demandes -->
      <set name="demandes" cascade="save-update,delete" lazy="false">
         <key column="group_id"/>
         <one-to-many class="modele.Demande"/>
      </set>

      <!--  Mapping User proprietaire -->
      <many-to-one name="proprietaire" class="modele.User" column="user_id" not-null="true"/>

      <!--  Mapping ArrayList<User> invites -->
      <set name="invites" table="participation" lazy="false">
         <key column="group_id"/>
         <many-to-many column="user_id" class="modele.User"/>
      </set>

   </class>
</hibernate-mapping>

UserDaoTest.java

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<Group> groups = new HashSet<Group>();
        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<User> listUsers = UserDao.listUser();

        Assert.assertFalse(listUsers.contains(user));
    }
}
Was it helpful?

Solution

Please try adding the inverse=true to User file set name proprietary also with cascade=all",delete-orphan

which deletes the Groups when the User is deleted. Also please be extremely careful of how you delete User in Hibernate:

Session must be flushed prior to deleting User.
All Groups linked to your User must be evicted from all active sessions and 2nd level cache.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top