手动更新休眠版本 [英] Updating hibernate version manually

查看:82
本文介绍了手动更新休眠版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个类,Foo和Bar被映射为@OneToOne(双向),使用Hibernate(3.6.1 final)和JPA(2.0),如 -

  @Entity 
public class Foo {
@Id
private Long id;

@OneToOne(cascade = CascadeType.ALL,mappedBy =foo)
私人酒吧;

@OneToOne(cascade = CascadeType.ALL,mappedBy =foo)
private quux qux;

@版本
private int version;

// getters和setters省略
}

@Entity
公共类Bar {
@Id
private Long id ;

@OneToOne
@JoinColumn(name =foo_id,nullable = false)
private Foo foo;

// getters和setters省略
}

@实体
公共类Qux {
@Id
私有长ID ;

@OneToOne
@JoinColumn(name =foo_id,nullable = false)
private Foo foo;

// getters和setters省略
}

请注意,Bar和Qux没有 @Version



如果我们更新Bar那么hibernate将不会增加Foo的版本,对于Qux也是如此。但是我们的业务逻辑需要 - 如果有人更新Foo中的Bar,而其他线程正在尝试更新同一个Foo的Qux,但没有更新Bar,反之亦然,则此类更新应该失败。

Since如果我们更新Bar,hibernate不会更新Foo的版本属性,但是如果我们更新Bar和Qux,我们决定手动更新Foo的版本(我知道这很奇怪,并且不推荐)。

它工作得很好..但我担心一些并发的角落案例可能会失败,或者会有意想不到的行为。

为了这个目的对版本进行这种调整是否安全?或者有没有其他更好的替代方案(我已经尝试过乐观/悲观的力量增量)

解决方案

正确的方法强制版本更新如下:

  em.lock(entity,LockModeType.OPTIMISTIC_FORCE_INCREMENT); 

打算在这种情况下使用。



也可以使用 LockModeType 的其他方法 EntityManager


I have two classes say Foo and Bar mapped as @OneToOne (bidirectional) using Hibernate (3.6.1 final) with JPA (2.0) like -

@Entity
public class Foo{
    @Id
    private Long id;

    @OneToOne(cascade = CascadeType.ALL, mappedBy = "foo")
    private Bar bar;

    @OneToOne(cascade = CascadeType.ALL, mappedBy = "foo")
    private Qux qux;

    @Version
    private int version;

    // getters and setters omitted
}

@Entity
public class Bar{
    @Id
    private Long id;

    @OneToOne
        @JoinColumn(name = "foo_id",  nullable = false)
    private Foo foo;

    // getters and setters omitted
}

@Entity
public class Qux {
    @Id
    private Long id;

    @OneToOne
        @JoinColumn(name = "foo_id",  nullable = false)
    private Foo foo;

    // getters and setters omitted
}

Note that - Bar and Qux doesn't have @Version column

If we update Bar then hibernate will not increment the version of Foo and same for Qux. But our business logic needs - if someone updates Bar in the Foo and other thread is trying to update the Qux of the same Foo but doesn't have updated Bar and vice versa then such updates should fail.
Since hibernate doesn't update the version property of Foo if we update the Bar, we decided to update the version of Foo manually (I know it's pretty weird and not recommended) if we update the Bar and Qux.
It works perfectly fine.. but I'm worry about some corner cases in concurrency which may fail this or will have unintended behavior.
Is it safe to do this kind of tweak with version for this purpose? OR is there any other better alternative to this (I've already tried optimistic/pessimistic force increament)

解决方案

The correct way to force version update is the following:

em.lock(entity, LockModeType.OPTIMISTIC_FORCE_INCREMENT);

It's intended to be used in such cases.

Other methods of EntityManager that take LockModeType can be used as well.

这篇关于手动更新休眠版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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