Hibernate不会删除Parent oneToMany relationshipShip

时间:2018-08-02 15:49:07

标签: spring hibernate jpa

距离我阻止使用JPA和HibernateProvider的级联移除父级和子级实体已有几天了。我读了很多关于这个主题的书,但是我读的书对我而言并不起作用。

1 /我的父级UtilisateurEntity使用级联,lazy和orphanRemoval,子级RoleEntity使用

@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 /我将spring和注解配置用于businnes bean,将xml配置用于基础设施bean,例如(数据源,事务,entitimanagerFactory,jpaVendorAdapter)。 我使用JPA将持久性数据倒入MYSQL数据库并像jpa提供程序一样休眠。

3 /我使用maven,我的应用程序分为几个maven子模块,一个用于服务(业务),一个用于dao,一个用于实体,一个用于批处理,一个用于基础架构(AOP,事务,...)。 ),一个用于webapp。每个子模式都有自己的弹簧配置。 我使用Dto模式,这意味着每个子模块管理自己的对象模型(DAO子模块的实体,Service子模块的dto)。 Selma映射器在两个对象之间建立转换。

3 / Service utilisateur使用dao创建和删除utilisateur和自己的角色。

@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) 它将我的新UtilisateurEntity创建到Mysql DB中。该功能使用映射器将DTO转换为ENTITY,而DAO可以持久地存储到数据库中。

但是当我删除父实体时,所以UtilisateurEntity像这样:     this.utilisateurService.delete(dto); 此调用表明通过登录功能找到实体的方法正确。该实体已返回。 在那之后,     utilisateurDao(entity); 不会给出错误,但是会给出实体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>

请帮助

1 个答案:

答案 0 :(得分:0)

如果有帮助,问题是我在用于MySQL 5数据库的Hibernate配置中声明了错误的方言。

hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect
相关问题