映射对象标识符-incrementidentityhilonativeassignedsequence<meta>
Posted shamgod
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了映射对象标识符-incrementidentityhilonativeassignedsequence<meta>相关的知识,希望对你有一定的参考价值。
1.
1 package mypack; 2 3 import java.lang.reflect.Constructor; 4 import org.hibernate.*; 5 import org.hibernate.cfg.Configuration; 6 import java.io.*; 7 import java.sql.Time; 8 import java.util.*; 9 10 public class BusinessService{ 11 public static SessionFactory sessionFactory; 12 static{ 13 try{ 14 Configuration config = new Configuration().configure(); 15 sessionFactory = config.buildSessionFactory(); 16 }catch(RuntimeException e){e.printStackTrace();throw e;} 17 } 18 19 public void findAllObjects(String className){ 20 Session session = sessionFactory.openSession(); 21 Transaction tx = null; 22 try { 23 tx = session.beginTransaction(); 24 List objects=session.createQuery("from " +className).list(); 25 for (Iterator it = objects.iterator(); it.hasNext();) { 26 Long id=new Long(0); 27 if(className.equals("mypack.NativeTester")) 28 id=((NativeTester) it.next()).getId(); 29 if(className.equals("mypack.IncrementTester")) 30 id=((IncrementTester) it.next()).getId(); 31 if(className.equals("mypack.IdentityTester")) 32 id=((IdentityTester) it.next()).getId(); 33 if(className.equals("mypack.HiloTester")) 34 id=((HiloTester) it.next()).getId(); 35 36 System.out.println("ID of "+ className+":"+id); 37 } 38 39 tx.commit(); 40 41 }catch (RuntimeException e) { 42 if (tx != null) { 43 tx.rollback(); 44 } 45 throw e; 46 } finally { 47 session.close(); 48 } 49 } 50 51 public void saveObject(Object object){ 52 Session session = sessionFactory.openSession(); 53 Transaction tx = null; 54 try { 55 tx = session.beginTransaction(); 56 session.save(object); 57 tx.commit(); 58 59 }catch (RuntimeException e) { 60 if (tx != null) { 61 tx.rollback(); 62 } 63 throw e; 64 } finally { 65 session.close(); 66 } 67 } 68 69 public void deleteAllObjects(String className){ 70 Session session = sessionFactory.openSession(); 71 Transaction tx = null; 72 try { 73 tx = session.beginTransaction(); 74 Query query=session.createQuery("delete from " +className); 75 query.executeUpdate(); 76 tx.commit(); 77 78 }catch (RuntimeException e) { 79 if (tx != null) { 80 tx.rollback(); 81 } 82 throw e; 83 } finally { 84 session.close(); 85 } 86 } 87 88 public void test(String className) throws Exception{ 89 deleteAllObjects(className); 90 Object o1=Class.forName(className).newInstance(); 91 saveObject(o1); 92 Object o2=Class.forName(className).newInstance(); 93 saveObject(o2); 94 Object o3=Class.forName(className).newInstance(); 95 saveObject(o3); 96 findAllObjects(className); 97 98 } 99 100 public static void main(String args[])throws Exception { 101 String className; 102 if(args.length==0) 103 className="mypack.NativeTester"; 104 else 105 className=args[0]; 106 new BusinessService().test(className); 107 108 sessionFactory.close(); 109 } 110 }
2.
1 <?xml version="1.0"?> 2 <!DOCTYPE hibernate-mapping 3 PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 4 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 5 <hibernate-mapping> 6 7 <class name="mypack.HiloTester" table="HILO_TESTER"> 8 9 <id name="id" type="long" column="ID"> 10 <generator class="hilo"> 11 <param name="table">hi_value</param> 12 <param name="column">next_value</param> 13 <param name="max_lo">100</param> 14 </generator> 15 </id> 16 17 <property name="name" type="string" column="NAME" /> 18 19 </class> 20 21 </hibernate-mapping>
3.
1 package mypack; 2 public class HiloTester { 3 private Long id; 4 private String name; 5 6 public HiloTester() { 7 } 8 9 public HiloTester(String name) { 10 this.name = name; 11 } 12 13 public Long getId() { 14 return this.id; 15 } 16 17 public void setId(Long id) { 18 this.id = id; 19 } 20 public String getName() { 21 return this.name; 22 } 23 24 public void setName(String name) { 25 this.name = name; 26 } 27 28 29 30 31 }
4.
1 <?xml version="1.0"?> 2 <!DOCTYPE hibernate-mapping 3 PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 4 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 5 <hibernate-mapping> 6 7 <class name="mypack.IdentityTester" table="IDENTITY_TESTER"> 8 9 <id name="id" type="long" column="ID"> 10 <generator class="identity"/> 11 </id> 12 13 <property name="name" type="string" column="NAME" /> 14 15 </class> 16 17 </hibernate-mapping>
5.
1 <?xml version="1.0"?> 2 <!DOCTYPE hibernate-mapping 3 PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 4 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 5 <hibernate-mapping> 6 7 <class name="mypack.IncrementTester" table="INCREMENT_TESTER" > 8 9 <id name="id" type="long" column="ID"> 10 <meta attribute="scope-set">private</meta> 11 <generator class="increment"/> 12 </id> 13 14 <property name="name" type="string" column="NAME" /> 15 16 </class> 17 18 </hibernate-mapping>
6.
1 <?xml version="1.0"?> 2 <!DOCTYPE hibernate-mapping 3 PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 4 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 5 <hibernate-mapping> 6 7 <class name="mypack.NativeTester" table="NATIVE_TESTER" > 8 9 <id name="id" type="long" column="ID"> 10 <generator class="native"/> 11 </id> 12 13 <property name="name" type="string" column="NAME" /> 14 15 </class> 16 17 </hibernate-mapping>
7.
1 <?xml version="1.0" encoding="utf-8" ?> 2 <!DOCTYPE hibernate-configuration 3 PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN" 4 "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 5 <hibernate-configuration> 6 <session-factory> 7 <property name="dialect"> 8 org.hibernate.dialect.mysqlDialect 9 </property> 10 <property name="connection.driver_class"> 11 com.mysql.jdbc.Driver 12 </property> 13 <property name="connection.url"> 14 jdbc:mysql://localhost:3306/sampledb 15 </property> 16 <property name="connection.username">root</property> 17 <property name="connection.password">1234</property> 18 <property name="show_sql">true</property> 19 <mapping resource="mypack/HiloTester.hbm.xml" /> 20 <mapping resource="mypack/IdentityTester.hbm.xml" /> 21 <mapping resource="mypack/IncrementTester.hbm.xml" /> 22 <mapping resource="mypack/NativeTester.hbm.xml" /> 23 </session-factory> 24 </hibernate-configuration>
8.
1 use sampledb; 2 3 drop table if exists HILO_TESTER; 4 drop table if exists IDENTITY_TESTER; 5 drop table if exists INCREMENT_TESTER; 6 drop table if exists NATIVE_TESTER; 7 drop table if exists hi_value; 8 create table HILO_TESTER (ID bigint not null, name varchar(15), primary key (ID)); 9 create table IDENTITY_TESTER (ID bigint not null auto_increment, name varchar(15), primary key (ID)); 10 create table INCREMENT_TESTER (ID bigint not null, NAME varchar(15), primary key (ID)); 11 create table NATIVE_TESTER (ID bigint not null auto_increment, name varchar(15), primary key (ID)); 12 create table hi_value ( next_value integer ); 13 insert into hi_value values ( 0 );
9.
1 <?xml version="1.0"?> 2 <project name="Learning Hibernate" default="prepare" basedir="."> 3 4 <!-- Set up properties containing important project directories --> 5 <property name="source.root" value="src"/> 6 <property name="class.root" value="classes"/> 7 <property name="lib.dir" value="lib"/> 8 <property name="schema.dir" value="schema"/> 9 10 <!-- Set up the class path for compilation and execution --> 11 <path id="project.class.path"> 12 <!-- Include our own classes, of course --> 13 <pathelement location="${class.root}" /> 14 <!-- Include jars in the project library directory --> 15 <fileset dir="${lib.dir}"> 16 <include name="*.jar"/> 17 </fileset> 18 </path> 19 20 21 <!-- Create our runtime subdirectories and copy resources into them --> 22 <target name="prepare" description="Sets up build structures"> 23 <delete dir="${class.root}"/> 24 <mkdir dir="${class.root}"/> 25 26 <!-- Copy our property files and O/R mappings for use at runtime --> 27 <copy todir="${class.root}" > 28 <fileset dir="${source.root}" > 29 <include name="**/*.properties"/> 30 <include name="**/*.hbm.xml"/> 31 <include name="**/*.cfg.xml"/> 32 </fileset> 33 </copy> 34 </target> 35 36 37 <!-- Compile the java source of the project --> 38 <target name="compile" depends="prepare" 39 description="Compiles all Java classes"> 40 <javac srcdir="${source.root}" 41 destdir="${class.root}" 42 debug="on" 43 optimize="off" 44 deprecation="on"> 45 <classpath refid="project.class.path"/> 46 </javac> 47 </target> 48 49 50 <target name="run_increment" description="Run a Hibernate sample" 51 depends="compile"> 52 <java classname="mypack.BusinessService" fork="true"> 53 <arg value="mypack.IncrementTester" /> 54 <classpath refid="project.class.path"/> 55 </java> 56 </target> 57 58 <target name="run_identity" description="Run a Hibernate sample" 59 depends="compile"> 60 <java classname="mypack.BusinessService" fork="true"> 61 <arg value="mypack.IdentityTester" /> 62 <classpath refid="project.class.path"/> 63 </java> 64 </target> 65 66 67 <target name="run_hilo" description="Run a Hibernate sample" 68 depends="compile"> 69 <java classname="mypack.BusinessService" fork="true"> 70 <arg value="mypack.HiloTester" /> 71 <classpath refid="project.class.path"/> 72 </java> 73 </target> 74 75 <target name="run_native" description="Run a Hibernate sample" 76 depends="compile"> 77 <java classname="mypack.BusinessService" fork="true"> 78 <arg value="mypack.NativeTester" /> 79 <classpath refid="project.class.path"/> 80 </java> 81 </target> 82 83 </project>
以上是关于映射对象标识符-incrementidentityhilonativeassignedsequence<meta>的主要内容,如果未能解决你的问题,请参考以下文章