Java -- use hibernate on Eclipse

Source: Internet
Author: User

I am not familiar with the latest project using Java. No way, I had to stick to my head, and spent a long time, finally completed the first hibernate example in eclipse. Next, record the key steps and take notes for future reference.

(1) download Hibernate and import it to the project.
Project-> properies-> JAVA build path-> libraries-> Add external jars..., select hibernate3.jar under the hibernate root directory and add it to the project.
Next, you need to add all the files in the Lib folder under hibernate to the project as a user library. Otherwise, if you only add hibernate3.jar, You can compile it through, when running, the exception of classnotdef is thrown because hibernate3.jar depends on files in the Lib folder of hibernate.

2) the background database of our application uses Oracle, so we should first introduce the package containing oracle JDBC driver in the example project, classes12.jar. The jar file is located in the JDBC \ lib directory of the Oracle installation directory.
In eclipse, choose Project> properies> JAVA build path> libraries> Add external jars..., select classes12.jar, and add it to the project.

(3) generate the hibernate. cfg. xml file.
Usually, Hibernate configuration files and. HBM. xml files can be automatically generated. There are many automatic generation tools. I use hibernatesynchronizer, which can be added to eclipse as a plug-in. After the hibernatesynchronizer plug-in is correctly loaded, we can add the hibernate configuration file to the current project: file-> New-> Other-> hibernate configuration file. The following interface is displayed:

Note that the driver class should be oracle. JDBC. Driver. oracledriver for Oracle, and the database URL format should be correct, such:

JDBC: oracle: thin: @ 10.8.8.221: 1521: orcl



It is best to store the hibernate. cfg. xml file in the root directory of the project.

4) generate the. HBM. xml file. File-> New-> Other-> hibernate Mapping File, the following interface is displayed:


After entering the password, Click Refresh to list all the database tables that can be accessed in tables, and select the tables to be generated. HBM. in the XML file, click Finish to generate the corresponding table. HBM. XML file. For example, if the mobileuser table is selected above, mobileuser is generated. HBM. XML file.

(5) The entity class is automatically generated from the. HBM. xml file.
In package explorer, right-click the mobileuser. HBM. xml file and choose hibernate synchronizer> Synchronize files to generate the corresponding entity class and DAO class. If you only want object classes, you can go to project> properies> hibernate synchronizer> data access objects, remove the "I wowould like to have daos created for me" option.

(6) Add the corresponding mapping resource to the hibernate. cfg. xml file.
In package explorer, right-click the mobileuser. HBM. xml file and choose hibernate synchronizer> Add mapping reference.
The following configuration is automatically generated in hibernate. cfg. xml:

<Mapping Resource = "hibernatetest/mobileuser. HBM. xml"/>

(7) modify the automatically generated hibernate. cfg. xml file. Add the following in the header of the hibernate. cfg. xml file:

<! Doctype hibernate-configuration public
"-// Hibernate/hibernate configuration DTD 3.0 // en"
Http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd>

It is tedious to add this XML snippet every time the hibernate. cfg. xml file is automatically modified.

Everything is ready. Now you can write a test to test it:

// As an example only, no exception handling is performed.
Public static void main (string [] ARGs)
{
Configuration CFG = new configuration (). Configure ();
Sessionfactory sfactory = cfg. buildsessionfactory ();

Session session = sfactory. opensession ();
Transaction Tx = session. begintransaction ();
Mobileuser user = (mobileuser) Session. Load (mobileuser. Class, new INTEGER (2 ));
String age = user. getmobilenumber ();

System. Out. println (AGE );
TX. Commit ();
Session. Close ();
}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.