温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

JPA中如何使用@EntityListeners注解

发布时间:2021-08-02 17:34:36 来源:亿速云 阅读:2161 作者: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