JPA 命名查询 |删除 |无法输入更新/删除查询:

问题描述

我的代码如下: 实体

@Entity(name = "TerpCrtV")
@NamedQueries({
     @NamedQuery(name = "deleteallCrt",query = "DELETE FROM TerpCrtV")
})

命名查询在 DAO 层如下调用

@PersistenceContext(unitName="masterPU")
private EntityManager entityManagerMaster;

public int deleteallCustomerTeams() throws Exception {
    return entityManagerMaster.createNamedQuery("deleteallCrt",TerpCrtV.class).executeUpdate();
}

使用 usertransaction 调用如下: 用户事务是 EJB 之一并注入到 bean 中

private void persist(List<TerpCrtV> terpCustomerTeams) throws Exception  {
    logger.info("In CustomerTeamscopierImpl: Delete all CRT");
    userTransaction.begin();
    deleteallCrtV();
    userTransaction.commit();

我面临的错误如下:

2021-04-05 12:36:53,454 ERROR [org.jboss.as.ejb3.invocation] (EJB default - 2) WFLYEJB0034: EJB Invocation Failed on component MasterdataPersistence for method public int com.evry.integrator.crm.persistence.MasterdataPersistence.deleteallCustomerTeams() throws java.lang.Exception: javax.ejb.EJBTransactionRolledbackException: Update/delete queries cannot be typed
        at org.jboss.as.ejb3.tx.CMTTxInterceptor.invokeInCallerTx(CMTTxInterceptor.java:203)
        at org.jboss.as.ejb3.tx.CMTTxInterceptor.required(CMTTxInterceptor.java:364)
       ]com.evry.integrator.crm.persistence.MasterdataPersistence$$$view7.deleteallCustomerTeams(UnkNown Source)
        at com.evry.integrator.terpcrm.schedular.CustomerTeamscopierImpl.deleteallCrtV(CustomerTeamscopierImpl.java:86)
        at com.evry.integrator.terpcrm.schedular.CustomerTeamscopierImpl.persist(CustomerTeamscopierImpl.java:64)
        at com.evry.integrator.terpcrm.schedular.CustomerTeamscopierImpl.importDataFromTerpAndInsertToMasterdata(CustomerTeamscopierImpl.java:139)

解决方法

它告诉您调用 createNamedQuery 时不能有第二个参数(因为您没有返回任何结果)。

这应该有效:

entityManagerMaster.createNamedQuery("deleteAllCrt").executeUpdate();

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...