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

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

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

c&0xff00   2021-08-02 我要评论
想了解基于JPA实体类监听器@EntityListeners注解的使用实例的相关内容吗,c&0xff00在本文为您仔细讲解JPA实体类@EntityListeners注解的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:JPA实体类监听器,@EntityListeners注解,实体类注解,下面大家一起来学习吧。

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());
    }
}

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们