Developing Hibernate applications with eclipse

Source: Internet
Author: User
Tags contains interface modify mysql new features stub mysql database
Program Eclipse is a very popular development platform, the open architecture allows many programmers to find their own personalized work environment, Hibernate is also the current bull-fork framework, at least in the EJB 3, before the big line, it is we consider or mapping is very good choice. This article mainly describes how to use the Hibernate SYN plug-in in the Eclipse 3.0.1 rapid development of hibernate applications, improve our efficiency. About the details of the Hibernate framework, this article does not do too much discussion, please refer to the relevant information, recommend the "Hibernate in Action" book.

First you need to install this plugin, Eclipse 3.0.1 provides us with a more convenient way to manage Plug-ins, select the Menu "Help-Software Update-Find and install" (My Eclipse uses the language of the Chinese, please use the English interface user to control), select "Search for new features to install", The operating interface is as follows:


Select "New remote Site", fill in the name "Hibernate plugin", and fill in the following URL: Http://www.binamics.com/hibernatesync
The following figure:


Then, select the "Hibernate plugin" We just added in the "Install" interface, and Next,eclipse can download and complete the installation on its own, and eclipse will do it automatically once it restarts.

Here's a simple one-table operation that lets us quickly familiarize ourselves with the development process. We choose MySQL database to do this application, first set up a data table, named User_info, contains ID, name, password three fields, the table statement is as follows:

# Host:localhost
# Database:test
# Table: ' User_info '
#
CREATE TABLE ' User_info ' (
' id ' int (one) not NULL auto_increment,
' Name ' varchar ' not NULL default ',
' Password ' varchar not NULL default ',
PRIMARY KEY (' id ')
);


Then create a new normal Java project: "New-Project-java project," and note that all LIB files are added to hibernate, as well as MySQL's JDBC driver files. Below we need to add a hibernate profile in the project, select "New-Other-hibernate-hibernate configuration file" in the SRC directory, as shown in the following figure:


In the pop-up interface, you need to specify the database to use, as well as the information required to connect the database, we have chosen the database for MySQL, and configured the database URL and administrator account, as follows:


In the above illustration, we can also specify that a connection be obtained from the Jndi data source, which can be configured by clicking on the "Datasource" tab.

After completion, the system generates a file named "Hibernate.cfg.xml" that contains basic configuration information and can be added manually if an advanced configuration is required.

Below we will generate the mapping file, first create a new package "Org.bromon.zizz.pojo", under this package select "New-Other-hibernate-hibernate mapping file", click "Refresh" in the pop-up interface, It will list all the data tables in the library, select the "user_info" table we want to use, and the package that contains the Pojo file we want to generate: Org.bromon.zizz.pojo. The following illustration:


You can configure other options for HBM in the Properties tab of the above interface, including file extensions, aggregation column names, id generation rules, and so on. When finished, the system will automatically generate a file called "USERINFO.HBM", which we can generate related stub classes. Select Hibernate synchronizer-synchronize files on the userinfo.hbm file:


This operation generates three packages of eight class files, which encapsulate the Hibernate operation details. So that we can focus on the development of business logic, read these files carefully can improve your understanding of hibernate, the growth of application skills.

Then we need to add information about UserInfo in the Hibernate configuration file and select "Synchronizer-add Mapping Reference" on the USERINFO.HBM. Now we can start writing our own program logic, and the code can be written in Org.bromon.zizz.pojo.dao. In Userinfodao, the code for this class will not be modified by the plugin. We first create a new record and add a main method to the class:

public static void Main (String args[])
{
Try
{
_rootdao.initialize ();
Userinfodao uid=new Userinfodao ();

Org.bromon.zizz.pojo.UserInfo ui=new org.bromon.zizz.pojo.UserInfo ();
Ui.setname ("Bromon");
Ui.setpassword ("123");

Uid.save (UI);

}catch (Exception e)
{
E.printstacktrace ();
}
}


As can be seen, the plug-in has been the session and transaction operations are encapsulated, our code work has been greatly simplified. And we can use the plug-in from the Hibernate editor to edit the hbm file, very convenient, we need to generate the ID of the way to "identity":


To make this program work, you will also need to make some modifications to the configuration file Hibernate.cfg.xml. Notably, do not modify the configuration file in an environment other than the plug-in (such as using tools such as Dreamweaver), or it will cause the system to be unsynchronized with resources, which may be a bug in the plugin. Open the file using the Eclipse's text editor, which has the following contents:

Net.sf.hibernate.transaction.JTATransactionFactory


Java:comp/usertransaction


Because in our example, we do not use JTA to control the transaction, so we need to comment out the above content, the program can run normally.

Ok,run, you can see that the data has been saved to the database.

If you need to redesign the datasheet structure in real-world development, just make the appropriate modifications in the. hbm file, and then perform the "Synchronize and Overwrite" operation, and the plugin will regenerate the stub file, and we just need to modify the program logic to make it very convenient. With such a function plug-in, we can greatly from the configuration file compilation, error-free, so as to improve our work efficiency.

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.