Solve the problem that the null value of JPA save of method overrides the default value of mysql preset

  • 2021-12-13 08:15:37
  • OfStack

Directory JPA save () method null value overrides mysql default value overrides cause solution data jpa dynamic insertion (null is sql default value, utime automatically updates)

JPA save () method null value overrides mysql default

Coverage reason

The save () method defaults to the null value when no parameters are passed in, while the field in the mysql table is set to the null value. At this time, although we set the default value, the null value will override the default value.


Simply set the field to disallow the null value so that the null value is replaced with the default value.

data jpa Dynamic Insertion (null is the default for sql, utime is automatically updated)

@Table(name = "tb_order_history")
public class OrderHistory implements Serializable {
    private static final long serialVersionUID = -1L;
    @Column(name = "id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id; //  Order identification id
    @Column(name = "user_id")
    private Long userId; //  User ID 
    @Column(name = "channel_id")
    private Long channelId; //  Channel identification 
    @Column(name = "is_active")
    private int isActive;// Is the order closed 
    @Column(name = "status")
    private OrderStatus orderStatus;//  Order status 
    @Column(name = "ctime")
    private Timestamp ctime; //  Creation time 
    @Column(name = "utime",updatable = false)
    private Timestamp utime; //  Update time 

Related articles: