查询出来的对象ProductCategory就已经有updateTime和createTime了,然而你只是把对象的categoryType给修改了一下,修改之后就执行save方法保存了。所以它还是原来的时间。如果有一个自动更新时间的目的,那就增加一个注解
@DynamicUpdate //动态更新时间. public class ProductCategory{
package com.imooc.sell.dataobject; //import javax.persistence.Table; import org.hibernate.annotations.DynamicUpdate; import org.hibernate.annotations.Proxy; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import java.util.Date; /** * 类目 * Created by zhongzh * 2018-05-20 9:31 * s_product_category */ //@Table(name = "s_product_category") @Entity//把数据库映射成对象 @Proxy(lazy = false) @DynamicUpdate //动态更新时间. public class ProductCategory{ /** 类目id. */ @Id//Id是主键,自增类型的。 //@GeneratedValue//相当于调用了native策略 //@GeneratedValue(strategy = GenerationType.AUTO) @GeneratedValue(strategy = GenerationType.IDENTITY) private Integer categoryId;//字段名的命名方式也是一样的,把下划线改成为空。 /** 类目名字. */ private String categoryName; /** 类目编号. */ private Integer categoryType; /** 创建时间. */ private Date createTime; /** 修改时间. */ private Date updateTime; //不要忘了Getter和Setter方法 public Integer getCategoryId() { return categoryId; } public void setCategoryId(Integer categoryId) { this.categoryId = categoryId; } public String getCategoryName() { return categoryName; } public void setCategoryName(String categoryName) { this.categoryName = categoryName; } public Integer getCategoryType() { return categoryType; } public void setCategoryType(Integer categoryType) { this.categoryType = categoryType; } public Date getCreateTime() { return createTime; } public void setCreateTime(Date createTime) { this.createTime = createTime; } public Date getUpdateTime() { return updateTime; } public void setUpdateTime(Date updateTime) { this.updateTime = updateTime; } @Override public String toString() { return "ProductCategory{" + "categoryId=" + categoryId + ", categoryName='" + categoryName + '\'' + ", categoryType=" + categoryType + '}'; } }
更新之前
Hibernate: select productcat0_.category_id as category1_0_0_, productcat0_.category_name as category2_0_0_, productcat0_.category_type as category3_0_0_, productcat0_.create_time as create_t4_0_0_, productcat0_.update_time as update_t5_0_0_ from product_category productcat0_ where productcat0_.category_id=? Hibernate: select productcat0_.category_id as category1_0_0_, productcat0_.category_name as category2_0_0_, productcat0_.category_type as category3_0_0_, productcat0_.create_time as create_t4_0_0_, productcat0_.update_time as update_t5_0_0_ from product_category productcat0_ where productcat0_.category_id=?
更新之后
@Test public void saveTest(){ ProductCategory productCategory = repository.getOne(2); //ProductCategory productCategory = new ProductCategory(); //productCategory.setCategoryId(2); //productCategory.setCategoryName("女生最爱"); //productCategory.setCategoryType(3); //productCategory.setCategoryName("男生最爱"); //productCategory.setCategoryType(4); //productCategory.setCategoryName("老少咸宜"); //productCategory.setCategoryType(5); productCategory.setCategoryType(11); repository.save(productCategory); }
Hibernate: select productcat0_.category_id as category1_0_0_, productcat0_.category_name as category2_0_0_, productcat0_.category_type as category3_0_0_, productcat0_.create_time as create_t4_0_0_, productcat0_.update_time as update_t5_0_0_ from product_category productcat0_ where productcat0_.category_id=? Hibernate: select productcat0_.category_id as category1_0_0_, productcat0_.category_name as category2_0_0_, productcat0_.category_type as category3_0_0_, productcat0_.create_time as create_t4_0_0_, productcat0_.update_time as update_t5_0_0_ from product_category productcat0_ where productcat0_.category_id=? Hibernate: update product_category set category_type=? where category_id=?
更新之后
老司机带来干货了!!!!!
实体类对象不用再写getter和setter方法了。在pom.xml引进