自从我阻止使用JPA和HibernateProvider中的级联删除父实体和子实体以来,已经有几天了。我读了很多关于这个主题的书,但我读到的东西对我来说并不管用。
1/我的父UtilisateurEntity,使用cascade、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/ i业务bean使用spring和注释配置,基础架构bean (如数据源、事务、entitimanagerFactory、jpaVendorAdapter)使用xml配置。我使用JPA将持久化数据注入MYSQL数据库,并像jpa提供程序一样使用hibernate。
3/ i使用maven,我的应用程序分为几个maven子模块,一个用于服务(业务),一个用于dao,一个用于实体,一个用于批处理,一个用于基础设施(AOP,事务,...),一个用于webapp。每个子模式都有自己的弹簧配置。我使用Dto模式,意思是每个子模块管理自己的对象模型(实体用于DAO子模块,DAO用于服务子模块)。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),它是在Mysql DB中创建我的新UtilisateurEntity。该函数使用mapper将DTO转换为ENTITY,并将DAO持久化到DB中。
但是当我删除父实体时,像这样的UtilisateurEntity : this.utilisateurService.delete(dto);这个调用表明函数find Entity by login是正确调用的。实体已返回。之后,utilisateurDao( entity );的调用不会给出错误,而是实体的父实体和子实体,因此UtilisateurEntity和她的RoleEntity不会被删除到Mysql DB中。
我认为问题出在事务spring配置上,但我看不出有什么错误。
<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>请帮帮忙
发布于 2018-08-30 05:37:13
如果有帮助的话,问题是我在MySQL5 DataBase的Hibernate配置中使用了错误的方言。
hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialecthttps://stackoverflow.com/questions/51657666
复制相似问题