编程语言
首页 > 编程语言> > java-休眠的JPQL引发无法输入更新/删除查询

java-休眠的JPQL引发无法输入更新/删除查询

作者:互联网

我正在尝试使用HQL删除实体,但失败了.

    TypedQuery<Seller> query = Seller.entityManager().createQuery(
        "DELETE FROM Seller AS o WHERE o.company=:company AND o.id=:id", Seller.class);
    query.setParameter("company", company);
    query.setParameter("id", id);
    int result = query.executeUpdate();

我得到的stacktrace:

Update/delete queries cannot be typed; nested exception is java.lang.IllegalArgumentException: Update/delete queries cannot be typed
    at org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:296)
    at org.springframework.orm.jpa.aspectj.JpaExceptionTranslatorAspect.ajc$afterThrowing$org_springframework_orm_jpa_aspectj_JpaExceptionTranslatorAspect$1$18a1ac9(JpaExceptionTranslatorAspect.aj:33)
    at com.ahp.core.model.Seller.deleteSeller_aroundBody4(Seller.java:111)
    at com.ahp.core.model.Seller.deleteSeller(Seller.java:1)
    at com.ahp.core.processor.SellerProcessor.delete(SellerProcessor.java:175)
    at com.ahp.core.processor.SellerProcessor.consume(SellerProcessor.java:80)
    at com.ahp.core.processor.SellerProcessor.consume(SellerProcessor.java:1)
    at com.ahp.messaging.processor.AbstractRPCConsumer.onMessage(AbstractRPCConsumer.java:32)
    at org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter.onMessage(MessageListenerAdapter.java:228)
    at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.doInvokeListener(AbstractMessageListenerContainer.java:756)
    at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.invokeListener(AbstractMessageListenerContainer.java:679)
    at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.access$001(SimpleMessageListenerContainer.java:82)
    at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer$1.invokeListener(SimpleMessageListenerContainer.java:167)
    at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.invokeListener(SimpleMessageListenerContainer.java:1241)
    at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.executeListener(AbstractMessageListenerContainer.java:660)
    at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.doReceiveAndExecute(SimpleMessageListenerContainer.java:1005)
    at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.receiveAndExecute(SimpleMessageListenerContainer.java:989)
    at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.access$700(SimpleMessageListenerContainer.java:82)
    at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer$AsyncMessageProcessingConsumer.run(SimpleMessageListenerContainer.java:1103)
    at java.lang.Thread.run(Thread.java:744)
Caused by: java.lang.IllegalArgumentException: Update/delete queries cannot be typed
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.resultClassChecking(AbstractEntityManagerImpl.java:363)
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:344)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:366)
    at com.sun.proxy.$Proxy57.createQuery(Unknown Source)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:241)
    at com.sun.proxy.$Proxy56.createQuery(Unknown Source)
    ... 18 more

Seller.java是由Spring Roo生成的:

@RooJavaBean
@RooToString
@RooJpaActiveRecord
public class Seller {
...

Seller_Roo_Jpa_Entity.aj:

privileged aspect Seller_Roo_Jpa_Entity {

    declare @type: Seller: @Entity;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    private Long Seller.id;

    ...

Seller_Roo_Jpa_ActiveRecord.aj:

privileged aspect Seller_Roo_Jpa_ActiveRecord {

    @PersistenceContext
    transient EntityManager Seller.entityManager;

    ...

我尝试将delete方法更改为如下所示,以使我完全不使用TypedQuery:

import javax.transaction.Transactional;

...

@Transactional
public static Boolean deleteSeller(Company company,  Long id){
    Query query = Seller.entityManager().createQuery(
            "DELETE FROM Seller AS o WHERE o.company=:company AND o.id=:id");
    query.setParameter("company", company);
    query.setParameter("id", id);
    int result = query.executeUpdate();
    return result > 0;
}

…但这给了我另一个例外:

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:316)
    at org.springframework.orm.jpa.aspectj.JpaExceptionTranslatorAspect.ajc$afterThrowing$org_springframework_orm_jpa_aspectj_JpaExceptionTranslatorAspect$1$18a1ac9(JpaExceptionTranslatorAspect.aj:33)
    ...
Caused by: javax.persistence.TransactionRequiredException: Executing an update/delete query
    at org.hibernate.jpa.spi.AbstractQueryImpl.executeUpdate(AbstractQueryImpl.java:71)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.springframework.orm.jpa.SharedEntityManagerCreator$DeferredQueryInvocationHandler.invoke(SharedEntityManagerCreator.java:311)
    at com.sun.proxy.$Proxy58.executeUpdate(Unknown Source)
    ... 18 more

我的方法带有@Transactional注释,所以我看不到它不在事务内部.

这个question和这个question似乎正在使用HQL进行删除查询,因此它一定有可能,我在这里错过了什么?

解决方法:

将您的DELETE查询更改为:

Query query = Seller.entityManager().createQuery(
    "DELETE FROM Seller AS o WHERE o.company=:company AND o.id=:id");
query.setParameter("company", company);
query.setParameter("id", id);
int result = query.executeUpdate();

不键入DML JPQL查询,因为它们仅返回受影响的行,因此不需要返回类型.

标签:jpa,hibernate,jpql,spring,java
来源: https://codeday.me/bug/20191028/1952006.html