- 浏览: 114317 次
- 性别:
- 来自: 宁波
文章分类
最新评论
通过在表中及POJO中增加一个Timestamp字段来表示记录的最后更新时间,来达到多用户同时更改一条数据的冲突,这个timestamp由数据库自动添加,无需人工干预 数据库结构: pojos: Student.hbm.xml Hibernate.cfg.xml 测试代码: 测试结果:
import java.util.Date;
public class Student {
private String id;
private String name;
private Date lastUpdateDateTime;
public String getId() {
return id;
}
public Date getLastUpdateDateTime() {
return lastUpdateDateTime;
}
public void setLastUpdateDateTime(Date lastUpdateDateTime) {
this.lastUpdateDateTime = lastUpdateDateTime;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse - Hibernate Tools
-->
<hibernate-mapping>
<class name="Timestamp.Student" table="studentTimestamp" >
<id name="id" unsaved-value="null">
<generator class="uuid.hex"></generator>
</id>
<!-- timestamp标签必须跟在id标签后面 -->
<timestamp name="lastUpdateDateTime" column="lastUpdateDateTime"></timestamp>
<property name="name" type="string" column="name"></property>
</class>
</hibernate-mapping>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="connection.username">root</property>
<property name="connection.url">
jdbc:mysql://localhost:3306/schoolproject?characterEncoding=gb2312&useUnicode=true
</property>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="myeclipse.connection.profile">mysql</property>
<property name="connection.password">1234</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="hibernate.show_sql">true</property>
<property name="current_session_context_class">thread</property>
<property name="jdbc.batch_size">15</property>
<mapping resource="Timestamp/Student.hbm.xml" />
</session-factory>
</hibernate-configuration>
import java.io.File;
import java.util.Iterator;
import java.util.Set;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class Test {
public static void main(String[] args) {
String filePath=System.getProperty("user.dir")+File.separator+"src/Timestamp"+File.separator+"hibernate.cfg.xml";
File file=new File(filePath);
System.out.println(filePath);
SessionFactory sessionFactory=new Configuration().configure(file).buildSessionFactory();
Session session=sessionFactory.openSession();
Transaction t=session.beginTransaction();
Student stu=new Student();
stu.setName("tom11");
session.save(stu);
t.commit();
/*
* 模拟多个session操作student数据表
*/
Session session1=sessionFactory.openSession();
Session session2=sessionFactory.openSession();
Student stu1=(Student)session1.createQuery("from Student s where s.name='tom11'").uniqueResult();
Student stu2=(Student)session2.createQuery("from Student s where s.name='tom11'").uniqueResult();
//这时候,两个版本号是相同的
System.out.println("v1="+stu1.getLastUpdateDateTime()+"--v2="+stu2.getLastUpdateDateTime());
Transaction tx1=session1.beginTransaction();
stu1.setName("session1");
tx1.commit();
//这时候,两个版本号是不同的,其中一个的版本号递增了
System.out.println("v1="+stu1.getLastUpdateDateTime()+"--v2="+stu2.getLastUpdateDateTime());
Transaction tx2=session2.beginTransaction();
stu2.setName("session2");
tx2.commit();
}
}
Hibernate: insert into studentTimestamp (lastUpdateDateTime, name, id) values (?, ?, ?)
Hibernate: select student0_.id as id0_, student0_.lastUpdateDateTime as lastUpda2_0_, student0_.name as name0_ from studentTimestamp student0_ where student0_.name='tom11'
Hibernate: select student0_.id as id0_, student0_.lastUpdateDateTime as lastUpda2_0_, student0_.name as name0_ from studentTimestamp student0_ where student0_.name='tom11'
v1=2007-12-12 16:30:53.0--v2=2007-12-12 16:30:53.0
Hibernate: update studentTimestamp set lastUpdateDateTime=?, name=? where id=? and lastUpdateDateTime=?
Exception in thread "main" org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect):
v1=2007-12-12 16:30:54.844--v2=2007-12-12 16:30:53.0
Hibernate: update studentTimestamp set lastUpdateDateTime=?, name=? where id=? and lastUpdateDateTime=?
发表评论
-
Open Session In View(转)
2010-12-04 21:58 1068英文原文:http://community.jboss.org ... -
open session in view 研究
2010-12-04 21:54 747在没有使用Spring提供的Open Session In V ... -
Hibernate并发控制
2010-12-04 19:46 10063. 多个事务并发引起的问题:3.1. 第 ... -
servlet和filter的异同
2010-12-04 19:28 952以前总以为filter就是一种特殊servlet,所以他们 ... -
JavaScript,调用函数的5种方法
2010-11-28 16:16 878JavaScript,调用函数的5种方法 一次又一次的,我发现 ... -
IOC原理
2010-11-28 09:18 828IOC(inview of control)控制反转 ... -
Hibernate级联操作Cascade学之---delete
2010-11-27 10:06 938所在cascade,就是说我在更新一方的时候,可以根据这一 ... -
HTTP POST GET 本质区别详解
2010-11-27 09:47 827HTTP POST GET 本质区别详解 收藏 ... -
Hibernate二级缓存(转)
2010-11-27 09:38 766Hibernate二级缓存(转 ... -
加速你的Hibernate引擎(下)
2010-11-27 09:37 770加速你的Hibernate引擎(下) 文章分类:Ja ... -
加速你的Hibernate引擎(上)
2010-11-27 09:36 844加速你的Hibernate引擎(上) 文章分类:Ja ... -
spring Transaction Manager和hibernate session
2010-11-27 09:33 1038spring Transaction Manager ... -
浅析Spring提供的事务管理方法
2010-11-27 09:32 780浅析Spring提供的事务管理方法 2006-0 ... -
hibernate性能优化
2010-11-27 09:24 633转 hibernate性能优化 ... -
Hibernate 性能优化技巧
2010-11-27 09:23 724转 Hibernate 性能优化 ... -
Hibernate乐观锁实现之Version
2010-11-27 09:21 802转 Hibernate乐观锁实现之Version ... -
Hibernate悲观锁定与乐观锁定区别
2010-11-27 09:20 673转 Hibernate悲观锁定 ... -
Hibernate二级缓存 ---- 最佳实践
2010-11-27 09:19 878转 Hibernate二级缓存 ---- 最佳实践 ... -
Spring如何与struts2结合
2010-11-22 12:34 9304.Spring如何与struts2结合 关键字: ... -
struts2 iterator
2010-11-21 21:12 894struts2的s:iterator 可以遍历 数据栈里面 ...
相关推荐
TimeStamp(用java实现时间戳)
API接口设计之token、timestamp、sign的具体使用demo示例。
Hibernate 注解 annotation 超级详细
主要介绍了MySQL 5.6 中的 TIMESTAMP 和 explicit_defaults_for_timestamp 参数,需要的朋友可以参考下
ICMP timestamp请求响应漏洞 修复 Traceroute探测漏洞 修复 使用firewall-cmd打开关闭防火墙与端口 linux 7 ICMP timestamp请求响应漏洞 修复 Traceroute探测漏洞 修复 使用firewall-cmd打开关闭防火墙与端口 linux ...
文章目录Timestamptimestamp.go如何使用 Timestamp path: google/protobuf/timestamp.proto 在 timestamppb 中 Timestamp 包含两个字段 seconds 表示秒 nanos 表示纳秒 message Timestamp { int64 seconds = 1; ...
11.3. 乐观并发控制(Optimistic concurrency control) 11.3.1. 应用程序级别的版本检查(Application version checking) 11.3.2. 扩展周期的session和自动版本化 11.3.3. 脱管对象(deatched object)和自动版本化 ...
oracle中TIMESTAMP与DATE比较
11.3. 乐观并发控制(Optimistic concurrency control) 11.3.1. 应用程序级别的版本检查(Application version checking) 11.3.2. 扩展周期的session和自动版本化 11.3.3. 脱管对象(deatched object)和自动版本化 ...
11.3. 乐观并发控制(Optimistic concurrency control) 11.3.1. 应用程序级别的版本检查(Application version checking) 11.3.2. 扩展周期的session和自动版本化 11.3.3. 脱管对象(deatched object)和自动版本化 ...
NULL 博文链接:https://zhengkaifl.iteye.com/blog/734139
开始Hibernate之旅 1.2. 第一个可持久化类 1.3. 映射cat 1.4. 与猫同乐 1.5. 结语 2. 体系结构 2.1. 总览 2.2. JMX集成 2.3. JCA支持 3. SessionFactory配置 3.1. 可编程配置方式...
@Version 注解用于支持乐观锁版本控制。 @Entity public class Flight implements Serializable { ... @Version @Column(name="OPTLOCK") public Integer getVersion() { ... } } version属性映射到 "OPTLOCK...
hibernate@注解方式配置实体类时,利用javadoc接口生成数据库表及字段的注释说明,支持oracle、sqlserver、db2、mysql数据库。因用到java\lib\tools.jar,需要将该jar放入工程lib下(或者tomcat\lib下、或加入...
11.3. 乐观并发控制(Optimistic concurrency control) 11.3.1. 应用程序级别的版本检查(Application version checking) 11.3.2. 扩展周期的session和自动版本化 11.3.3. 脱管对象(deatched object)和自动版本化 ...
11.3. 乐观并发控制(Optimistic concurrency control) 11.3.1. 应用程序级别的版本检查(Application version checking) 11.3.2. 扩展周期的session和自动版本化 11.3.3. 脱管对象(deatched object)和自动版本化 ...
1.1. 开始Hibernate之旅 1.2. 第一个持久化类 1.3. 映射cat 1.4. 与Cat同乐 1.5. 结语 2. 架构(Architecture) 2.1. 概况(Overview) 2.2. 实例状态 2.3. JMX整合 2.4. 对JCA的支持 3. 配置 3.1. 可编程的配置方式 ...
oracle --timestamporacle --timestamporacle --timestamporacle --timestamporacle --timestamporacle --timestamporacle --timestamporacle --timestamp
oracle timestamp详解 将常用的转换方法及使用注意事项都罗列出来了
1.1. 开始Hibernate之旅 1.2. 第一个持久化类 1.3. 映射cat 1.4. 与Cat同乐 1.5. 结语 2. Hibernate入门 2.1. 前言 2.2. 第一部分 - 第一个Hibernate程序 2.2.1. 第一个class 2.2.2. 映射文件 2.2.3. ...