基于JPA實(shí)體類監(jiān)聽器@EntityListeners注解的使用實(shí)例
JPA實(shí)體類監(jiān)聽器@EntityListeners注解的使用
被@Prepersist注解的方法 ,完成save之前的操作。
被@Preupdate注解的方法 ,完成update之前的操作。
被@PreRemove注解的方法 ,完成remove之前的操作。
被@Postpersist注解的方法 ,完成save之后的操作。
被@Postupdate注解的方法 ,完成update之后的操作。
被@PostRemovet注解的方法 ,完成remove之后的操作。

自定義實(shí)體類監(jiān)聽類:
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("結(jié)束保存--"+entity.toString());
}
@PostUpdate
public void PostUpdate(Object entity){
System.out.println("結(jié)束更新--"+entity.toString());
}
}
實(shí)體類上增加注解:
@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("更新。。。");
}
}
執(zhí)行結(jié)果:

未執(zhí)行監(jiān)聽器內(nèi)容? 暫不清楚是什么原因
這是Application的配置:
@SpringBootApplication
@EnableSpringConfigured
@EnableJpaAuditing
@EnableAspectJAutoProxy(proxyTargetClass=true)
public class EvaluateApplication {
public static void main(String[] args) {
SpringApplication.run(EvaluateApplication.class, args);
}
}
@MappedSupperclass、@EntityListeners注解注意事項(xiàng)
1. @MappedSupperclass注解
(1) 該注解標(biāo)注在類上,用來標(biāo)識父類;
(2) 該注解標(biāo)識的類不能映射到數(shù)據(jù)庫,被標(biāo)識的類的屬性必須通過子類來映射;
(3) 該注解標(biāo)識了類之后,不能再有@Entity和@Table注解標(biāo)識該類
(4) 標(biāo)識有該注解的類,通常屬性上用以下注解@Id @GeneratedVale(strategy=GenerationType.IDENTITY) 、
@CreateDate 、 @CreateBy、@LastModifiedBy、@LastModifiedDate等注解用在父類上,子類基礎(chǔ)該父類即可,
并在子類標(biāo)注@Table和@Entity
2.@EntityListeners(AuditingEntityListener.class)注解
(1) 該注解用于監(jiān)聽實(shí)體類,在save、update之后的狀態(tài)
(2) 使用了@EntityListeners(AuditingEntityListener.class)之后,記得在Application
啟動類上加@EnableJpaAuditing,不然@CreateDate,@LastModifiedBy不生效
3.啟用@EnableJpaAuditing,配置AuditorAware實(shí)現(xiàn)類
(1)只有使用了@EnableJpaAuditing注解,@CreateDate @LastModifiedBy等注解才會生效;
(2) 只有實(shí)現(xiàn)了AuditorAware接口,才會指定@CreateBy用戶名信息。
//AuditorAware 實(shí)現(xiàn)類,指定當(dāng)前操作用戶信息
@Configuration
public class UserAuditorConfig implements AuditorAware<String> {
@Override
public Optional<String> getCurrentAuditor() {
return Optional.of(SpringSecurityHolder.currentUserName());
}
}
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
解決 IDEA 創(chuàng)建 Gradle 項(xiàng)目沒有src目錄問題
這篇文章主要介紹了解決 IDEA 創(chuàng)建 Gradle 項(xiàng)目沒有src目錄問題,本文圖文并茂給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-06-06
Springboot實(shí)例講解實(shí)現(xiàn)專業(yè)材料認(rèn)證管理系統(tǒng)流程
這是一個基于java的畢業(yè)設(shè)計(jì)項(xiàng)目,畢設(shè)課題為springboot框架的知識產(chǎn)權(quán)服務(wù)平臺系統(tǒng),是一個采用b/s結(jié)構(gòu)的javaweb項(xiàng)目,需要的朋友可以參考下2022-06-06

