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

不支持DML操作。无法使用spring数据更新postgresql数据库中的数据

陶星辰
2023-03-14

嗨,我使用的是Spring启动和Spring数据,我想从数据库中获取数据的基础上的id,但我不能检索它。我得到这个错误"异常":

"org.springframework.dao.InvalidDataAccessApiUsageExctive","消息":"org.hibernate.hql.internal.QueryExcutionRequest estExctive:不支持DML操作[Updatecom.ge.health.poc.model.SpringModel SET name='斯涅哈',其中id=?];嵌套异常是java.lang.IllegalStateExctive:org.hibernate.hql.internal.QueryExcutionRequest estExctive:不支持DML操作[Updatecom.ge.health.poc.model.SpringModel SET name='斯涅哈',其中id=?]", "路径":"/updatedata"}

主课

  package com.ge.health.poc;

  import org.springframework.boot.SpringApplication;
  import org.springframework.boot.autoconfigure.SpringBootApplication;

  @SpringBootApplication
  public class SpringDataApplication {

     public static void main(String[] args) {
         SpringApplication.run(SpringDataApplication.class, args);
     }
  }

控制器类

    package com.ge.health.poc.controller;

    import java.io.IOException;
    import java.text.ParseException;

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestBody;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.ResponseBody;
    import org.springframework.web.bind.annotation.RestController;
    import org.springframework.web.servlet.mvc.support.RedirectAttributes;

    import com.fasterxml.jackson.core.JsonParseException;
    import com.fasterxml.jackson.databind.JsonMappingException;
    import com.fasterxml.jackson.databind.ObjectMapper;
    import com.ge.health.poc.model.SpringModel;
    import com.ge.health.poc.service.BookServiceImpl;

    @RestController
    public class SpringController {

        @Autowired
        BookServiceImpl bookserviceimpl;

        @RequestMapping(value = "/insertdata", method = RequestMethod.POST)
        @ResponseBody
        public void helloService(@RequestBody String input, final RedirectAttributes redirectAttributes)
                throws JsonParseException, JsonMappingException, IOException, ParseException {

            System.out.println(input);
            ObjectMapper mapper = new ObjectMapper();
            SpringModel pojodata = mapper.readValue(input, SpringModel.class);
            System.out.println(pojodata);
            System.out.println(pojodata.getAuthor());
            bookserviceimpl.save(pojodata);
        }

        @RequestMapping(value = "/getdata/{id}")
        @ResponseBody
        public void retreiveData(@PathVariable("id") int id)
                throws JsonParseException, JsonMappingException, IOException, ParseException {

            System.out.println("id is:" + id);
            bookserviceimpl.retreive(id);

        }

        @RequestMapping(value = "/deletedata", method = RequestMethod.DELETE)
        @ResponseBody
        public void deleteData(@RequestBody String id)
                throws JsonParseException, JsonMappingException, IOException, ParseException {

            System.out.println("M in delete");
            System.out.println(id);

            ObjectMapper mapper = new ObjectMapper();
            SpringModel pojodata = mapper.readValue(id, SpringModel.class);
            int idd = (pojodata.getId());
            System.out.println("value oof idd is:" + idd);
            System.out.println("M into delete method");

            bookserviceimpl.delete(idd);

        }

        @RequestMapping(value = "/updatedata", method = RequestMethod.PUT)
        @ResponseBody
        public void updateData(@RequestBody String id)
                throws JsonParseException, JsonMappingException, IOException, ParseException {

            System.out.println("M in update");
            System.out.println(id);

            ObjectMapper mapper = new ObjectMapper();
            SpringModel pojodata = mapper.readValue(id, SpringModel.class);
            int idd = (pojodata.getId());
            System.out.println("value oof idd is:" + idd);
            bookserviceimpl.update(idd);
        }

    }

存储库

    package com.ge.health.poc.interfac;

    import org.springframework.data.jpa.repository.JpaRepository;
    import org.springframework.data.jpa.repository.Query;
    import org.springframework.stereotype.Repository;
    import org.springframework.transaction.annotation.Transactional;

    import com.ge.health.poc.model.SpringModel;

    @Repository
    @Transactional
    public interface BookRepository extends JpaRepository<SpringModel, Long> {

        @Query("select author from SpringModel where id=?")
        String findName(int id);

        @Query("Update SpringModel SET name='sneha' where id=?")
        String UpdateByID(int id);

        @Query("delete from SpringModel where id=?")
        String deleteById(int id);

    }

