在JPA Spring数据中使用DELETE别名是否有任何已知问题?同样的工作就像删除别名的魅力。 我有一个JPA Spring数据本机查询如下: -
@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 Spring数据中使用DELETE别名是否存在任何已知问题?同样的工作就像删除别名的魅力一样。
参考完成 该文档还显示了使用别名http://docs.spring.io/spring-data/jpa/docs/1.4.2.RELEASE/reference/html/jpa.repositories.html
来自文档: - 2.5.1交易查询方法
要允许您的查询方法是事务性的,只需在您定义的存储库接口使用@Transactional。
例2.20。在查询方法中使用@Transactional
@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();
}
通常,您需要将readOnly标志设置为true,因为大多数查询方法只会读取数据。与之相反,deleteInactiveUsers()使用@Modifying注释并覆盖事务配置。因此,该方法将在readOnly标志设置为false的情况下执行。
答案 0 :(得分:4)
您将nativeQuery
的{{1}}标志设置为true,因此查询将按原样发送到服务器(在您的情况下为Mysql)。 Mysql不允许@Query
中的别名。将DELETE
设置为false或不使用别名。
*当然,还有一个选项可以使用,例如,SQLServer接受这样的语法而不是Mysql :)
答案 1 :(得分:0)
执行以下查询时出错的原因是因为它不是删除查询的正确MySql语法。您使用的是nativeQuery,别名用法不正确。
正确的语法是:
longitude