【问题标题】:exception while using nativeJPQL delete Query使用 nativeJPQL 删除查询时出现异常
【发布时间】:2021-04-04 00:04:50
【问题描述】:

我尝试使用 nativeQuery 删除,但我有这个异常:

org.springframework.dao.InvalidDataAccessApiUsageException: Executing an update/delete query; nested exception is javax.persistence.TransactionRequiredException: Executing an update/delete query
    at org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:403)
    ...................
Caused by: javax.persistence.TransactionRequiredException: Executing an update/delete query
    at org.hibernate.internal.AbstractSharedSessionContract.checkTransactionNeededForUpdateOperation(AbstractSharedSessionContract.java:413)
    at org.hibernate.query.internal.AbstractProducedQuery.executeUpdate(AbstractProducedQuery.java:1668)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
...............

我的员工实体:

@Entity
public class Employe implements Serializable{

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;
    
    private String prenom;
    
    private String nom;
    
    private String email;
    
    private int isActif;
    
    @Enumerated(EnumType.STRING)
    private Role role;

    @OneToOne(mappedBy ="employe", cascade = CascadeType.REMOVE)
    Contrat contrat;
    
    @ManyToMany( mappedBy="employes", fetch= FetchType.EAGER , cascade = {CascadeType.PERSIST })
    private List<Departement> departements;
    
    @ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE ,CascadeType.REMOVE})
    List<Mission> missions;
.........

我的部门实体:

@Entity
public class Departement implements Serializable{

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;
    
    private String name;
    
    @ManyToOne( cascade = {CascadeType.PERSIST, CascadeType.MERGE} )
    private Entreprise entreprise;
    
    @OneToMany( mappedBy="departement", cascade = {CascadeType.PERSIST, CascadeType.MERGE})
    private List<Mission> missions;
    
    @ManyToMany(fetch = FetchType.EAGER, cascade = {CascadeType.PERSIST, CascadeType.MERGE})
    private List<Employe> employes;
    

我的服务:

@Autowired IEmployeRepository empRep; …… @覆盖 公共无效 deleteEmployerById(int id) { 雇员 e = empRep.findById(id).orElse(null); // 删除该作者的所有关联 empRep.deleteEmployerDepartementsAssociations(id); empRep.delete(e); }

我的 EmployeRepository:

@Repository 公共接口 IEmployeRepository 扩展 CrudRepository{

 @Modifying
 @Query(value = "DELETE FROM departement_employes WHERE employes_id = :id ", nativeQuery = true)
 void deleteEmployerDepartementsAssociations(@Param("id") int id );
 

}

只有删除查询不起作用,我正在尝试删除与某个员工关联的所有部门。

员工与部门的关联是多对多双向关联。

我正在尝试在删除员工之前删除关联

【问题讨论】:

    标签: spring hibernate spring-data-jpa


    【解决方案1】:

    尝试以这种方式更正您的查询:

    @Modifying
    @Query(value = "DELETE FROM departement_employes WHERE employes_id = :id ", nativeQuery = true)
    void deleteEmployerDepartementsAssociations(@Param("id") int id );
    

    您必须将任何数据库修改操作包装在事务中。例如看这个article

    因此,作为一种可能的方法,您可以通过@Transactional 注释来注释您的服务方法:

    @Override
    @Transactional
    public void deleteEmployerById(int id) {
       // ...
    }
    

    【讨论】:

    • 我像你告诉我的那样编辑了,但我有另一个例外,我将编辑我的问题,例外刚刚改变
    • 一个新的概念,我将再次编辑我的问题并提供更多详细信息
    • 为什么你认为我有异常?员工和部门之间的关系是多对多双向的,我不确定级联或删除的服务,可能那里有问题?
    • 哇,非常感谢!它已经工作了,但是删除关联后服务中的员工,在数据库中不再删除它,我应该更改实体中的级联类型吗?
    • 这无济于事。当您使用本机查询进行删除时。顺便说一下@ManyToMany 关联,REMOVE 实体状态转换对于级联没有意义,因为它会传播到链接表之外。由于另一端可能被父端的其他实体引用,因此自动删除可能以ConstraintViolationException 结束。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多