有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java无法通过jpa存储库删除记录

在Springboot中,我在存储库中创建了一个delete方法。我使用了同样的存储库方法来删除服务中的记录。记录不会被删除。它抛出异常。请告知

代码:

public interface SopCffDtlDependentRepository extends JpaRepository<SopCffDtlDependent,String>{

    String deleteByQuotationNo(String quotationNo);
    }


@Autowired
SopCffDtlDependentRepository sopCffDtlDependentRepository;


String noOfRecordsDeleted = sopCffDtlDependentRepository.deleteByQuotationNo(params.getQuotationNo());

                @Entity
                @Table(name = "SOP_CFF_DTL_DEPENDENT")
                public class SopCffDtlDependent {


                    @Column(name="NAME")
                    private String name;

                    @Column(name = "RELATIONSHIP")
                    private String relationShip;

                    @Column(name ="DOB")
                    private Date dob;

                    @Column(name="GENDER")
                    private String gender;

                    @Column(name="YEAR_OF_SUPPORT")
                    private Integer yearOfSupport;

                    @Id
                    @Column(name="QUOTATION_NO")    
                    private String quotationNo;

                    @Column(name="INVESTMENT_PREFER")   
                    private String investmentPrefer;

                    @Column(name="SEQUENCE_NO") 
                    private int sequenceNo;
}

例外情况:

 org.springframework.dao.InvalidDataAccessApiUsageException: No EntityManager with actual transaction available for current thread - cannot reliably process 'remove' call; nested exception is javax.persistence.TransactionRequiredException: No EntityManager with actual transaction available for current thread - cannot reliably process 'remove' call

共 (2) 个答案

  1. # 1 楼答案

    您应该根据条件添加要删除的修改注释和事务注释。通过查看您的日志,我不确定这是否是原因

    @Transactional
    @Modifying
    void deleteByQuotationNo(String quotationNo);
    
  2. # 2 楼答案

    使用@Service时,最好将@Transactional注释放在那里:

    public class SopCffDtlDependentServiceImpl implements SopCffDtlDependentService {
        @Autowired
        SopCffDtlDependentRepository repo;
        ...
        @Override
        @Transactional
        public String deleteByQuotationNo(String quotationNo) {
            repo.deleteByQuotationNo(quotationNo);