2015-06-28 26 views
1

在JPA Spring數據中有DELETE的別名有什麼已知問題嗎?同樣的作品就像在刪除別名時的魅力一樣。 我有如下一個JPA春數據本地查詢: -爲什麼使用別名的本地刪除查詢在Spring Data JPA中不起作用?

@Modifying 
    @Transactional 
    @Query(value = "delete from Customer d where d.name = ?1", nativeQuery = true) 
    public void removeOnName(String name); 

這下面拋出一個異常: -

01:11:13.616 [main] WARN o.h.e.jdbc.spi.SqlExceptionHelper - SQL Error: 1064, SQLState: 42000 
01:11:13.616 [main] ERROR o.h.e.jdbc.spi.SqlExceptionHelper - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'd where d.name = 'kiran'' at line 1 
01:11:13.621 [main] WARN o.h.e.jdbc.spi.SqlExceptionHelper - SQL Warning Code: 1064, SQLState: 42000 
01:11:13.621 [main] WARN o.h.e.jdbc.spi.SqlExceptionHelper - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'd where d.name = 'kiran'' at line 1 
org.springframework.dao.InvalidDataAccessResourceUsageException: could not execute statement; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not execute statement 
    at org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:172) 
    at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:155) 
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:417) 
    at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:59) 
    at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:213) 
    at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:147) 
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) 
    at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodIntercceptor.invoke(CrudMethodMetadataPostProcessor.java:122) 
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) 
    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92) 
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) 
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207) 
    at com.sun.proxy.$Proxy91.removeDevciesOnGuid(Unknown Source) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:483) 

查詢執行很好,當我刪除的別名。 如下工作查詢: -

@Modifying 
     @Transactional 
     @Query(value = "delete from Customer where name = ?1", nativeQuery = true) 
     public void removeOnName(String name); 

是否有具有與JPA春數據刪除別名任何已知問題?同樣的作品就像在刪除別名時的魅力一樣。

查詢進行 該文檔還展示了使用別名http://docs.spring.io/spring-data/jpa/docs/1.4.2.RELEASE/reference/html/jpa.repositories.html

從技術文檔: - 2.5.1事務查詢方法

爲了讓你的查詢方法是事務性簡單地使用@Transactional在您定義的儲存庫界面。

例2.20。在查詢方法

@Transactional(readOnly = true) 
public interface UserRepository extends JpaRepository<User, Long> { 

    List<User> findByLastname(String lastname); 

     @Modifying 
    @Transactional 
    @Query("delete from User u where u.active = false") 
    void deleteInactiveUsers(); 
} 

使用@Transactional通常你會希望只讀標誌設置爲true,因爲大多數的查詢方法將只讀取數據。與deleteInactiveUsers()相比,它使用@Modifying註釋並重寫事務配置。因此,該方法將在readOnly標誌設置爲false的情況下執行。

回答

4

您將nativeQuery標誌@Query設置爲true,所以查詢發送到服務器(您的情況下爲Mysql)。 Mysql不允許在DELETE中使用別名。將nativeQuery設置爲false或不使用別名。

*當然,也有一個選項可以使用,例如,SQLServer的該會接受這樣的語法,而不是Mysql的:)

0

原因,你得到一個錯誤,在執行下面的查詢,因爲它不是一個正確的MySQL刪除查詢的語法。您正在使用nativeQuery,並且別名用法不正確。

正確的語法是:

"delete d from Customer as d where d.name=?1" 

@Modifying 
@Transactional 
@Query(value = "delete d from Customer as d where d.name=?1", nativeQuery = true) 
public void removeOnName(String name); 
+0

MySQL不使用 「TSQL」。您似乎在考慮SQLServer或Sybase ... –