JPA Audit

在spring jpa中,支持在字段或者方法上进行注解@CreatedDate@CreatedBy@LastModifiedDate@LastModifiedBy,从字面意思可以很清楚的了解,这几个注解的用处。

  • @CreatedDate
    表示该字段为创建时间时间字段,在这个实体被insert的时候,会设置值
  • @CreatedBy
    表示该字段为创建人,在这个实体被insert的时候,会设置值

  • @LastModifiedDate@LastModifiedBy同理。

如何使用?

首先申明实体类,需要在类上加上注解@EntityListeners(AuditingEntityListener.class),其次在application启动类中加上注解EnableJpaAuditing,同时在需要的字段上加上@CreatedDate@CreatedBy@LastModifiedDate@LastModifiedBy等注解。

这个时候,在jpa.save方法被调用的时候,时间字段会自动设置并插入数据库,但是CreatedBy和LastModifiedBy并没有赋值,因为需要实现AuditorAware接口来返回你需要插入的值。

  • Application
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.annotation.Import;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing; @SpringBootApplication
@EnableJpaAuditing
public class WalletApplication {
public static void main(String[] args) {
new SpringApplicationBuilder(WalletApplication.class).web(true).run(args);
}
}
  • AuditorAware
import org.springframework.context.annotation.Configuration;
import org.springframework.data.domain.AuditorAware;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder; @Configuration
public class UserIDAuditorBean implements AuditorAware<Long> {
@Override
public Long getCurrentAuditor() {
SecurityContext ctx = SecurityContextHolder.getContext();
if (ctx == null) {
return null;
}
if (ctx.getAuthentication() == null) {
return null;
}
if (ctx.getAuthentication().getPrincipal() == null) {
return null;
}
Object principal = ctx.getAuthentication().getPrincipal();
if (principal.getClass().isAssignableFrom(Long.class)) {
return (Long) principal;
} else {
return null;
}
}
}
  • Entity
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.Table; import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener; /**
* 店铺与支付渠道设备绑定.
* @author Wang.ch
*
*/
@Entity
@Table(name = "store_source_bind")
@EntityListeners(AuditingEntityListener.class)
public class StoreSourceBind {
/**
* 创建时间
*/
@Column(name = "create_time")
@CreatedDate
private Date createTime;
/**
* 创建人
*/
@Column(name = "create_by")
@CreatedBy
private Long createBy;
/**
* 修改时间
*/
@Column(name = "lastmodified_time")
@LastModifiedDate
private Date lastmodifiedTime;
/**
* 修改人
*/
@Column(name = "lastmodified_by")
@LastModifiedBy
private String lastmodifiedBy;
}

Spring JPA 使用@CreatedDate、@CreatedBy、@LastModifiedDate、@LastModifiedBy 自动生成时间和修改者的更多相关文章

  1. Spring Boot (七)MyBatis代码自动生成和辅助插件

    一.简介 1.1 MyBatis Generator介绍 MyBatis Generator 是MyBatis 官方出品的一款,用来自动生成MyBatis的 mapper.dao.entity 的框架 ...

  2. win10如何设置自动睡眠时间(修改电源计划不好用的情况下)

    https://answers.microsoft.com/en-us/windows/forum/windows_10-power/windows-10-sleeping-when-set-not- ...

  3. IDEA创建新文件时自动生成时间和作者

    打开设置,打开下图的选项并且输入 /** * @author 你的名字 * @date ${DATE} ${TIME} */

  4. Java JPA设置默认值、Timestamp设置、自动获取时间

    设置默认值 @Column(name="state",columnDefinition="tinyint default 0") private Integer ...

  5. net软件自动生成开发编程框架编程机器人

    有一个.net自动生成平台(编程机器人)推荐给大家,常规几天十几天的工作,机器人几分钟搞定,不写一行代码,留下大把休闲时光,适应于聪明人:不想太累的程序员(看看风景泡泡妞),不想多请人的老板(有限资金 ...

  6. 设置ViewPager 自动滑动时间,速度 方便展示动画

    ViewPager.setCurrentItem(position),即使已设置动画,但是没有动画效果 原因:因为ViewPager滑动之前的时间间隔太短,可以通过反射,去修改ViewPager自动滑 ...

  7. Spring Data JPA系列5:让IDEA自动帮你写JPA实体定义代码

    大家好,又见面了. 这是本系列的最后一篇文档啦,先来回顾下前面4篇: 在第1篇<Spring Data JPA系列1:JDBC.ORM.JPA.Spring Data JPA,傻傻分不清楚?给你 ...

  8. Spring+jpa+access

    ========访问数据库的属于文件============ driver=com.hxtt.sql.access.AccessDriverurl=jdbc:access:/D:/eclipse/pr ...

  9. Hibernate | Spring JPA | MySQL 使用过程遇到的一些问题

    1. 使用过程 2. 背景 3. 遇到问题 3.1 不指定Hibernate数据库方言,默认SQL生成方式 3.2 抛出异常Hibernate加入了@Transactional事务不会回滚 3.3 H ...

随机推荐

  1. centos7.3 kubernetes/k8s 1.10 离线安装 --已验证

    本文介绍在centos7.3使用kubeadm快速离线安装kubernetes 1.10. 采用单master,单node(可以多node),占用资源较少,方便在笔记本或学习环境快速部署,不适用于生产 ...

  2. AI应用开发实战 - 手写识别应用入门

    AI应用开发实战 - 手写识别应用入门 手写体识别的应用已经非常流行了,如输入法,图片中的文字识别等.但对于大多数开发人员来说,如何实现这样的一个应用,还是会感觉无从下手.本文从简单的MNIST训练出 ...

  3. 自己整理的所有java知识点(不断迭代中)

    1. 自己整理的所有java知识点(不断迭代中) 画图工具注册 https://www.processon.com/i/599d35fae4b00d97d7f9bb17 1.1. Java整体知识架构 ...

  4. AI - TensorFlow - 第一个神经网络(First Neural Network)

    Hello world # coding=utf-8 import tensorflow as tf import os os.environ[' try: tf.contrib.eager.enab ...

  5. python --- 二分查找算法

    二分查找法:在我的理解中这个查找方法为什么会叫二分呢,我认为是将要查询的一个列表分成了两份,然后在利用某个值来进行比较,在一个不断循环的过程中来找出我们要找的某一个值. 废话不多说,先上代码: def ...

  6. git 建议使用

    1 登录github官网首页 创建一个项目 2 本地克隆下载git项目 git clone https://github.com/wangguoxingduanxuejing/branch-pract ...

  7. Android APP应用启动页白屏(StartingWindow)优化

    版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 StartingWindow 的处理方式: 使用系统默认的 StartingWindow :用户点了应用图标启动应用,马上弹出系统默 ...

  8. Windows 10 安装ElasticSearch(2)- MSI安装ElasticSearch和安装Kibana

    翻阅上篇文章:Windows 10 安装 ElasticSearch 上次写的是下载Zip包安装的,在下载页面 发现有 MSI (BETA) 的下载可选项.了解之后发现MSI安装也值得尝试. MSI安 ...

  9. C# ADO.NET的SqlDataReader对象,判断是否包含指定字段

    在使用ado.net的SqlDataReader对象时,如果SqlDataReader实例对象中没有对应的字段,则会在那一行报错.而SqlDataReader类又没有判断是否存在指定字段的方法,怎么办 ...

  10. [转]Blue Prism Architecture

    本文转自:https://mindmajix.com/blue-prism-architecture Introduction Automation technology is widely bloo ...