Summary of small examples of Hibernate written by ourselves

xiaoxiao2021-03-06  96

He is new, learning Hibernate, I hope everyone should not laugh.

Normal environment introduction:

JB7.0

Hibernate-2.0.3

Oracle8.1 client

database:

Oracle8.1 on the server in the LAN

Server IP address: x.x.x.x

Port: 1521

Database name: orahib

(Please ensure that the above database information is entered, and get the login name and password of the operation database)

Start programming:

(1) Creating a new project with JB, such as Hibrante_Demo2,

(2) Add request libraries for your project (Project-> Project Properties -> Paths-> Required Libraries): Add all JARs in the lib directory in the hibernate-2.0.3 directory, add hibernate-2.0.3 root of hibernate2 .jar, add Classes12.zip in the Lib subdirectory in the JDBC directory in the local ORACLE8.1 directory (here there is a drive Oracle.jdbc.driver.OracleDriver when connecting the database.

---------------------------------------------------------------------------------------------------------------------------------------

2004.3.11 15:23 Supplement:

When running this procedure, there are many friends who come to the log4j error. I suggest you pay attention to whether there is a log4j package in classpath,

If there is this package

# You can delete it in classpath

# You can also copy the log4j's Prop file (path is: hibernate directory /src/log4j.properties) to the engine's classes subdirectory.

Thank maxpain, Zxyfd2000 proposes this issue.

---------------------------------------------------------------------------------------------------------------------------------------

(3) New Class, package is Person, class name is Personmodel, which has three member variables, ID, name, address (corresponding to three fields in the database table. 咦, the table is not there! It doesn't matter, behind The program will use Hibernate to automatically generate tables, do not need to operate the database built table, it is really a province,), the GET, SET method, code results of three member variables in the class are as follows:

Code:

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;

}

}

This kind of work in this Person.PersonModel is over, I also take a break.

__________________

Herk passenger

The last time was revised by Vatful at 2004-3-11, 3:24 pm, a total of 3 times amended

Back to the top

Vatful

JBoss

Time: 2004-3-03 Wednesday, 3:40 PM: Hibernate's summary of small examples written by itself (2)

-------------------------------------------------- ------------------------------

Do the Person.Personmodel class, I invite you to pay attention, don't you say that you can automatically generate a table? Yes! Isn't that what is the definition of the field? Yeah is completely correct. Is it feeling very free!

俺 This test is mainly to insert and query the table, and this table has only one primary key (here is ID)! Also, you have to write the configuration file in the post-name HMDEL.HBM.XML (name format is the class name .hbm.xml, and the content in the class is completely correspondence, the connection between the database table), because the Personmodel written in this test. HBM.XML is very simple, the primary key is automatically generated with the Oracle database, which is an integer data, and this ID is defined as a long type when writing. If you want to use other types of primary keys to see other articles.

(4) Compile the Person.PersonModel class, then copy the hibernate.properties file under the SRC subdirectory in the hibernate-2.0.3 directory to the classs subdirectory in the Hiberante_Demo2 this project directory.

(5) Modify the data source connection in hibernate.properties, find the location of the HypersonicsQL data source connection as shown below, which is the default data source connection.

Code:

## Hypersonicsql

Hibernate.dialev 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

The fifth line in the six lines has been annotated, and now other five elements are also commented.

The result is as follows

Code:

## Hypersonicsql

# hibernate.dialect net.sf.hibernate.dialect.hsqldiaforct

# 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

Found again

Code:

## Oracle

# hibernate.dialect net.sf.hibernate.diaaLect.OrcledgeDiaLect

# 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

This is an Oracle data source connection

Remove the comment, modify

Code:

## Oracle

Hibernate.Dialev Net.sf.hibernate.DiaLect.OrglediaLect

Hibernate.Connection.driver_class oracle.jdbc.driver.Oracledriver

Hibernate.Connection.username your_database_username

Hibernate.connection.password your_database_password

Hibernate.Connection.url JDBC: Oracle: Thin: @ x.x.x.x: 1521: Orahib

Oracle.jdbc.driver.OracleDriver, because of this class, so start to add Class12.zip to. Next, I understand, the login name, password, X.x.x.x of the database is the IP address of the server where the database is located, and 1521 is the port, ORAHIB is the database name.

(6) New PersonModel.hbm.xml in the directory where Personmodel.class is located, and its contents are as follows:

Code:

Table = "ZY_PERSON">

Zy_Person_id_seq

Simple explanation

Personmodel This class corresponds to the Zy_Person in the database (now there is not yet, automatically generated behind)

Zy_Person_id_seq

