Author: bluesky35 (blue) development environment: Windows2000 JBuilderX Hibernate2.1.6 Oracle9i 1. Installation JBuilderX and Oracle9i, and use the following information to configure the Oracle user name: system password: admin service: cpdb2 go to http:. // prodownloads. SourceForge.Net/Hibernate/ Download the Hibernate API package hibernate-2.1.6.zip, as shown in the following figure 3. Select Project Properties in the Project menu in JBuilder, add Hibernate all API packages in Require Library, but also join ORACLE directory ORANT / JDBC / LIB / CLASS12.ZIP4. Then copy the compressed hibernate-2.1 / etc / hibernate.properties to local Project's classes directory and do the following changes: 1 Note Default database connection, will ## HypersonicSQLhibernate.dialect net.sf.hibernate.dialect.HSQLDialecthibernate.connection.driver_class org.hsqldb.jdbcDriverhibernate.connection.username sahibernate.connection.passwordhibernate.connection.url jdbc: hsqldb: hsql: //localhosthibernate.connection.url jdbc: hsqldb : testhibernate.connection.url jdbc: hsqldb :. changed to ## HypersonicSQL # hibernate.dialect net.sf.hibernate.dialect.HSQLDialect # hibernate.connection.driver_class org.hsqldb.jdbcDriver # hibernate.connection.username sa # hibernate. Connection.Password # hibernate.connection.URL JDBC: HSQLDB: HSQL: // Loca lhost # hibernate.connection.url jdbc: hsqldb: test # hibernate.connection.url jdbc: hsqldb :. and then modify the connection to Oracle, hibernate.dialect net.sf.hibernate.dialect.Oracle9Dialect hibernate.dialect the ## Oracle # # net.sf.hibernate.dialect.OracleDialect # hibernate.connection.driver_class oracle.jdbc.driver.OracleDriver # hibernate.connection.username ora # hibernate.connection.password ora # hibernate.connection.url jdbc: oracle: thin: @localhost : 1521: test was changed to ## Oraclehibernate.dialect net.sf.hibernate.dialect.Oracle9Dialecthibernate.dialect net.sf.hibernate.dialect.OracleDialecthibernate.connection.driver_class oracle.jdbc.driver.OracleDriverhibernate.connection.username systemhibernate.connection. Password adminhibernate.connection.URL JDBC: Oracle: Thin: @
172.28.122.49:1521:CPDB Note: @ 172.28.122.49 For the Oracle Server IP address, 1521 is the port number 5. Create package person under Project, establish four Class Class under Package,: 1PersonModel.javaPackage Person; import java.io.Serializable; public class PersonModel implements Serializable {private Long id; private String name; private String address; public Long getId () {return id;} public void setId (Long id) {this.id = id;} public void setName (String name) {this.name = name;} public String getName () {return name;} public void setAddress (String address) {this.address = address;} public String getAddress () {return address;}} ②TestPersonModel1.javapackage person; import net.sf.hibernate.SessionFactory; import net.sf.hibernate.cfg.Configuration; import net.sf.hibernate.tool.hbm2ddl.SchemaExport; public class TestPersonModel1 {private static SessionFactory sessionFactory; public static void Main (string [] args) throws exception {system.out.println ("start"); configuration conf = new configuration (). addclass PersonModel.class); SchemaExport dbExport = new SchemaExport (conf); dbExport.setOutputFile ( "sql_out_lib // sql.txt"); dbExport.create (true, true);}} ③TestPersonModel2.javapackage person; import net.sf.hibernate .Session; import net.sf.hibernate.Transaction; import net.sf.hibernate.SessionFactory; import net.sf.hibernate.cfg.Configuration; public class TestPersonModel2 {private static SessionFactory sessionFactory; public static void main (String [] args "throws exception {configuration configured (); addclass (personmodel.class); // Insert the first data in the table SessionFactory = confAactory = confAactory (); session s = sessionfactory.opensession (); Transaction T =
S.BegintransAction (); Personmodel P1 = New Personmodel (); p1.setname ("zhaol"); p1.setaddress ("shanghai"); // Persistence S.Save (P1); // Database T.CMmit (); sclose ();}} 4TestPersonmodel3.javaPackage Person; import net.sf.hibernate.Session; import net.sf.hibernate.SessionFactory; import net.sf.hibernate.cfg.configuration; import Net .sf.hibernate.Query; public class TestPersonModel3 {private static SessionFactory sessionFactory; public static void main (String [] args) throws Exception {Configuration conf = new Configuration () addClass (PersonModel.class);. sessionFactory = conf.buildSessionFactory ( ); Session s = sessionFactory.openSession (); personmodel p = new personmodel (); query = S.CreateQuery ("from personmodel as p where p.id = 1"); p = (personmodel) q.list () .get (0); System.out.println (p.getname ()); s.Close ();}} where PersonModel.java is the storage structure of fields in the field, TestPersonModel1.java is test automatic creation table and field TestPersonModel2.java is a test in the table inserted records, TestPersonModel3.java is a test from the table to obtain records. 6. Compile all JAV A file 7. Ok, to the last step, it is also the most critical step, we have defined the database connection (Hibernate.properties) created the storage structure of the field (Personmodel.java), also written the code of the operation database ( TestPersonModel1.java ~ testpersonmodel3.java), next to define the configuration files in the database, because the storage structure file of our field is Personmodel.java, so create a perspodel.hbm.xml (Note: Configuration file The name is composed of [File Name .hbm.xml], creating a location for Project's class / person directory (Note: Database definition file hibernate.properties is placed under the Class directory, and the table configuration file To put it under the corresponding package, our package name is Person, so put it in class / person) Persnmodel.hbm.xml is as follows: XML Version = "1.0" eNCoding = "GB2312"?>