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

摘要:
PublicclassTestEntityListeners{@PrePersistpublicvoidPrePersist(对象实体){System.out.println(“开始保存--”+entity.toString());

被@Prepersist注解的方法 ,完成save之前的操作。
被@Preupdate注解的方法 ,完成update之前的操作。
被@PreRemove注解的方法 ,完成remove之前的操作。
被@Postpersist注解的方法 ,完成save之后的操作。
被@Postupdate注解的方法 ,完成update之后的操作。
被@PostRemovet注解的方法 ,完成remove之后的操作。JPA实体类监听器@EntityListeners注解使用实例第1张

自定义实体类监听类:

 
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注解使用实例第2张

 这是Application的配置:

@SpringBootApplication
@EnableSpringConfigured
@EnableJpaAuditing
@EnableAspectJAutoProxy(proxyTargetClass=true)
public class EvaluateApplication {
 
    public static void main(String[] args) {
        SpringApplication.run(EvaluateApplication.class, args);
    }
 
}





免责声明:文章转载自《JPA实体类监听器@EntityListeners注解使用实例》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇2017年陕西省网络空间安全技术大赛WP解决迅雷的“重复任务提示”下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

python之logging 模块(下篇)

四、日志处理流程(第二种日志使用方式) 上面简单配置的方法例子中我们了解到了logging.debug()、logging.info()、logging.warning()、logging.error()、logging.critical()(分别用以记录不同级别的日志信息),logging.basicConfig()(用默认日志格式(Formatter)...

Internet Explorer无法打开站点,已终止操作

在页面还没有ready的时候就调用了htmlObject的appendChild或者innerHTML操作,这样会在IE上弹出一个对话框:“Internet Explorer无法打开站点,已终止操作”   在网上搜索了一下,解决方法大概为以下两点:   (1)在appendChild或者innerHTML操作处判断document.readyState==...

php中session_start()相关问题分析与解决办法

介绍下,在php中使用session时遇到的一些问题,与相关解决方法。1.错误提示Warning: Cannot send session cookie - headers already sentWarning: Cannot send session cache limiter - headers already sent分析及解决办法这一类问题,的原...

【转载】C#的ArrayList使用IndexOf方法查找第一个符合条件的元素位置

在C#的编程开发中,ArrayList集合是一个常用的非泛型类集合,在ArrayList集合中如果需要查找第一个符合条件的元素所在的位置,可以使用ArrayList集合的IndexOf方法,IndexOf方法将会返回符合条件的第一个元素在集合中的索引位置信息,如果未查到符合条件的元素对象,则返回-1。 IndexOf方法的其中一个常用方法签名为:virtu...

JavaScript 引用类型 ( 5章 )

引用类型常被称为 “类”,但是这在JavaScript中不太合适。它是属性和方法的集合。 引用类型的值"对象"是引用类型的实例。 特殊的标识符和运算符  符号 类型 执行操作 () 函数 函数调用 new 构造函数调用 创建新对象     new 运算符用来创建一个新对象,并调用构造函数初始化它,new 是一个一元运算符,出现在构造函数的调...

Android 综合揭秘 —— 全面剖释 Service 服务

引言 Service 服务是 Android 系统最常用的四大部件之一,Android 支持 Service 服务的原因主要目的有两个,一是简化后台任务的实现,二是实现在同一台设备当中跨进程的远程信息通信。Service 服务主要分为 Local Service 本地服务与 Remote Service 远程服务两种,本地服务只支持同一进程内的应用程序进行...