jpa更新部分字段一般怎么处理

来源:9-6 卖家订单-controller-订单详情

Mark的一天

2018-12-17

jpa更新部分字段一般怎么处理 比较简单一点

写回答

2回答

廖师兄

2018-12-17

实体类上加上@DynamicUpdate

0
4
廖师兄
回复
Mark的一天
实际情况,肯定要先查出来,再进行业务判断(是否可以取消),然后再修改
2018-12-24
共4条回复

Mark的一天

提问者

2018-12-17

始终不生效,不确定什么原因

==========================pom


<parent>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-parent</artifactId>
   <version>1.5.9.RELEASE</version>
   <relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
   <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
   <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
   <java.version>1.8</java.version>
</properties>

<dependencies>
   <!--jpa-->
   <dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-data-jpa</artifactId>
   </dependency>

==========================实体bean


import lombok.Data;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import java.util.Date;

@Entity
@Table(name = "hhy_book")
@DynamicUpdate(true)
@DynamicInsert(true)
@SelectBeforeUpdate(true)

@Data
public class Book {
   /* id */
   @Id
   private String id;

   /* 书名 */
   private String name;

   private String description;

   private Date createTime;

   private Date updateTime;
}


==========================BookRepository


import com.guangchilieche.server.domain.Book;
import org.springframework.data.jpa.repository.JpaRepository;

public interface BookRepository extends JpaRepository<Book, String> {
}


==========================测试代码

@Test
public void testJpaUpdate() {
   Book book0 = new Book();
   book0.setId("1");
   book0.setName("aaaa");
   book0.setDescription("bbbb");
   book0 = hhyBookRepository.saveAndFlush(book0);
   log.error("book0:" + JsonUtil.bean2Json(book0));
   log.error("------------------update");
   Book book1 = new Book();
   book1.setId("1");
   book1.setName("cccccc");
   book1.setDescription(null);
   book1 = hhyBookRepository.saveAndFlush(book1);
   log.error("book1:" + JsonUtil.bean2Json(book1));

   log.error("------------------query");
   Book book2 = hhyBookRepository.findOne("1");
   log.error("book2:" + JsonUtil.bean2Json(book2));
}

==========================测试log

cHibernate: 

    select

        book0_.id as id1_7_0_,

        book0_.create_time as create_t2_7_0_,

        book0_.description as descript3_7_0_,

        book0_.name as name4_7_0_,

        book0_.update_time as update_t5_7_0_ 

    from

        hhy_book book0_ 

    where

        book0_.id=?

Hibernate: 

    insert 

    into

        hhy_book

        (description, name, id) 

    values

        (?, ?, ?)

2018-12-17 15:39:39.222 ERROR 5497 --- [           main] c.g.s.c.m.c.MemberClientControllerTest   : book0:{

  "id" : "1",

  "name" : "aaaa",

  "description" : "bbbb",

  "createTime" : null,

  "updateTime" : null

}

2018-12-17 15:39:39.223 ERROR 5497 --- [           main] c.g.s.c.m.c.MemberClientControllerTest   : ------------------update

Hibernate: 

    select

        book0_.id as id1_7_0_,

        book0_.create_time as create_t2_7_0_,

        book0_.description as descript3_7_0_,

        book0_.name as name4_7_0_,

        book0_.update_time as update_t5_7_0_ 

    from

        hhy_book book0_ 

    where

        book0_.id=?

Hibernate: 

    update

        hhy_book 

    set

        description=?,

        name=? 

    where

        id=?

2018-12-17 15:39:39.229 ERROR 5497 --- [           main] c.g.s.c.m.c.MemberClientControllerTest   : book1:{

  "id" : "1",

  "name" : "cccccc",

  "description" : null,

  "createTime" : null,

  "updateTime" : null

}

2018-12-17 15:39:39.229 ERROR 5497 --- [           main] c.g.s.c.m.c.MemberClientControllerTest   : ------------------query

Hibernate: 

    select

        book0_.id as id1_7_0_,

        book0_.create_time as create_t2_7_0_,

        book0_.description as descript3_7_0_,

        book0_.name as name4_7_0_,

        book0_.update_time as update_t5_7_0_ 

    from

        hhy_book book0_ 

    where

        book0_.id=?

2018-12-17 15:39:39.234 ERROR 5497 --- [           main] c.g.s.c.m.c.MemberClientControllerTest   : book2:{

  "id" : "1",

  "name" : "cccccc",

  "description" : null,

  "createTime" : null,

  "updateTime" : null

}

=================

0
0

Spring Boot双版本(1.5/2.1) 打造企业级微信点餐系统

从0到1开发中小型企业级Java应用,并学会迭代重构技巧

6410 学习 · 5247 问题

查看课程