好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

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

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

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

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

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

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

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

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

自定义实体类监听类:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

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

     }

}

实体类上增加注解:

?

1

@EntityListeners (value = {TestEntityListeners. class })

?

1

2

3

4

5

6

7

8

9

@Entity

@Table (name = "product" )

@EntityListeners (value = {TestEntityListeners. class })

public class Product {

     private int id;

     private String productId;

     private String productName;

     //getter setter toString()

}

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

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

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的配置:

?

1

2

3

4

5

6

7

8

9

@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用户名信息。

?

1

2

3

4

5

6

7

8

//AuditorAware 实现类,指定当前操作用户信息

@Configuration

public class UserAuditorConfig implements AuditorAware<String> {

     @Override

     public Optional<String> getCurrentAuditor() {

         return Optional.of(SpringSecurityHolder.currentUserName());

     }

}

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

原文链接:https://blog.csdn.net/weixin_37968613/article/details/88554236

查看更多关于基于JPA实体类监听器@EntityListeners注解的使用实例的详细内容...

  阅读:28次