前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Hibernate “Callback methods”和“Entity listeners” 使用详解(注解方式)

Hibernate “Callback methods”和“Entity listeners” 使用详解(注解方式)

作者头像
飞奔去旅行
发布2019-06-13 11:20:09
9910
发布2019-06-13 11:20:09
举报
文章被收录于专栏:智慧协同智慧协同

Callback methodsEntity Listeners是Hibernate特别有用的特性,有时候会带来很多意想不到的功效哦!所以这里花点时间整理一下关于Callback methodsEntity Listeners的特性和使用方法,供大家查阅。

Callback methods顾名思义:“回调方法”,作用在Entity类中,结合@Entity。Hibernate支持通过注解和xml的方式轻松对Entity定义回调方法,个性化数据的增删改查

Hibernate支持的回调注解

@PrePersist

Executed before the entity manager persist operation is actually executed or cascaded. This call is synchronous with the persist operation. 在数据持久化到数据库之前执行

@PreRemove

Executed before the entity manager remove operation is actually executed or cascaded. This call is synchronous with the remove operation. 执行数据删除之前调用

@PostPersist

Executed after the entity manager persist operation is actually executed or cascaded. This call is invoked after the database INSERT is executed. 在执行数据插入之后调用

@PostRemove

Executed after the entity manager remove operation is actually executed or cascaded. This call is synchronous with the remove operation. 在执行数据删除之后调用

@PreUpdate

Executed before the database UPDATE operation. 在执行数据更新之前调用

@PostUpdate

Executed after the database UPDATE operation. 在执行数据更新之后调用

@PostLoad

Executed after an entity has been loaded into the current persistence context or an entity has been refreshed. 在数据从数据库加载并且赋值到当前对象后调用

使用场景

假设我们持久化的Entity都需要保存createdTime字段。这个字段比较特殊,在保存数据之前获取当前时间,赋值并保存。 传统的做法:

代码语言:javascript
复制
entity.setCreatedTime(new Date());
entityDao.save(entity);

使用Callback methods的做法 在创建entity的model类中使用@PrePersist,因为我们是在保存数据库之前给其赋值

代码语言:javascript
复制
@Entity(...)
public class EntityModel{
  ...
  private Date createdTime;
  @PrePersist
  protected void onCreate() {    
    createdTime= new Date();
  }
}

这样我们在业务处理的时候,只需处理业务相关的属性就可以了! 其他回调方法的用法类似,根据场景选择不同的回调就可以了。

关于EntityListeners

上面介绍了Callback methodsEntityListeners其实是定义了多个Callback methods。 接上面的示例,加入我们定义的所有的Entity都要保存createdTime属性,那么就可以定义一个EntityListener(一个Entity支持多个EntityListener的定义),将回调方法定义在其中,然后将Listener指定给Entity即可。 示例: 首先我们定义一个名为CreatedTimePersistentListener的类

代码语言:javascript
复制
public class CreatedTimePersistentListener{
  @PrePersist
  protected void onCreate(Object object) {
    try {
      BeanUtils.setProperty(object, "createdTime", new Date());
    }cache(Exception e){
      //....
    }
  }
}

使用apache的BeanUtils工具类,通过反射的方式,将createdTime属性赋值给object对象。(object对象必须包含createdTime属性)

然后通过@EntityListeners注解,作用给指定的Entity

代码语言:javascript
复制
@EntityListeners({CreatedTimePersistentListener.class})
public class EntityModel{
}

只要加了@EntityListeners({CreatedTimePersistentListener.class})的Entity都会默认在保存数据之前执行createdTime的赋值。

综合来说,“Callback methods”和“Entity listeners” 使用方法很简单,却非常有用,使我们的代码更容易组织和维护!

参考:Entity listeners and Callback methods

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2015.12.01 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Hibernate支持的回调注解
    • @PrePersist
      • @PreRemove
        • @PostPersist
          • @PostRemove
            • @PreUpdate
              • @PostUpdate
                • @PostLoad
                • 使用场景
                • 关于EntityListeners
                相关产品与服务
                数据库
                云数据库为企业提供了完善的关系型数据库、非关系型数据库、分析型数据库和数据库生态工具。您可以通过产品选择和组合搭建,轻松实现高可靠、高可用性、高性能等数据库需求。云数据库服务也可大幅减少您的运维工作量,更专注于业务发展,让企业一站式享受数据上云及分布式架构的技术红利!
                领券
                问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档