Author: bluesky35 (blue sky)
Development Environment: Windows2000 JBuilderx Hibernate2.1.6 Oracle9i 1. Install JBuilderx and Oracle9i, and use the following information to configure Oracle Username: System Password: Admin Service: CPDB2. First Http://produnloads.SourceForge.Net/Hibernate/
Download the Hibernate API package hibernate-2.1.6.6.zip, as shown in the following figure 3. Select Project Properties in the Project menu in JBuilder, join the Hibernate all API packages in Require Library, and join the ORACLE directory ORANT / JDBC / LIB / CLASS12.ZIP4. Then copy the cut-compressed hibernate-2.1 / etc / hibernate.properties to local Project's classs directory and do the following changes: 1 Note Default database connection, will ## Hypersonicsql
hibernate.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 :. Change 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: / /localhost#hibernate.connection.url jdbc: hsqldb: test # hibernate.Connection.URL JDBC: hsqldb :. then modify Oracle's connection, will ## oracle
# Hibernate.dialect net.sf.hibernate.dialect.Oracle9Dialect # hibernate.dialect 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 change to ## Oracle
hibernate.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: 152.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 in Package, respectively: 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 ()} public string getaddress;}
} 2testPersonModel1.javaPackage Person;
Import net.sf.hibrnate.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.setputfile ("sql_out_lib // sql.txt"); dbexport.create (true, true);}} 3TestPersonmodel2.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 conf = new Configuration () addClass (PersonModel.class);. // inserted in the first data table
SessionFactory = conf.buildsessionFactory (); session s = sessionFactory.openSession ();
Transaction t = s.begintransaction ();
Personmodel P1 = New Personmodel (); p1.setname ("zhaol"); P1.SetAddress ("Shanghai);
// Persistence S.Save (P1); // There is already recorded in the database T.CMMIT (); s.close ();}}
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 q = 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 a storage structure of fields in the field. TestPersonModel1.java is a test automatic creation table and field. TestPersonModel2.java is a test in the table. TestPersonModel3.java is a test from the table. 6. Compile all Java file 7. Ok, to the last step, it is also the most critical step, we have defined the database connection (Hibernate.properties) created a field storage structure (Personmodel.java), also written the code of the operation database. (TestPersonModel1.java ~ testpersonmodel3.java), next to define the configuration file in the database in the database, because the storage structure file of our field is Personmodel.java, so create a pers -model.hbm.xml (Note: Configuration of Table) The file name is composed of [Storage Structure File Name .hbm.xml], create a location for the coplass / person directory of Project (Note: Database definition file hibernate.properties is placed under the class directory, and the table configuration The file should be placed under the corresponding package, we hereinafter are Person, so put in class / person) Persnmodel.hbm.xml is as follows: XML Version = "1.0" encoding = "GB2312"? >
Copyright Notice: 9CBS is this BLOG managed service provider. If this paper involves copyright issues, 9CBS does not assume relevant responsibilities, please contact the copyright owner directly with the article Author.