当前位置: 首页 > 知识库问答 >
问题:

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

夏振国
2023-03-14

在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

@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的情况下执行。

共有1个答案

穆建元
2023-03-14

您将@querynativequery标志设置为true,以便将查询按原样发送到服务器(在您的示例中为Mysql)。Mysql不允许在delete中使用别名。将nativequery设置为false或不使用别名。

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

 类似资料:
  • 问题内容: 我正在尝试从名为user_enrole的表中删除所有记录。 我认为查询的语法没有错,但这给了我错误提示 #1064-您的SQL语法有误;检查与您的MySQL服务器版本相对应的手册,以在第1行的’* FROM user_enrole’附近使用正确的语法 我已经仔细检查了我的语法,但我无法找出问题出在哪里,请有人指出。 是由于该表与使用表之间的关系而发生的还是什么? 问题答案: 您无需在删

  • > 在SWI-Prolog shell中,我执行以下命令:use_module(library(semweb/sparql_client))。加载sparql客户端库的 然后,在SWI-Prolog shell中,我执行以下SPARQL查询: ?-sparql_query('select count(*)where{?person a http://dbpedia.org/ontology/per

  • 问题内容: 我的脚本是这样的: 生成的sql是: 因此,执行sql时出现错误。 我有两个问题: 为什么用表的别名代替? 为什么in have子句不被替换,如何解决? 谢谢你的建议… 问题答案: 1:为什么表的别名是“ i”而不是“ p”? 2:为什么having子句中的“ num_a”没有被“ i__0”替换,如何解决? 这两个问题都可以简单回答:Doctrine使用它自己的别名进行查询。您不需要

  • 我遵循Rails指南:http://guides.rubyonrails.org/getting_started.html 我的索引中有下面一行。html。雇员再培训局: 但它不起作用;它只会进入展示页面。 供你参考,这是我的路线。rb: 我的控制器: 我的申请书。js: 我的申请书。html。雇员再培训局: 请注意,我已尝试更改

  • 问题内容: 该代码非常有用: 但是这段代码 不起作用 : 它引发此错误: ZMQError:没有这样的设备 为什么,zeromq无法使用localhost接口? 它只能在同一台计算机上的IPC上运行吗? 问题答案: 问题在于: 尝试更改为:

  • 问题内容: 在styles.css中,我正在使用媒体查询,这两个查询都使用以下变体: 当我缩小窗口时,这些网站的大小会调整为我希望在常规浏览器(Safari,Firefox)中使用的布局,但是,手机上的移动布局根本无法显示。相反,我只是看到默认的CSS。 谁能指出我正确的方向? 问题答案: 所有这三个都是有用的提示,但看来我需要添加一个元标记: 现在它似乎可以同时在Android(2.2)和iPh