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 ...
随机推荐
- mongodb的常用操作
对于nosql之前工作中有用到bekerlydb,最近开始了解mongodb,先简单写下mongodb的一些常用操作,当是个总结: 1.mongodb使用数据库(database)和集合(collec ...
- javascript 创建对象及对象原型链属性介绍
我们知道javascript里定义一个普通对象的方法,如: let obj = {}; obj.num = 1; obj.string = 'string'; obj.func = function( ...
- Jquery 在动态元素上绑定事件
弄了很久却没有弄出来,感觉没有错,但是动态元素上的事件根本就不响应,代码如下: <input type="button" id="btnyes" valu ...
- Linq to Entities
首先要添加一个ADO.NET实体数据模型 添加一个Entities 对象,其用法和linqtosql类似例如: StudentInfoEntities2 entity = new StudentInf ...
- 1103. Integer Factorization (30)
The K-P factorization of a positive integer N is to write N as the sum of the P-th power of K positi ...
- poj 3237 Tree 树链剖分+线段树
Description You are given a tree with N nodes. The tree’s nodes are numbered 1 through N and its edg ...
- MySQL 5.7.9的多源复制
什么是多源复制? 首先,我们需要清楚 multi-master 与multi-source 复制不是一样的. Multi-Master 复制通常是环形复制,你可以在任意主机上将数据复制给其他主机. M ...
- 项目文件包含 ToolsVersion="12.0" 设置,而此版本的 MSBuild 不支持该工具版本
解决方法: 右键点击你的项目,选择属性,再点击配置属性中的常规,常规中有个平台工作集,把V120改成V100,点击应用即可.
- 在IOS中使用json
1.从https://github.com/stig/json-framework/中下载json框架:json-framework 2.解压下载的包,将class文件夹下的所有文件导入到当前工程下. ...
- 【BZOJ 1090】[SCOI2003]字符串折叠
Description 折 叠的定义如下: 1. 一个字符串可以看成它自身的折叠.记作S S 2. X(S)是X(X>1)个S连接在一起的串的折叠.记作X(S) SSSS…S(X个S). ...