问题描述
我是JPA和Hibernate的新手,我对乐观锁定有疑问.我有一个拥有@Version注释字段的类.当我更新由此类表示的Entity时,版本计数器不会增加.这是我的代码:班级:
@Entity
@Table (name = "Studenten")
public class Student implements Serializable{
private static final long serialVersionUID = 705252921575133272L;
@Version
private int version;
private int matrnr;
private String name;
private int semester;
public Student (){
}
public Student (int matrnr, String name){
this.matrnr = matrnr;
this.name = name;
}
public Student (int matrnr, String name, int semester){
this(matrnr, name);
this.semester = semester;
}
这是主要方法:
public static void main(String[] args) {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("VEDA_Vortrag");
EntityManager em = emf.createEntityManager();
EntityTransaction tx = em.getTransaction();
try{
tx.begin();
Student s = em.find(Student.class, 195948);
s.setSemester(1);
em.persist(s);
tx.commit();
}catch(Exception e){
if(tx != null && tx.isActive()){
tx.rollback();
System.out.println("Error in Transaction. Rollback!");
}
}
finally{
em.close();
emf.close();
}
}
这是控制台所说的:
Hibernate:
select
student0_.matrnr as matrnr0_0_,
student0_.name as name0_0_,
student0_.semester as semester0_0_,
student0_.version as version0_0_
from
Studenten student0_
where
student0_.matrnr=?
Hibernate:
update
Studenten
set
name=?,
semester=?,
version=?
where
matrnr=?
有人可以告诉我怎么了吗?
修改:好的,我尝试了一些东西.我已将锁定设置为LockModeType.OPTIMISTIC_FORCE_INCREMENT并收到此错误消息:
ERROR: HHH000099: an assertion failure occured (this may indicate a bug in Hibernate, but is more likely due to unsafe use of the session): org.hibernate.AssertionFailure: cannot force version increment on non-versioned entity
Jun 20, 2014 9:00:52 AM org.hibernate.AssertionFailure <init>
因此,很明显我有一个非版本化的实体.但为什么?我的课堂上有@Version注释.
修改:我开始认为问题出在persistence.xml中.在这里:
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
<persistence-unit name="VEDA_Vortrag">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/uni"/>
<property name="javax.persistence.jdbc.user" value="*******"/>
<property name="javax.persistence.jdbc.password" value="******"/>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
</properties>
</persistence-unit>
有什么问题吗?
正如我在中解释的那样本文,entityManager.persist()
方法只能用于以前从未保存过的新实体.
由于您要获取一个实体,因此无论如何都不需要调用持久或合并. 脏检查将代表您进行更新. >
无论如何,提交都会触发刷新,因此您应该会看到更新.
请确保您使用的是javax.persistence.Version
批注,而不是其他软件包(spring数据或类似内容)中的
I'm new to JPA and Hibernate and I have a problem with optimistic locking. I have a class wich hase an @Version annotated field. When I update the Entity represented by this class, the version counter does not increase. Here is my code:The class:
@Entity
@Table (name = "Studenten")
public class Student implements Serializable{
private static final long serialVersionUID = 705252921575133272L;
@Version
private int version;
private int matrnr;
private String name;
private int semester;
public Student (){
}
public Student (int matrnr, String name){
this.matrnr = matrnr;
this.name = name;
}
public Student (int matrnr, String name, int semester){
this(matrnr, name);
this.semester = semester;
}
and here is the main method:
public static void main(String[] args) {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("VEDA_Vortrag");
EntityManager em = emf.createEntityManager();
EntityTransaction tx = em.getTransaction();
try{
tx.begin();
Student s = em.find(Student.class, 195948);
s.setSemester(1);
em.persist(s);
tx.commit();
}catch(Exception e){
if(tx != null && tx.isActive()){
tx.rollback();
System.out.println("Error in Transaction. Rollback!");
}
}
finally{
em.close();
emf.close();
}
}
and here is what the console says:
Hibernate:
select
student0_.matrnr as matrnr0_0_,
student0_.name as name0_0_,
student0_.semester as semester0_0_,
student0_.version as version0_0_
from
Studenten student0_
where
student0_.matrnr=?
Hibernate:
update
Studenten
set
name=?,
semester=?,
version=?
where
matrnr=?
Can somebody tell me what's wrong?
Edit:ok, I have tried something. I've set the locking to LockModeType.OPTIMISTIC_FORCE_INCREMENT and got this Error-Message:
ERROR: HHH000099: an assertion failure occured (this may indicate a bug in Hibernate, but is more likely due to unsafe use of the session): org.hibernate.AssertionFailure: cannot force version increment on non-versioned entity
Jun 20, 2014 9:00:52 AM org.hibernate.AssertionFailure <init>
So it seems clear that I have a non-versioned entity. But why? I have the @Version Annotation in my class.
Edit:I begin to think that the problem is in the persistence.xml. Here it is:
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
<persistence-unit name="VEDA_Vortrag">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/uni"/>
<property name="javax.persistence.jdbc.user" value="*******"/>
<property name="javax.persistence.jdbc.password" value="******"/>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
</properties>
</persistence-unit>
Is there anything wrong?
As I explained in this article, The entityManager.persist()
method is meant to be used only for new entities that have never been persisted before.
Because you are fetching an entity you don't need to call persist or merge anyway. The dirty checking will do the update on your behalf.
The commit will trigger the flush anyway so you should see the update.
Make sure you use the javax.persistence.Version
annotation and not from other package (spring data or something similar).
这篇关于JPA @Version字段未递增的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!