溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

JPA中如何使用@EntityListeners注解

發布時間:2021-08-02 17:34:36 來源:億速云 閱讀:2273 作者:Leah 欄目:開發技術

這篇文章給大家介紹JPA中如何使用@EntityListeners注解,內容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。

JPA實體類監聽器@EntityListeners注解的使用

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

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

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

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

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

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

JPA中如何使用@EntityListeners注解

自定義實體類監聽類:

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("更新。。。");
	}
}

執行結果:

JPA中如何使用@EntityListeners注解

未執行監聽器內容? 暫不清楚是什么原因

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

關于JPA中如何使用@EntityListeners注解就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女