欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

基于JPA实体类监听器@EntityListeners注解的使用实例

时间:2022-01-12 08:51:02|栏目:JAVA代码|点击:

JPA实体类监听器@EntityListeners注解的使用

被@Prepersist注解的方法 ,完成save之前的操作。

被@Preupdate注解的方法 ,完成update之前的操作。

被@PreRemove注解的方法 ,完成remove之前的操作。

被@Postpersist注解的方法 ,完成save之后的操作。

被@Postupdate注解的方法 ,完成update之后的操作。

被@PostRemovet注解的方法 ,完成remove之后的操作。

自定义实体类监听类:

import org.springframework.stereotype.Component;
import javax.persistence.PostPersist;
import javax.persistence.PostUpdate;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
public class TestEntityListeners {
	@PrePersist
	public void PrePersist(Object entity){
		System.out.println("开始保存--"+entity.toString());
	}
	@PreUpdate
	public void PreUpdate(Object entity){
		System.out.println("开始更新--"+entity.toString());
	}
 
	@PostPersist
	public void PostPersist(Object entity){
		System.out.println("结束保存--"+entity.toString());
	}
 
       @PostUpdate
	public void PostUpdate(Object entity){
		System.out.println("结束更新--"+entity.toString());
	}
}

实体类上增加注解:

@EntityListeners(value = {TestEntityListeners.class})
@Entity
@Table(name = "product")
@EntityListeners(value = {TestEntityListeners.class})
public class Product {
	private int id;
	private String productId;
	private String productName;
    //getter setter toString()
}

写两个测试保存、更新的方法:

import com.goods.evaluate.entity.Product;
import com.goods.evaluate.service.TestService1;
import com.goods.evaluate.repository.ProductRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; 
import java.util.Optional; 
@Service
public class TestService implements TestService1 { 
	@Autowired
	private ProductRepository productRepository; 
	@Transactional
	public void testSave(){
		Optional<Product> product = productRepository.findById(10);
		productRepository.save(product.orElse(null));
		System.out.println("保存。。。");
	} 
	@Transactional
	public void testUpdate(){
		productRepository.updateProduct("test10");
		System.out.println("更新。。。");
	}
}

执行结果:

未执行监听器内容? 暂不清楚是什么原因

这是Application的配置:

@SpringBootApplication
@EnableSpringConfigured
@EnableJpaAuditing
@EnableAspectJAutoProxy(proxyTargetClass=true)
public class EvaluateApplication { 
	public static void main(String[] args) {
		SpringApplication.run(EvaluateApplication.class, args);
	} 
}

@MappedSupperclass、@EntityListeners注解注意事项

1. @MappedSupperclass注解

(1) 该注解标注在类上,用来标识父类;

(2) 该注解标识的类不能映射到数据库,被标识的类的属性必须通过子类来映射;

(3) 该注解标识了类之后,不能再有@Entity和@Table注解标识该类

(4) 标识有该注解的类,通常属性上用以下注解@Id @GeneratedVale(strategy=GenerationType.IDENTITY) 、

@CreateDate 、 @CreateBy、@LastModifiedBy、@LastModifiedDate等注解用在父类上,子类基础该父类即可,

并在子类标注@Table和@Entity

2.@EntityListeners(AuditingEntityListener.class)注解

(1) 该注解用于监听实体类,在save、update之后的状态

(2) 使用了@EntityListeners(AuditingEntityListener.class)之后,记得在Application

启动类上加@EnableJpaAuditing,不然@CreateDate,@LastModifiedBy不生效

3.启用@EnableJpaAuditing,配置AuditorAware实现类

(1)只有使用了@EnableJpaAuditing注解,@CreateDate @LastModifiedBy等注解才会生效;

(2) 只有实现了AuditorAware接口,才会指定@CreateBy用户名信息。

//AuditorAware 实现类,指定当前操作用户信息
@Configuration
public class UserAuditorConfig implements AuditorAware<String> {
    @Override
    public Optional<String> getCurrentAuditor() {
        return Optional.of(SpringSecurityHolder.currentUserName());
    }
}

上一篇:Spring boot 配置多个redis的方法示例

栏    目:JAVA代码

下一篇:Springboot 读取自定义pro文件注入static静态变量方式

本文标题:基于JPA实体类监听器@EntityListeners注解的使用实例

本文地址:http://www.codeinn.net/misctech/189934.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有