1

Spring Hibernate Jpa repository.

I am unable to cascade delete also I am not getting any error also.

I have following entities and have added relationships as below.

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;

import org.hibernate.annotations.OnDelete;
import org.hibernate.annotations.OnDeleteAction;

import com.abc.enumerator.ServiceSpecializationEnum;
import com.fasterxml.jackson.annotation.JsonIgnore;

@Entity
public class SpecializationServices extends BaseEntity{

    /**
     * 
     */
    private static final long serialVersionUID = -6438039604817328747L;

    private String name;

    @Enumerated(EnumType.STRING)
    private ServiceSpecializationEnum type;

    //@JsonIgnore
    @OneToMany(mappedBy = "specialization", fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true)
    private List<UserSpecializationServices> userSpecializations;

Here is the UserSpecializationServices

import javax.persistence.Entity;
import javax.persistence.ManyToOne;

@Entity
public class UserSpecializationServices extends BaseEntity{

    /**
     * 
     */
    private static final long serialVersionUID = 7884804824368079879L;

    @ManyToOne
    private UserIdentity userIdentity;

    @ManyToOne
    private SpecializationServices specialization;

Also my repository looks as below.

@Repository
public interface SpecializationServicesRepository extends JpaRepository<SpecializationServices, Long>{
}

I am performing the following delete action on serviceImpl

    @Transactional
    @Service
    public class SpecializationServiceImpl {        
        @Autowired
        private SpecializationServicesRepository specializationServiceRepository;

        @Override
        public void deleteServices(Long id) {
SpecializationServices specializationService = specializationServiceRepository.findById(id).get();
specializationServiceRepository.delete(specializationService);
        }
    }

Even I have tried Hibernate package @cascade(CascadeType.ALL) too but no luck. I have tried @OnDelete(action = OnDeleteAction.CASCADE) too still it is not deleting and not throwing any errors.

Please let me know if I am missing anything here?

Jaini Naveen
  • 155
  • 1
  • 17
  • look at this https://stackoverflow.com/questions/2011519/jpa-onetomany-not-deleting-child – Nonika Nov 01 '18 at 08:43
  • When you delete the child from collection does its foreign key for SpecializationServices became null into your UserSpecializationServices table? – Nonika Nov 01 '18 at 08:47
  • Nonika Thanks for commenting on my questions. I read the suggested stackoverflow post but I have used orphanRemoval=true, also I tried as hibernate suggested @Cascde annotation too as per the hibernate specification. In my case Parent and Child both are not getting deleted and I am not seeing any error or warning in the console too. – Jaini Naveen Nov 01 '18 at 10:06
  • I have created a project to mimic your situation but can't produce such kind of behavior can you specify what version of spring-boot are you using? and also can you add your UserIdentity and BaseEntity. Or better can you create a test project reproducing the problem and share it? – Nonika Nov 01 '18 at 11:12
  • on .delete(specializationService) everithing is being deleted even without orphanRemoval=true with just only CascadeType.ALL – Nonika Nov 01 '18 at 11:13
  • Thanks alot for trying to figure the issue. – Jaini Naveen Nov 01 '18 at 14:20
  • Thanks alot for trying to figure the issue. Here is the configuration spring-boot 2.0.4 and data related dependencies are spring-boot-starter-data-rest,spring-boot-starter-jdbc. After alot of debug I came to know the following behavior 1. If I use CascadeType.ALL it is not working, if I use cascadeType.REMOVE it is removing only associated child records not the parent. So I dont have any clue what is going on here. I will try to replicate the same in a sample project and will share you on github – Jaini Naveen Nov 01 '18 at 14:33
  • Also we are using Postgresql as our db. – Jaini Naveen Nov 01 '18 at 15:11
  • I've tested it with oracle and it works as expected I have not Postgre. I will install it and try again – Nonika Nov 01 '18 at 15:13
  • You have a very complex setup with inheritance and a child that is a child of multiple entities. You need to back off this complexity until you figure out where the issue lies. Is it because of the inheritance or is it because of multiple parents, or what? Please provide a [MCVE](https://stackoverflow.com/help/mcve) – K.Nicholas Nov 01 '18 at 15:14
  • If a entity is a child of multiple parents why would you expect it be deleted by one when it is still a child of another? Don't you think that would cause a constraint violation exception? Why don't you try removing the child and desired parents specifically? – K.Nicholas Nov 01 '18 at 15:17
  • The entity is child of only one entity. Please let me know if something made you to misunderstand you. – Jaini Naveen Nov 01 '18 at 15:48
  • We have implemented simple inheritance here to add common fields like id, created date, createdBy, updatedDate, updatedBy, version etc... since all these fields are common across all the tables. Does this makes any issue for cascade operations? – Jaini Naveen Nov 01 '18 at 15:51

0 Answers0