触发器与 JPA 事件 [英] Triggers versus JPA Event

查看:22
本文介绍了触发器与 JPA 事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Spring 3.1.0.RELEASE、JSF 2.x、JPA 2 和 Hibernate Provider、MySql 5.1.x 做一个 Web 应用程序.该应用程序在 Tomcat 7.X 上运行.

I'm doing a Web application using Spring 3.1.0.RELEASE, JSF 2.x, JPA 2 with Hibernate Provider, MySql 5.1.x. The application runs on Tomcat 7.X.

在我的实体中,我有一些日期,例如上次更新日期:

In my entities I have some date like last update date:

@Column(name = "last_update_date", insertable = false, updatable = false)
@Temporal(TemporalType.TIMESTAMP)
private Date lastUpdateDate;

目前我有一个更新的触发器:

For the moment I have a trigger that updates:

CREATE TRIGGER upd_site BEFORE UPDATE ON site
FOR EACH ROW SET NEW.last_update_date = CURRENT_TIMESTAMP();

它工作正常,但我只是注意到 JPA 中有一些回调方法 http://www.objectdb.com/java/jpa/persistence/event

It works fine, but I just notice that there is some callbacks methods in JPA http://www.objectdb.com/java/jpa/persistence/event

JPA Events 和 MySql 的触发器之间最好的是什么?

What is the best between JPA Events and the MySql's triggers ?

谢谢.

推荐答案

我已经将它与数据库中的触发器和 JPA 侦听器同时使用,我选择了 JPA 侦听器,因为:

I have used it both ways with Triggers in the DB and with JPA listeners, I have settled on the JPA listeners because:

  • 唯一与 JPA 代码中的数据库对话的代码,因此我不必担心时间戳字段会过期.(如果这在将来发生变化,我可以添加触发器并更改我映射的 super cals)

  • the only code talking to the database in JPA code so I don't have to worry about the time stamp fields falling out of date. (If this changes in the future I can add triggers and change my mapped super calss)

JPA 侦听器不那么复杂,因为我不必在我的数据库中创建大量触发器,因此我需要维护的东西更少.由于我正在积极开发和更改数据库结构,因此在我快速迭代开发过程中不必去更新触发器是很棒的.

JPA listeners are less complex in the sense that I did not have to go creating lots of triggers in my database so I had less things to maintain. Since I am actively developing and changing the db structure as I go along its great not to have to go and update triggers as I rapidly iterate through the development.

我完全控制了数据库并为数据库制定了一个规则,即每个表都将有一个整数 pkey 和一个整数版本,并且时间戳表将具有 insert_tsupdate_ts 列这些是我的数据库设计中的通用规则,所以生活很简单我有这两个映射的超类,因为我从它们扩展而来,这使我的所有实体都易于编码.

I have complete control over the database and made a rule for the db that every table was going to have a integer pkey, and an integer version, and that the time stamped tables would have insert_ts and update_ts columns these are universal rules in my db design so life is easy I have these two mapped superclases that make all my enitites simple to code since I extend from them.

@MappedSuperclass
public abstract class PersistableObject {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name="pkey")
    private Integer pkey;

    @Version
    @Column(name="version")
    private Integer version;

    public Integer getPkey() {
        return this.pkey;
    }

    public Integer getVersion() {
        return this.version;
    }

    @Override
    public String toString() {
        return "Presistable Object: pkey=" + this.pkey + " Object: " + this.getClass().getName();
    }
}

@MappedSuperclass
public class TimeStampedPersistableObject extends PersistableObject {

    @Column(name = "insert_ts")
    @Temporal(TemporalType.TIMESTAMP)
    private Date    insertTimestamp;

    @Column(name = "update_ts")
    @Temporal(TemporalType.TIMESTAMP)
    private Date    updateTimestamp;

    @SuppressWarnings("unused")
    @PrePersist
    private void onInsert() {
        this.insertTimestamp = new Date();
        this.updateTimestamp = this.insertTimestamp;
    }

    @SuppressWarnings("unused")
    @PreUpdate
    private void onUpdate() {
        this.updateTimestamp = new Date();
    }


    public Date getInsertTimestamp() {
        return this.insertTimestamp;
    }


    public Date getUpdateTimestamp() {
        return this.updateTimestamp;
    }
}

这篇关于触发器与 JPA 事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