Spring JPA Hibernate JpaRepository级联oneToMany删除不起作用

时间:2018-11-01 05:30:42

标签: spring hibernate jpa spring-data-jpa spring-data

Spring Hibernate Jpa存储库。

我无法级联删除,也没有任何错误。

我有以下实体,并添加了以下关系。

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;

这是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;

我的存储库也如下所示。

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

我正在对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);
        }
    }

即使我也尝试过Hibernate包@cascade(CascadeType.ALL),但是没有运气。 我也尝试过@OnDelete(action = OnDeleteAction.CASCADE)仍然没有删除并且没有引发任何错误。

请让我知道我是否在这里遗漏了什么?

0 个答案:

没有答案