我想删除用户是其所有者的所有组,但此时它不起作用。我认为在映射User.hbm.xml或Group.hbm.xml的层面上缺少某些东西,但我不知道。错误是“无法删除或更新父行:外键约束失败(sharedmap
。groupe
,CONSTRAINT FK_gq7win10rtxufsxu1n5istm2p
FOREIGN KEY(user_id
)REFERENCES {{ 1}}(user
))“
以下是有关的类和文件xml:
User.java
id
userDAO的
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;
}
...
}
User.hbm.xml
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();
}
}
}
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.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>
UserDaoTest.java
<?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>
答案 0 :(得分:2)
请尝试将inverse = true添加到用户文件集名称,并使用cascade = all“,delete-orphan
删除用户时删除组。另外请非常小心你如何在Hibernate中删除用户:
在删除用户之前必须刷新会话 链接到您的用户的所有组必须从所有活动会话和二级缓存中逐出。