随笔 - 832  文章 - 2  评论 - 31  阅读 - 167万

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);
    }
 
}
复制代码

 





posted on   小破孩楼主  阅读(2236)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
< 2025年3月 >
23 24 25 26 27 28 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 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示