java 如何在休眠中进行乐观锁定
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6909035/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to do optimistic locking in hibernate
提问by Anupam Gupta
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...
我对 Hibernate 和 Spring 完全陌生,在尝试学习 Spring、Hibernate、Maven 等时,我只知道如何使用所有这三个来运行 hello world 示例。根据我的基本理解,我被分配了执行乐观锁定的任务。就我用谷歌搜索而言,我只能看到这并不是很困难,我只需要在我的 xml 中添加一个版本标签,并在我的映射类中添加一个整数变量版本..像这样......
public class MyClass {
...
private int version;
...
}
my xml should be like this
我的 xml 应该是这样的
<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.
当第二个用户保存时,hibernate 将自动处理版本控制,hibernate 发现该用户正在处理陈旧数据并抛出 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.
如果有人可以为此指出一个 hello world 示例,那将非常有帮助。
I would also like to mention that I am trying to implement "last commit wins" scenerio
我还想提一下,我正在尝试实施“最后一次提交获胜”的场景
回答by Sergey Galchenko
I used Hibernate annotations and here is my implementation of optimistic locking
我使用了 Hibernate 注释,这是我对乐观锁定的实现
@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
以及一些将一个元素添加到 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.
创建后,DB 中的新元素的版本为 0。修改后 - 版本 1。
HibernateHelper.java
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;
}
}
回答by Sonu
If we are using the xml style we can use as below in the hbm file:
如果我们使用 xml 样式,我们可以在 hbm 文件中使用如下:
<id name="productId" column="pid" />
**<version name="v" column="ver" />**
<property name="proName" column="pname" length="10"/>