1. Install jbuilderx and Oracle9i, and configure Oracle with the following information
User name: System
Password dmin
Service PDB
2. Go to http://prodownloads.sourceforge.net/hibernate/download hibernate> api的hibhibernate-2.1.6.zip, as shown in
3. Select project properties from the project menu in JBuilder, add all the hibernate API packages to the require library, and add the Oracle directory orant/jdbc/lib/classes12.zip.
4. Copy the compressed hibernate-2.1/etc/hibernate. properties to the classes directory of the local project and make the following changes:
① Comment on the default database connection, Set
# 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 jdbcsqldbsql: // localhost
Hibernate. Connection. url jdbcsqldb: Test
Hibernate. Connection. url jdbcsqldb :.
Change
# 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 jdbcsqldbsql: // localhost
# Hibernate. Connection. url jdbcsqldb: Test
# Hibernate. Connection. url jdbcsqldb :.
Then modify the Oracle connection and set
# 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 jdbcracle: thin :@ localhost: 1521: Test
Change
# 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 System
Hibernate. Connection. Password Admin
Hibernate. Connection. url jdbcracle: thin: @ 172.28.122.49: 1521pdb
Note: @ 172.28.122.49 is the IP address of the Oracle server, and 1521 is the port number.
5. Create a package person under the project and four classes under the package, respectively:
① Personmodel. Java
Package 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.java
Package 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;
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.java
Package 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;
Public static void main (string [] ARGs) throws exception {
Configuration conf = new configuration (). addclass (personmodel. Class );
// Insert the first data in the 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 );
// Existing records in the database
T. Commit ();
S. Close ();
}
}
④ Testpersonmodel3.java
Package 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;
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 ();
}
}
Here, personmodel. Java is the storage structure of the fields in the corresponding database. testpersonmodel1.java is the test to automatically create tables and fields, testpersonmodel2.java is the test to insert records into the table, and testpersonmodel3.java is the test to obtain records from the table.
6. compile all java files
7. Okay. The last step is also the most critical step. We have defined the database connection (hibernate. properties)
The storage structure of the field (personmodel. Java) is created, and the Code for operating the database (testpersonmodel1.java ~ Testpersonmodel3.java), the next step is to define the configuration file of the table in the database, because the storage structure file of our field is personmodel. java, so create a personmodel. HBM. XML (note: the configuration file name of the table is composed of [storage structure file name of the field +. HBM. created in the class/person directory of the project (note: the database definition file hibernate. properties is placed under the class directory, and the configuration file of the table should be placed under the corresponding package. Here, the package name is person, so it is placed under Class/person)
The content of personmodel. HBM. XML is as follows:
<? XML version = "1.0" encoding = "gb2312"?>
<! Doctype hibernate-Mapping System "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<Hibernate-mapping>
<Class name = "person. personmodel"
Table = "ZL">
<ID name = "ID" type = "long">
<Generator class = "sequence">
<Param name = "sequence"> zl_id_seq </param>
</Generator>
</ID>
<Property name = "name"/>
<Property name = "Address"/>
</Class>
</Hibernate-mapping>
The table name ZL contains two fields (name and address, corresponding to person. personmodel). ID is a sequence.
8. now, the configuration is complete. Run testpersonmodel1.class first, And let hibernate automatically create a table named ZL (with the field name and address in it) for us, after running, the JB display and database changes are as follows:
How is it? Have the table been created.
Next, run testpersonmodel2.class and let hibernate insert a record for us.