Spring Data JPA: How to update a model elegantly?

后端 未结 2 2048
醉梦人生
醉梦人生 2020-12-16 02:30
  1. My Model is like this:

    @Entity
    @Table(name = \"user\")
    public class User {
    
        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        private          
    
    
            
2条回答
  •  Happy的楠姐
    2020-12-16 02:38

    At first I need to say the Patch solution by @Oliver Gierke is really great.

    And In my previous project, my solution is:
    1. Create a abstract class to implement JpaRepository, and implement save() function.
    2. In the save function, get the entity from DB. It not exist, just save it. Otherwise, use reflection to get all NOT NULL fields from the updated entity, and set into entity from DB. (it is similar as you do, but I did it for all save function)

提交回复
热议问题