我mpl.java

    package com.ge.health.poc.service;

    import javax.persistence.EntityManager;

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Component;

    import com.ge.health.poc.interfac.BookRepository;
    import com.ge.health.poc.model.SpringModel;

    @Component
    public class BookServiceImpl implements BookService {

        @Autowired
        EntityManager entitymanager;

        @Autowired
        BookRepository bookrepo;

        @Override
        public void save(SpringModel bookdata) {

            bookrepo.save(bookdata);
        }

        public String retreive(int id) {
            String s = bookrepo.findName(id);
            System.out.println("Author name is:" + s);
            return null;

        }

        public void delete(int id) {

            System.out.println("M into service delete method");
            bookrepo.deleteById(id);
        }

        public void update(int id) {

            System.out.println("M in service update");
            bookrepo.UpdateByID(id);

        }

    }

这是模范班

            package com.ge.health.poc.model;

            import javax.persistence.Column;
            import javax.persistence.Entity;
            import javax.persistence.Id;
            import javax.persistence.Table;

            @Entity
            @Table(name = "spring_model")
            public class SpringModel {

                @Id
                private Long id;

                @Column
                private String name;

                public Long getId() {
                    return id;
                }

                public void setId(Long id) {
                    this.id = id;
                }

                @Column
                private String isbn;

                @Override
                public String toString() {
                    return "SpringModel [id=" + id + ", name=" + name + ", isbn=" + isbn + ", author=" + author + ", pages=" + pages
                            + "]";
                }

                @Column
                private String author;

                @Column
                private String pages;

                public String getName() {
                    return name;
                }

                public void setName(String name) {
                    this.name = name;
                }

                public String getIsbn() {
                    return isbn;
                }

                public void setIsbn(String isbn) {
                    this.isbn = isbn;
                }

                public String getAuthor() {
                    return author;
                }

                public void setAuthor(String author) {
                    this.author = author;
                }

                public String getPages() {
                    return pages;
                }

                public void setPages(String pages) {
                    this.pages = pages;
                }
            }

共有2个答案

慕乐语
2023-03-14

默认情况下,spring jpa会认为查询是select查询。因此,要确保查询正在更新特定实体的现有行

在更新现有行的方法上添加@modifying注释

这可能对你有用

堵昊焱
2023-03-14

在存储库方法上尝试注释@Modifying(org.springframework.data.jpa.repository.Modifying),在执行DML操作的服务实现中尝试注释@Transactional(org.springframework.transaction.annotation.Transactional)。更多信息请参考此答案。

 类似资料:
  • 我在springdatajpa中执行更新查询时遇到问题。这是存储库层中的代码: 如您所见,我已经添加了@修改注释,但它仍然抛出“不支持DML操作”的错误,我正在使用Hibernate 5.4。非常感谢任何帮助。

  • 我正在尝试更新数据库中的记录,但收到此错误 这是我的hql尝试 经过研究,我在方法的顶部添加了@修改注释,但错误仍然存在。请问怎么了?

  • Django试图尽可能多的支持所有数据库后端的特性。然而,并不是所有数据库都一样,所以我们必须在支持哪些特性和做出哪些安全的假定上做出设计决策。 本文描述了一些Django使用数据库的有关特性。当然,它并不想成为各服务器指定的文档或者参考手册的替代品。 综合说明 持续连接特性 持续连接的特性避免了每一次重新建立与数据库的连接的请求中所增加的压力。这些连接通过 CONN_MAX_AGE 参数(控制一

  • ThinkCMF的完全使用 TP5的数据库操作,这里只是简单说明基本操作,详细使用可以查看 TP5手册-数据库章节

  • ThinkCMF的完全使用 TP6.0的数据库操作,这里只是简单说明基本操作,详细使用可以查看 TP6.0手册-数据库章节

  • 这以前被问过一次,但解决方案并没有解决问题。我正在创建一个JUnit测试: 上述测试调用的查询是: 错误: