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 ...
随机推荐
- 下拉刷新--第三方开源--PullToRefresh
效果预览图: 下载地址:https://github.com/chrisbanes/Android-PullToRefresh activity_main.xml: <RelativeLayou ...
- Spark菜鸟学习营Day3 RDD编程进阶
Spark菜鸟学习营Day3 RDD编程进阶 RDD代码简化 对于昨天练习的代码,我们可以从几个方面来简化: 使用fluent风格写法,可以减少对于中间变量的定义. 使用lambda表示式来替换对象写 ...
- Oracle 摘去数据块的面纱
Offset 0 1 2 3 4 5 6 7 8 9 A B C D E F 00018000h 6 A2 0 0 0c 0 80 3 8b 61 15 0 0 0 3 4 type frmt spa ...
- ActiveMQ之Topic
与Queue不同,Topic实现的是发布/订阅模型,在下面的例子中,启动两个消费者共同监听一个Topic,然后循环给这个Topic发送多个消息. 例子: public class TopicTest ...
- 【rest】 深入理解rest
起因是想搞明白 ajax.rest风格和http请求数据会有什么区别 再来回顾一下概念: REST即表述性 状态 传递 满足这些约束条件和原则的应用程序或设计就是RESTful.需要注意的是,REST ...
- 【环境】Linux下连接无线网常用命令
启用/重启/关闭 网络服务 /etc/init.d/networking start /etc/init.d/networking restart /etc/init.d/networking sto ...
- SVN 安装配置
1,软件下载 到官方网站的下载二进制安装文件,来到二进制包下载部分,找到 Windows NT, 2000, XP and 2003部分,然后选择Apache 2.2 或者 Apache 2.4,这样 ...
- Matlab中@与函数调用
function m f=@(x) x^2; y(f,3); function y(f,x) disp(num2str(f(x))); end end 函数调用另一个函数的时候,把另一个函数名作为参数 ...
- C++字符串分割
//字符串分割函数 std::vector<std::string> split(std::string str,std::string pattern) { std::string::s ...
- select模式
在很多比较各种网络模型的文章中,但凡提到select模型时,都会说select受限于轮询的套接字数量,这个 数量也就是系统头文件中定义的FD_SETSIZE值(例如64).但事实上这个算不上真的限制. ...