title: Hibernate JPQL throws Update/delete queries cannot be typed
body: <p>I'm trying to delete an entity using HQL and it's failing.</p>
  
  <pre><code>    TypedQuery&lt;Seller&gt; 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();
  </code></pre>
  
  <p>The stacktrace I'm getting:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>Seller.java was generated by Spring Roo:</p>
  
  <pre><code>@RooJavaBean
  @RooToString
  @RooJpaActiveRecord
  public class Seller {
  ...
  </code></pre>
  
  <p>Seller_Roo_Jpa_Entity.aj:</p>
  
  <pre><code>privileged aspect Seller_Roo_Jpa_Entity {
  
      declare @type: Seller: @Entity;
  
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      @Column(name = "id")
      private Long Seller.id;
  
      ...
  </code></pre>
  
  <p>Seller_Roo_Jpa_ActiveRecord.aj:</p>
  
  <pre><code>privileged aspect Seller_Roo_Jpa_ActiveRecord {
  
      @PersistenceContext
      transient EntityManager Seller.entityManager;
  
      ...
  </code></pre>
  
  <p>I've tried changing the delete method to look like this so that I don't use TypedQuery at all:</p>
  
  <pre><code>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 &gt; 0;
  }
  </code></pre>
  
  <p>... but this is giving me another exception:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>My method is annotated with <code>@Transactional</code>, so I don't see how it is not inside a transaction.</p>
  
  <p>This <a href="https://stackoverflow.com/questions/6019654/hibernate-delete-using-hql">question</a> and this <a href="https://stackoverflow.com/questions/22101895/hibernate-hql-delete-query">question</a> seems to be using HQL to do a delete query, so it must be possible, what am I missing here?</p>