Hibernate逍遥游记-第4章映射对象标识符-increment、identity、hilo、native、assigned、sequence、<meta>
1.
package mypack; import java.lang.reflect.Constructor;
import org.hibernate.*;
import org.hibernate.cfg.Configuration;
import java.io.*;
import java.sql.Time;
import java.util.*; public class BusinessService{
public static SessionFactory sessionFactory;
static{
try{
Configuration config = new Configuration().configure();
sessionFactory = config.buildSessionFactory();
}catch(RuntimeException e){e.printStackTrace();throw e;}
} public void findAllObjects(String className){
Session session = sessionFactory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
List objects=session.createQuery("from " +className).list();
for (Iterator it = objects.iterator(); it.hasNext();) {
Long id=new Long(0);
if(className.equals("mypack.NativeTester"))
id=((NativeTester) it.next()).getId();
if(className.equals("mypack.IncrementTester"))
id=((IncrementTester) it.next()).getId();
if(className.equals("mypack.IdentityTester"))
id=((IdentityTester) it.next()).getId();
if(className.equals("mypack.HiloTester"))
id=((HiloTester) it.next()).getId(); System.out.println("ID of "+ className+":"+id);
} tx.commit(); }catch (RuntimeException e) {
if (tx != null) {
tx.rollback();
}
throw e;
} finally {
session.close();
}
} public void saveObject(Object object){
Session session = sessionFactory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
session.save(object);
tx.commit(); }catch (RuntimeException e) {
if (tx != null) {
tx.rollback();
}
throw e;
} finally {
session.close();
}
} public void deleteAllObjects(String className){
Session session = sessionFactory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
Query query=session.createQuery("delete from " +className);
query.executeUpdate();
tx.commit(); }catch (RuntimeException e) {
if (tx != null) {
tx.rollback();
}
throw e;
} finally {
session.close();
}
} public void test(String className) throws Exception{
deleteAllObjects(className);
Object o1=Class.forName(className).newInstance();
saveObject(o1);
Object o2=Class.forName(className).newInstance();
saveObject(o2);
Object o3=Class.forName(className).newInstance();
saveObject(o3);
findAllObjects(className); } public static void main(String args[])throws Exception {
String className;
if(args.length==0)
className="mypack.NativeTester";
else
className=args[0];
new BusinessService().test(className); sessionFactory.close();
}
}
2.
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping> <class name="mypack.HiloTester" table="HILO_TESTER"> <id name="id" type="long" column="ID">
<generator class="hilo">
<param name="table">hi_value</param>
<param name="column">next_value</param>
<param name="max_lo">100</param>
</generator>
</id> <property name="name" type="string" column="NAME" /> </class> </hibernate-mapping>
3.
package mypack;
public class HiloTester {
private Long id;
private String name; public HiloTester() {
} public HiloTester(String name) {
this.name = name;
} public Long getId() {
return this.id;
} public void setId(Long id) {
this.id = id;
}
public String getName() {
return this.name;
} public void setName(String name) {
this.name = name;
} }
4.
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping> <class name="mypack.IdentityTester" table="IDENTITY_TESTER"> <id name="id" type="long" column="ID">
<generator class="identity"/>
</id> <property name="name" type="string" column="NAME" /> </class> </hibernate-mapping>
5.
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping> <class name="mypack.IncrementTester" table="INCREMENT_TESTER" > <id name="id" type="long" column="ID">
<meta attribute="scope-set">private</meta>
<generator class="increment"/>
</id> <property name="name" type="string" column="NAME" /> </class> </hibernate-mapping>
6.
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping> <class name="mypack.NativeTester" table="NATIVE_TESTER" > <id name="id" type="long" column="ID">
<generator class="native"/>
</id> <property name="name" type="string" column="NAME" /> </class> </hibernate-mapping>
7.
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE hibernate-configuration
PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="connection.url">
jdbc:mysql://localhost:3306/sampledb
</property>
<property name="connection.username">root</property>
<property name="connection.password">1234</property>
<property name="show_sql">true</property>
<mapping resource="mypack/HiloTester.hbm.xml" />
<mapping resource="mypack/IdentityTester.hbm.xml" />
<mapping resource="mypack/IncrementTester.hbm.xml" />
<mapping resource="mypack/NativeTester.hbm.xml" />
</session-factory>
</hibernate-configuration>
8.
use sampledb; drop table if exists HILO_TESTER;
drop table if exists IDENTITY_TESTER;
drop table if exists INCREMENT_TESTER;
drop table if exists NATIVE_TESTER;
drop table if exists hi_value;
create table HILO_TESTER (ID bigint not null, name varchar(15), primary key (ID));
create table IDENTITY_TESTER (ID bigint not null auto_increment, name varchar(15), primary key (ID));
create table INCREMENT_TESTER (ID bigint not null, NAME varchar(15), primary key (ID));
create table NATIVE_TESTER (ID bigint not null auto_increment, name varchar(15), primary key (ID));
create table hi_value ( next_value integer );
insert into hi_value values ( 0 );
9.
<?xml version="1.0"?>
<project name="Learning Hibernate" default="prepare" basedir="."> <!-- Set up properties containing important project directories -->
<property name="source.root" value="src"/>
<property name="class.root" value="classes"/>
<property name="lib.dir" value="lib"/>
<property name="schema.dir" value="schema"/> <!-- Set up the class path for compilation and execution -->
<path id="project.class.path">
<!-- Include our own classes, of course -->
<pathelement location="${class.root}" />
<!-- Include jars in the project library directory -->
<fileset dir="${lib.dir}">
<include name="*.jar"/>
</fileset>
</path> <!-- Create our runtime subdirectories and copy resources into them -->
<target name="prepare" description="Sets up build structures">
<delete dir="${class.root}"/>
<mkdir dir="${class.root}"/> <!-- Copy our property files and O/R mappings for use at runtime -->
<copy todir="${class.root}" >
<fileset dir="${source.root}" >
<include name="**/*.properties"/>
<include name="**/*.hbm.xml"/>
<include name="**/*.cfg.xml"/>
</fileset>
</copy>
</target> <!-- Compile the java source of the project -->
<target name="compile" depends="prepare"
description="Compiles all Java classes">
<javac srcdir="${source.root}"
destdir="${class.root}"
debug="on"
optimize="off"
deprecation="on">
<classpath refid="project.class.path"/>
</javac>
</target> <target name="run_increment" description="Run a Hibernate sample"
depends="compile">
<java classname="mypack.BusinessService" fork="true">
<arg value="mypack.IncrementTester" />
<classpath refid="project.class.path"/>
</java>
</target> <target name="run_identity" description="Run a Hibernate sample"
depends="compile">
<java classname="mypack.BusinessService" fork="true">
<arg value="mypack.IdentityTester" />
<classpath refid="project.class.path"/>
</java>
</target> <target name="run_hilo" description="Run a Hibernate sample"
depends="compile">
<java classname="mypack.BusinessService" fork="true">
<arg value="mypack.HiloTester" />
<classpath refid="project.class.path"/>
</java>
</target> <target name="run_native" description="Run a Hibernate sample"
depends="compile">
<java classname="mypack.BusinessService" fork="true">
<arg value="mypack.NativeTester" />
<classpath refid="project.class.path"/>
</java>
</target> </project>
Hibernate逍遥游记-第4章映射对象标识符-increment、identity、hilo、native、assigned、sequence、<meta>的更多相关文章
- Hibernate逍遥游记-第13章 映射实体关联关系-006双向多对多(分解为一对多)
1. 2. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate ...
- Hibernate逍遥游记-第13章 映射实体关联关系-005双向多对多(使用组件类集合\<composite-element>\)
1. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hi ...
- Hibernate逍遥游记-第13章 映射实体关联关系-004双向多对多(inverse="true")
1. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hi ...
- Hibernate逍遥游记-第13章 映射实体关联关系-003单向多对多
0. 1. drop database if exists SAMPLEDB; create database SAMPLEDB; use SAMPLEDB; create table MONKEYS ...
- Hibernate逍遥游记-第13章 映射实体关联关系-002用主键映射一对一(<one-to-one constrained="true">、<generator class="foreign">)
1. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hi ...
- Hibernate逍遥游记-第13章 映射实体关联关系-001用外键映射一对一(<many-to-one unique="true">、<one-to-one>)
1. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hi ...
- Hibernate逍遥游记-第12章 映射值类型集合-005对集合排序Map(<order-by>\<sort>)
1. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hi ...
- Hibernate逍遥游记-第12章 映射值类型集合-005对集合排序(<order-by>\<sort>)
1. 2. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate ...
- Hibernate逍遥游记-第12章 映射值类型集合-004映射Map(<map-key>)
1. 2. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate ...
随机推荐
- 使用IO流创建文件并写入数据
/* 字符流和字节流: 字节流两个基类: InputStream OutputStream 字符流两个基类: Reader Writer 既然IO流是用于操作数据的, 那么数据的最常见体现形式是:文件 ...
- static方法不能直接访问类内的非static变量和不能调用this,super语句分析
大家都知道在static方法中,不能访问类内非static成员变量和方法.可是原因是什么呢? 这首先要从static方法的特性说起.static方法,即类的静态成员经常被称为"成员变量&qu ...
- jetty 8.x, 9.x无法加载jstl的PWC6188问题
参考: cannot load JSTL taglib within embedded Jetty server:http://stackoverflow.com/questions/2151075/ ...
- BLOB或TEXT字段使用散列值和前缀索引优化提高查询速度
1.创建表,存储引擎为myisam,对大文本字段blob使用MD5函数建立一个散列值 create table t2(id varchar(60), content blob, hash_value ...
- VBS基础篇 - class
Class 语句:声明一个类的名称,以及组成该类的变量.属性和方法的定义. Class name '参数name必选项,Class 的名称 statements '一个或多个语句,定义了 Class ...
- 历时一周,unity3d+xtion打造我的第一个休闲体感小游戏《空降奇兵》
1.游戏介绍 本游戏属于休闲小游戏,主要操作如下: 菜单控制:举起左手或右手,点击左边或者右边的菜单:挥动左手或右手,选择关卡: 操作方式:玩家跳跃,游戏中的伞兵从飞机开始降落:玩家通过控制伞兵的左右 ...
- android中设置Animation 动画效果
在 Android 中, Animation 动画效果的实现可以通过两种方式进行实现,一种是 tweened animation 渐变动画,另一种是 frame by frame animation ...
- SqlBulkCopy批量写入25万条数据只需3s
Microsoft SQL Server 提供一个称为 bcp 的流行的命令提示符实用工具,用于将数据从一个表移动到另一个表(表既可以在同一个服务器上,也可以在不同服务器上).SqlBulkCopy ...
- 在windows 7搭建xcode开发环境
前言:本文适用于没有ios开发环境(如无iphone,mac等),在windows上搭建出ios的开发环境.值得注意的是,在windows搭建的ios开发环境只能做开发测试,无法发布产品.若需要发布产 ...
- javascript_22_for_js性感的v
<script type="text/javascript"> window.onload=function(){ var aDiv=document.getEleme ...