Hibernate JPQL抛出无法输入更新/删除查询

时间:2015-05-03 21:40:29

标签: java spring hibernate jpa 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();

我得到的堆栈跟踪:

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;

    ...

我尝试将删除方法更改为这样,以便我根本不使用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执行删除查询,因此必须有可能,我在这里缺少什么?

1 个答案:

答案 0 :(得分:4)

将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查询未输入,因为它们只返回受影响的行,因此它们不需要返回类型。