如何在休眠中进行乐观锁

如何在休眠中进行乐观锁

本文介绍了如何在休眠中进行乐观锁定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Hibernate和Spring是全新的,并且在尝试学习Spring,Hibernate,Maven等方面我只知道如何运行hello world的例子。根据我的基本理解,我已经分配了执行乐观锁定的任务。至于我GOOGLE了它,我只能看到它并不是非常困难,我需要的是在我的映射类中添加一个版本标记在我的xml和整数变量版本中。像这样...

  public class MyClass {
...
private int version;
...
}

我的xml应该是这样的

 < class name =MyClass> 
< id ...>
< version name =versioncolumn =VERSIONaccess =field>
...
< / class>

当第二个用户保存时,hibernate会自动处理版本控制,hibernate发现这个用户正在处理陈旧的数据并抛出StaleObjectException。



只是想确认我的理解,提前致谢。



真的很有帮助,如果有人能指出我喜欢这个世界的例子。



我还想提到,我正在尝试实现最后提交胜利scenerio

解决方案

我使用了Hibernate注释,这里是我对乐观锁的实现

 @Entity 
public class MyObject {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

私有字符串数据;

@版本
私人整数版本; //这是版本字段
}

这里是工作示例

  //带版本字段的实体类
@Entity
公共类Ent1实现可序列化{

private static final long serialVersionUID = -5580880562659281420L;

@Id
整数a1;

整数a2;

@版本
私人整数版本;
}

和一些代码添加一个元素到DB

  session = HibernateHelper.getSessionFactory()。openSession(); 
transaction = session.beginTransaction();
Ent1 entity = new Ent1();
entity.setA1(new Integer(0));
entity.setA2(new Integer(1));
session.save(entity);
transaction.commit();

//获取保存对象并修改它
transaction = session.beginTransaction();
列表< Ent1> list =(List< Ent1>)session.createQuery(FROM Ent1 WHERE a1 = 0)。
Ent1 ent = list.get(0);
ent.setA2(new Integer(1000));
session.save(ent);
transaction.commit();

创建完成后,数据库中的新元素具有版本0.修改版本1后。


$ b HibernateHelper.java

  import org.hibernate。 SessionFactory的; 
import org.hibernate.cfg.AnnotationConfiguration;

public class HibernateHelper {

private static final SessionFactory sessionFactory;

static {
try {
sessionFactory = new AnnotationConfiguration()。configure()。buildSessionFactory();
} catch(Throwable ex){
System.err.println(Initial SessionFactory creation failed。+ ex);
抛出新的ExceptionInInitializerError(ex);



public static SessionFactory getSessionFactory(){
return sessionFactory;
}
}


I am completely new to Hibernate and Spring and in my attempt to learn Spring, Hibernate, Maven etc I only know how to run a hello world example using all of the three. With my basic understanding I have been assigned a task for performing optimistic locking. As far as I have googled it I can only see it is not very difficult all I need is to add a version tag in my xml and integer variable version in my mapped class.. Like this...

public class MyClass {
...
private int version;
...
}

my xml should be like this

<class name="MyClass">
<id ...>
<version name="version" column="VERSION" access="field">
...
</class>

And hibernate will automatically take care of versioning when second user saves, hibernate finds this user is working on the stale data and throws StaleObjectException.

Just wanted to confirm my understanding, thanks in advance.

It will be really helpful if some one can point me to a hello world example for this.

I would also like to mention that I am trying to implement "last commit wins" scenerio

解决方案

I used Hibernate annotations and here is my implementation of optimistic locking

@Entity
public class MyObject {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    private String data;

    @Version
    private Integer version; // this is version field
}

Here is working example

// Entity class with version field
@Entity
public class Ent1 implements Serializable {

    private static final long serialVersionUID = -5580880562659281420L;

    @Id
    Integer a1;

    Integer a2;

    @Version
    private Integer version;
}

And some code to add one element to DB

        session = HibernateHelper.getSessionFactory().openSession();
        transaction = session.beginTransaction();
        Ent1 entity = new Ent1();
        entity.setA1(new Integer(0));
        entity.setA2(new Integer(1));
        session.save(entity);
        transaction.commit();

        // get saved object and modify it
        transaction = session.beginTransaction();
        List<Ent1> list = (List<Ent1>)session.createQuery("FROM Ent1 WHERE a1 = 0").list();
        Ent1 ent = list.get(0);
        ent.setA2(new Integer(1000));
        session.save(ent);
        transaction.commit();

After creation, new element in DB has version 0. After modifying - version 1.

HibernateHelper.java

import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;

public class HibernateHelper {

    private static final SessionFactory sessionFactory;

    static {
        try {
            sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
        } catch (Throwable ex) {
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }
}

这篇关于如何在休眠中进行乐观锁定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 04:40