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

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

问题描述

我正在使用Spring 3.1.0.RELEASE,JSF 2.x,带有Hibernate Provider,MySql 5.1.x的JPA 2的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事件和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代码中唯一与数据库通信的代码,因此我不必担心时间戳记字段过期。 (如果将来这种情况发生变化,我可以添加触发器并更改我的映射超级calss。)

  • 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侦听器的复杂性就降低了,我不必去在数据库中创建很多触发器,这样我需要维护的东西就更少了。由于我一直在积极开发和更改db结构,因此在快速迭代开发过程时不必去更新触发器。

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_ts update_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天全站免登陆