您的位置:首页 > 产品设计 > UI/UE

Hibernate逍遥游记-第4章映射对象标识符-increment、identity、hilo、native、assigned、sequence、<meta>

2016-03-19 22:51 609 查看
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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: