Hibernate不会删除Parent oneToMany relationShip



自从我阻止从JPA和Hibernate Provider使用级联删除父实体和子实体以来,已经有几天了。我读了很多关于这个主题的书,但我读到的对我来说并不管用。

1/我的父UtilisateurEntity,使用级联、懒惰和孤儿移除,以及子角色实体,使用

@Entity(name = "UtilisateurEntity")
@Table(name = "utilisateur",
uniqueConstraints = {
@UniqueConstraint(name = "login", columnNames = "login")})
@XmlRootElement
public class UtilisateurEntity implements Serializable {
// skipped attributes for the post
**@OneToMany( mappedBy = "utilisateur", cascade = CascadeType.ALL, fetch = FetchType.LAZY, orphanRemoval = true)**
private List<RoleEntity> roles = new ArrayList();
//skip Constructor and getters/setters
public void addRole(RoleEntity roleEntity){
roleEntity.setUtilisateurEntity(this);
this.roles.add(roleEntity);
}
public void deleteRole(RoleEntity roleEntity){
this.roles.remove(roleEntity);
roleEntity.setUtilisateurEntity(null);
}
//skip equals and hashCode
}
@Entity(name = "RoleEntity")
@Table(name = "role",
uniqueConstraints = {
@UniqueConstraint(name = "role_utilisateurId", columnNames = {"role", "utilisateurId"})
})
@XmlRootElement
public class RoleEntity implements Serializable {
// skipped attributes for the post
@ManyToOne
@JoinColumn(name = "utilisateurId", nullable = false)
private UtilisateurEntity utilisateur;
//Skip Constructors, getters setters, equas and hashCode
}

2/I使用spring和annotation配置作为业务bean,使用xml配置作为基础设施bean(datasource、transaction、entitimanagerFactory、jpaVendorAdapter(。我使用JPA将死亡数据倒入MYSQL数据库,并像JPA提供者一样休眠。

3/I使用maven,我的应用程序分为几个maven子模块,一个用于服务(业务(,一个针对dao,一个面向实体,一个支持批处理,一个负责基础设施(AOP,事务,…(,另一个用于webapp。每个子模式都有自己的弹簧配置。我使用Dto模式,意味着每个子模块都管理自己的对象建模器(DAO子模块的实体,服务子模块的Dto(。塞尔玛映射器在两个对象之间构建转换。

3/服务使用者使用dao创建和删除使用者和自己的角色。

@Service("utilisateurService")
public class UtilisateurServiceImpl implements IUtilisateurService{
@Autowired
private IUtilisateurDao utilisateurDao;
@Autowired
private IUtilisateurMapper utilisateurMapper;
@Autowired
private PasswordEncoder passwordEncoder;
//skipped
@Override
public void delete(String login){
UtilisateurEntity entity;
entity = this.utilisateurDao.findByLogin(login);
this.utilisateurDao.delete(entity);
}
}
@Repository("utilisateurDao")
public class UtilisateurDaoImpl implements IUtilisateurDao
{
....
@Override
public void delete(T entity)
{
this.entityManager.remove(entity);
}
....
}

问题:当我像这样创建或更新UtilisateurEntity时:this.utilisateurService.create(dto(它是在MysqlDB中创建我的新UtilisateurEntity。该函数使用mapper将DTO转换为ENTITY,DAO可以毫无问题地保存到DB中。

但当我删除父实体时,UtilisateurEntity如下所示:this.utilisateurService.delete(dto(;这个调用表明函数通过登录来查找实体它是正确调用的。实体已返回。之后使用urDao(实体(;没有给出错误,但实体Parent和Child,因此UtilisateurEntity和她的RoleEntity不会被删除到Mysql DB中。

我认为问题出在事务弹簧配置上,但我没有看到错误。

<tx:advice id="serviceTxAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="find*" read-only="true"/>
<tx:method name="*" rollback-for="java.lang.Exception"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="serviceTxPointCut"
expression="execution(* com.hsmr.genealogie..*ServiceImpl.*(..))"/>
<aop:advisor advice-ref="serviceTxAdvice" pointcut-ref="serviceTxPointCut"/>
</aop:config>

请帮助

如果有帮助的话,问题是我在MYSQL 5数据库的Hibernate配置中发现了错误的方言。

hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect

最新更新