hibernate实体中@Column(name = "name",updatable=false)

本文介绍了一种解决页面更新时间在传递过程中丢失的方法。通过调整实体属性设置,确保了审计过程中更新时间的有效记录。
              平台有个功能,实体会有几个固定记录项目,比如创建时间,更新时间等。但是在页面传递时会丢失更新时间。如果在审计时,将createTime的属性增加updateable=“false”,那么就不会出现创建时间是空的问题了
@Id @Column(name = "id") @ApiModelProperty("ID") private String id; @Column(name = "code") @ApiModelProperty("项目编号") private String code; @Column(name = "opportunity_code") @ApiModelProperty("商机编号") private String opportunityCode; @Column(name = "name") @ApiModelProperty("项目名称") private String name; @Column(name = "kind") @ApiModelProperty("项目类型") private String kind; @Column(name = "manager_id") @ApiModelProperty("项目负责人工号") private String managerId; @Column(name = "manager_name") @ApiModelProperty("项目负责人姓名") private String managerName; @Column(name = "customer_name") @ApiModelProperty("客户名称") private String customerName; @Column(name = "product_model") @ApiModelProperty("产品型号") private String productModel; @Column(name = "develop_group") @ApiModelProperty("研发组") private String developGroup; @Column(name = "data_status") @ApiModelProperty("数据类型:1 新增,2:更新") private Integer dataStatus; @Column(name = "sync_status") @ApiModelProperty("同步状态:0未同步,1已同步") private Integer syncStatus = 0; @Column(name = "data_time") @Temporal(TemporalType.TIMESTAMP) @ApiModelProperty("项目数据创建或更新时间") private Date dataTime; @Column(name = "create_time") @Temporal(TemporalType.TIMESTAMP) @ApiModelProperty("创建时间") private Date createTime = new Date(); @Column(name = "last_update_time") @Temporal(TemporalType.TIMESTAMP) @ApiModelProperty("最后更新时间") private Date lastUpdateTime = new Date(); 我的实体类时这样的 然后如果是更新操作,data_status设置成“2”,插入的话就为“1”,然后每一次更新或者插入操作sync_status得设置成“0”,然后data_time是更新或者插入都得更新为当前时间,create_time是插入的时候才会有,更新的时候不变,然后last_update_time是插入的时候会有,更新的时候不变,id是插入的时候生成的,更新的时候不变
09-30
修改后的Product类: package com.warehouse.demo.entity; import javax.persistence.*; import java.math.BigDecimal; import java.time.LocalDateTime; @Entity @Table(name = "products") public class Product { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @Column(nullable = false, unique = true) private String productCode; @Column(nullable = false) private String name; private String description; @Column(nullable = false) private BigDecimal price; @Column(nullable = false) private Integer stockQuantity; @ManyToOne @JoinColumn(name = "category_id") private Category category; @ManyToOne @JoinColumn(name = "supplier_id") private Supplier supplier; @Column(nullable = false) private LocalDateTime createTime; private LocalDateTime updateTime; // 构造方法 public Product() {} public Product(String productCode, String name, String description, BigDecimal price, Integer stockQuantity) { this.productCode = productCode; this.name = name; this.description = description; this.price = price; this.stockQuantity = stockQuantity; this.createTime = LocalDateTime.now(); } // Getter 和 Setter 方法 public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getProductCode() { return productCode; } public void setProductCode(String productCode) { this.productCode = productCode; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public BigDecimal getPrice() { return price; } public void setPrice(BigDecimal price) { this.price = price; } public Integer getStockQuantity() { return stockQuantity; } public void setStockQuantity(Integer stockQuantity) { this.stockQuantity = stockQuantity; } public Category getCategory() { return category; } public void setCategory(Category category) { this.category = category; } public Supplier getSupplier() { return supplier; } public void setSupplier(Supplier supplier) { this.supplier = supplier; } public LocalDateTime getCreateTime() { return createTime; } public void setCreateTime(LocalDateTime createTime) { this.createTime = createTime; } public LocalDateTime getUpdateTime() { return updateTime; } public void setUpdateTime(LocalDateTime updateTime) { this.updateTime = updateTime; } @PrePersist protected void onCreate() { createTime = LocalDateTime.now(); } @PreUpdate protected void onUpdate() { updateTime = LocalDateTime.now(); } @Override public String toString() { return "Product{" + "id=" + id + ", productCode='" + productCode + '\'' + ", name='" + name + '\'' + ", price=" + price + ", stockQuantity=" + stockQuantity + '}'; } }
10-11
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值