gpt4 book ai didi

java - 为什么使用别名的 native 删除查询在 Spring Data JPA 中不起作用?

转载 作者:行者123 更新时间:2023-11-29 03:07:58 24 4
gpt4 key购买 nike

在 JPA Spring 数据中使用 DELETE 别名是否存在任何已知问题?同样的工作就像删除别名的魅力一样。我有一个 JPA Spring 数据 native 查询,如下所示:-

    @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 事务查询方式

要允许您的查询方法是事务性的,只需在您定义的存储库接口(interface)上使用@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 的情况下执行。

最佳答案

您将 @QuerynativeQuery 标志设置为 true,因此查询按原样发送到服务器(在您的例子中是 Mysql)。 Mysql 不允许在 DELETE 中使用别名。将 nativeQuery 设置为 false 或不使用别名。

*当然,还有一个选项可以使用,例如,SQLServer 可以接受这种语法而不是 Mysql :)

关于java - 为什么使用别名的 native 删除查询在 Spring Data JPA 中不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31094934/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com