4

我正在尝试构建一个 spring (3.1) & hibernate (3.6.10) web 应用程序并且遇到了休眠事件的问题。我实现了自己的 DefaultSaveOrUpdateEventListener 来更新/创建 dateCreated 和 lastUpdated 日期,每个 .saveOrUpdate() 事件都会调用它。到现在为止还挺好。

不应该在 .save() 或 .update() 事件上调用此侦听器吗?或者我在这里错过了什么?

听众:

public class SaveOrUpdateDateListener extends DefaultSaveOrUpdateEventListener {

    private static final long serialVersionUID = 1L;

    private static Logger log = Logger.getLogger(SaveOrUpdateDateListener.class);


    @Override
    public void onSaveOrUpdate(SaveOrUpdateEvent event) {
        log.debug("Entered onSaveOrUpdate()");

        if (event.getObject() instanceof BaseDomain) {
            BaseDomain record = (BaseDomain) event.getObject();

            record.setDateUpdated(new Date());

            if (record.getDateCreated() == null) {
                record.setDateCreated(new Date());
            }
        }

        super.onSaveOrUpdate(event);
    }
}

休眠配置(部分):

<sesssion-factory>

<event type="save-update">
            <listener class="net.test.listener.hibernate.SaveOrUpdateDateListener"/>
            <listener class="org.hibernate.event.def.DefaultSaveOrUpdateEventListener"/>
        </event>    
</session-factory>

示例 dao 方法(不起作用):

@Autowired
private SessionFactory sessionFactory;


@Override
public void add(Registration registration) {
    sessionFactory.getCurrentSession().save(registration);
}

@Override
public void update(Registration registration) {
    sessionFactory.getCurrentSession().update(registration);
}

示例道方法(作品):

@Autowired
private SessionFactory sessionFactory;


@Override
public void add(Registration registration) {
    sessionFactory.getCurrentSession().saveOrUpdate(registration);
}

@Override
public void update(Registration registration) {
    sessionFactory.getCurrentSession().saveOrUpdate(registration);
}



编辑

在我决定一般不使用 saveOrUpdate 之后,我希望通过使用如下基类来解决我的问题:

@MappedSuperclass
public class BaseDomain {

    @Id
    @GeneratedValue
    protected Integer id;

    @Column(nullable=false)
    protected Timestamp dateCreated;

    @Column(updatable = false, insertable = false, columnDefinition = "timestamp default current_timestamp on update current_timestamp")
    @Generated(GenerationTime.ALWAYS)
    protected Timestamp dateUpdated;


    protected BaseDomain() {
        this.dateCreated = new Timestamp(System.currentTimeMillis());
    }

    public Integer getId() {
        return id;
    }

    protected void setId(Integer id) {
        this.id = id;
    }

    public Timestamp getDateCreated() {
        return dateCreated;
    }

    public Date getDateUpdated() {
        return dateUpdated;
    }
}

As the current_timestamp can be used only once per table (mysql restriction) i tried to build the dateCreated via constructor.

4

1 回答 1

6

Save-update events occurs only as a consequence of calling saveOrUpdate. Save-update is not some kind of combinations of save, update. and saveOrUpdate.

In documentation this is spelled following way:

14.2. Event system
...
All the methods of the Session interface correlate to an event.
...
When a request is made of one of these methods, the Hibernate Session generates an appropriate event and passes it to the configured event listeners for that type.

于 2012-06-18T19:09:07.013 回答