It's been a couple of days since i blocked on the removal of a parent and child entity using cascade from JPA and HibernateProvider. I read a lot of about this subject but what i read does not work on my side.
1/ My parent UtilisateurEntity, using cascade, lazy and orphanRemoval, and the Child RoleEntity, using
@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 use spring and annotation configuration for the businnes bean and xml configuration for infrastructure bean like (datasource, transaction, entitimanagerFactory, jpaVendorAdapter). I use JPA pour peristence data into MYSQL database and hibernate like the jpa provider.
3/ I use maven and my application is divided into several maven sub-module, one for service (business), one for dao, one for entity, one for batch, one for infrastructure (AOP, transaction, ...), one for webapp. Each sub-mode has its own spring configuration. I use the Dto pattern, mean each sub-module manages her own object modele (entity for DAO sub-module, dto for Service sub-module). Selma mapper build conversion between both objects.
3/Service utilisateur use dao for create and delete utilisateur and own roles.
@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);
}
....
}
PROBLEMS: When i create or update UtilisateurEntity like this : this.utilisateurService.create(dto) it's create my new UtilisateurEntity into Mysql DB. The function use mapper to convert from DTO to ENTITY and DAO persist into DB without problems.
But when i remove the parent Entity, so the UtilisateurEntity like this: this.utilisateurService.delete(dto); this call show that the function find entity by login it is call correctly. The entity has been returned. After that, the call of utilisateurDao(entity); does not give the error, but the entity Parent and Child, so UtilisateurEntity and her RoleEntity are not deleted into Mysql DB.
I thought the problem was the transaction spring configuration, but i do not see the mistake.
<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>
Please Help