Java笔记-jpa中数据存储及更新应该注意的问题
生活随笔
收集整理的這篇文章主要介紹了
Java笔记-jpa中数据存储及更新应该注意的问题
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
本次源碼是基于此篇博文的:https://blog.csdn.net/qq78442761/article/details/95938406
?
這里要注意的問題是:
當數據庫表結構是這樣的!
這里就是update_time的默認值為CURRENT_TIMESTAMP
并且還設置了on update屬性。也就是當修改數據后,這個屬性會被更新。
檢索的例子:
@Test public void findOneTest(){ProductCategory productCategory = repository.findOne(1);System.out.println(productCategory); }這里是個存儲的例子:
@Test public void saveTest(){ProductCategory productCategory = new ProductCategory();productCategory.setCategoryName("男生最愛");productCategory.setCategoryType(1);repository.save(productCategory); }這個是更新操作:
@Test public void updateTest(){ProductCategory productCategory = repository.findOne(2);productCategory.setCategoryType(11);repository.save(productCategory); }這里要注意:
通過這個DynamicUpdate注解,可以實現當某個字段是on update時可以完成更新。
ProductCategory.java
package selldemo.demo.dataobject;import org.hibernate.annotations.DynamicUpdate; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import java.util.Date;@Entity @DynamicUpdate public class ProductCategory {@Id@GeneratedValueprivate Integer categoryId;private String categoryName;private Integer categoryType;private Date createTime;private Date updateTime;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;}@Overridepublic String toString() {return "ProductCategory{" +"categoryId=" + categoryId +", categoryName='" + categoryName + '\'' +", categoryType=" + categoryType +", createTime=" + createTime +", updateTime=" + updateTime +'}';} }?
總結
以上是生活随笔為你收集整理的Java笔记-jpa中数据存储及更新应该注意的问题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: RabbitMQ笔记-使用rabbitm
- 下一篇: 系统架构师学习笔记-系统性能评价