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

网友投稿 1350 2022-12-22

基于JPA实体类-@EntityListeners注解的使用实例

基于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 = productRepository.findById(10);

productRepository.save(product.orElse(null));

System.out.println("保存。。。");

}

@Transactional

public void testUpdate(){

productRepository.updateProduct("test10");

System.out.println("更新。。。");

}

}

执行结果:

未执行-内容? 暂不清楚是什么原因

这是AppVvBBollication的配置

@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 {

@Override

public Optional getCurrentAuditor() {

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

}

}

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:车载终端与车联网(车联网终端是什么)
下一篇:车载物联网信息终端(车联网智能车载终端)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~