Name = "id" This is the ID in the class, which defines it to use the tag, type LONG type, the primary key is generated in the form of class = "sequence" (this is suitable for Oracle Database), Zy_Person_ID_SEQ is the name of the corresponding Sequence in the Oracle database (this Sequence, the program will also be generated).

Another two member variables.

The above is a very simple configuration, and the Zy_Person in the database will have three fields, the names are in the class, name, and address will be String type. Data in the database can be different from the name in the class, please refer to other articles.

The configuration is completed, and the rest is to write test code test Hibernate.

Take a break.

__________________

Herk passenger

Time: 2004-3-03 Wednesday, 4:20 pm Title: Summary of small examples written by Hibernate (3)

-------------------------------------------------- ------------------------------

(7) The last step,

Create a SQL_OUT_LIB folder in the Hibrante_Demo2 engineering root directory.

In JB, create a new class, package is Person, class name TestPersonmodel, enter code, as follows:

Code:

Package Person;

Import Net.sf.hibActory;

Import Net.sf.hibiBernate.cfg.configuration;

Import net.sf.hibernate.tool.hbm2ddl.schemaExport;

Public class testpersonmodel {

PRIVATE STATIC sessionFactory sessionFactory;

Public static void main (string [] args) throws exception {

Configuration conf = new configuration (). Addclass (personmodel.class);

// For the first time it is used to create a table in the database

/ / And output the SQL statement to the TXT file

// After running, you will get rid of this paragraph, otherwise you will create a new table every time.

SchemaExport dbexport = new schemaexport (conf); dbexport.setoutputfile ("sql_out_lib // sql.txt");

Dbexport.create (true, true);

}

}

We have newly built a SQL_OUT_LIB directory, where an additional output file is set by SchemaExport, and the SQL statement generated by Hibernate is output to SQL.txt (this file name can be defined), and then perform the CREATE operation. At this time, the database is built in the database, and Zy_Person_ID_SEQ is built.

In JB, create a category, package is Person, class name TestPersonmodel2, enter code, as follows:

Code:

Package Person;

Import net.sf.hibernate.Session;

Import net.sf.hibernate.transaction;

Import Net.sf.hibActory;

Import Net.sf.hibiBernate.cfg.configuration;

Public class testpersonmodel2 {

PRIVATE STATIC sessionFactory 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 ("robin");

P1.Setaddress ("Shanghai");

// 2. Persistence

S.SAVE (P1);

/ / At this point P1 can already be found in the database

t.commit ();

s.close ();

}

}

This is the same as the JDBC, which is equivalent to connection, transaction is transaction processing, and PersonModel P1 does not need to set ID, the database is automatically generated.

Create a class in JB, package is Person, class name TestPersonmodel3, enter code, as follows:

Code:

Package Person;

Import net.sf.hibernate.Session;

Import Net.sf.hibActory;

Import Net.sf.hibiBernate.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 ();

}

}

Pay attention to the writing of the query statement, check the Personmodel class (就 Check Zy_Person table), starting a small P, where the wHERE statement checks small P ID is equal to 1; q.list () Returns the list class, then use GET (int)

Method GET (0) Take the first result, our query results came out.

ended

(2004.3.15 In the reply post behind this post, I added an example of modification and deletion, very simple, everyone can see)

__________________

Add time: 2003/12/16

Article: 44

¥¥ 100

Time: 2004-3-03 Wednesday, 4:50 pm Title: Several questions encountered

-------------------------------------------------- ------------------------------

Beginner Hibernate has to mention several issues that may encounter

(1) *. The definition of the primary key in the hbm.xml file should not make a mistake. To select the appropriate primary key generation method, the is used.

(2) When you write an example, you also copy the code from the Internet. Class Compilation cannot be passed, there is a character that cannot be identified in the XML file, which may be because there is a unfamiliar character in the copy code. If you have any questions, check it carefully.

(3) When the biggest error starts testing, the CREAT statement, the insert statement is successful, but the query statement can not succeed, depressed for a few days, and then there are other brothers and the same problem, and ask me This rookie, I am as it? I want RapidSQL to access the database CREAT statement, insert statement, query statement, indicating that there is no problem over there, this CREAT statement, insert statement can, indicate that the code is no problem, single single query statement is a blaming. I really didn't have anything, asked some masters and didn't reply, so I put it down. Once an accidental opportunity, let me touch the version of Oracle.jdbc.driver.OracleDriver, so I think of this, the original connection database is Oracle9, the version is too high.

Share with you only by this.

__________________

Herk passenger

转载请注明原文地址:https://www.9cbs.com/read-124783.html

New Post(0)