Java--Using hibernate on Eclipse

Source: Internet
Author: User

A recent project to be done in Java, is not familiar at all. No way, had to bite the bullet, spent most of the day, and finally completed the first hibernate example on Eclipse. The key steps are documented below to take notes for future viewing.

(1) Download hibernate and import hibernate into the project.
Project->properies->java Build path->libraries->add External JARs ..., Select Hibernate3.jar in the Hibernate root directory to add to the project.
Next, all the files under Hibernate in the Lib folder are added to the project as a user library, otherwise, If only add Hibernate3.jar, compile can pass, but run will throw Classnotdef exception, because Hibernate3.jar relies on hibernate under the Lib folder file.

2) Our application's backend database uses Oracle, so the first step is to introduce a package containing Oracle JDBC driver in the example project, Classes12.jar. The jar file is located under the Jdbc\lib directory of the Oracle installation directory.
In Eclipse, Project->properies->java Build path->libraries->add External JARs ..., Select Classes12.jar to add it to your project.

(3) generate the Hibernate.cfg.xml file.
Usually hibernate configuration files and. hbm.xml files can be automatically generated, this is a lot of auto-generated tools, I use Hibernatesynchronizer, it can be added as a plug-in to eclipse. When the Hibernatesynchronizer plugin is loaded correctly, we can add Hibernate profiles to the current project: File->new->other->hibernate->hibernate Configuration File, the following interface appears:


Note that the Driver class chooses Oracle.jdbc.driver.OracleDriver for Oracle, and the database URL is formatted correctly, such as:

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->hibernate Mapping File, the following interface appears:


After filling out the password, click the Refresh button to list all the accessible database tables in the tables. Then select the table for which you want to generate the. hbm.xml file, click Finish, and the corresponding. hbm.xml file will be generated, for example, if I select the Mobileuser table above, a Mobileuser.hbm.xml file will be generated.

(5) Automatically generate entity classes from the. hbm.xml file.
By selecting the Mobileuser.hbm.xml file in the Package explorer and right-->hibernate synchronizer->synchronize files, you can generate the corresponding entity classes and DAO classes. If you only want the entity class, then you can synchronizer->data Access Objects in Project->properies->hibernate, and you will "I would like to having DAOs Created for me "hook option is removed.

(6) Add the corresponding mapping resource in the Hibernate.cfg.xml file.
With the Mobileuser.hbm.xml file selected in the Package Explorer, right-->hibernate synchronizer->add Mapping Reference, the
The following configuration is automatically generated in Hibernate.cfg.xml:

<mapping resource= "Hibernatetest/mobileuser.hbm.xml"/>

(7) Modify the auto-generated hibernate.cfg.xml file. Needs to be added at 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 fragment again every time the Hibernate.cfg.xml file is automatically modified.

Everything is available, now you can write a test to check it out:

Just as an example, no exception handling
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 ();
} from:http://zhuweisky.cnblogs.com/archive/2006/05/30/413045.html

Java--Using hibernate on Eclipse

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.