Use eclipse to develop a hibernate application (hibernate synchronizer plug-in)

Source: Internet
Author: User
Document directory
  • Common hibernate tools
Use eclipse to develop hibernate applications

Document tools
Recommended to friends
Print article

Time: 2006-01-24
Author: Yu Limin
Browsing times:

12446
Keyword: Eclipse, hibernate, plug-in, Orm

This article describes how to use the hibernate synchronizer plug-in to quickly develop hibernate applications in eclipse 3.0.1 to improve our work efficiency.

Eclipse is a very popular development platform. The open and scalable architecture allows many programmers to find their own personalized work environment. Hibernate is also a very popular o/R Mapping Framework, it is a good choice when we consider o/R Mapping, at least before EJB 3.0 becomes popular.

For details about the Hibernate framework, see hibernate in action.

Install this plug-in first. Eclipse 3.0.1 provides us with a more convenient plug-in management method. Choose "Help-Software Update-find and install" from the menu ", select "search for new feature to be installed". The operation interface is as follows:

Select new remote site, enter the name hibernate plugin, fill in the following url: http://www.binamics.com/hibernatesync

For example:

On the "Install" Page, select the newly added hibernate plugin, open it, select the next level of synchronizer, And next. The following installation steps are as follows: Next.
Eclipse can be downloaded and installed by itself, and then eclipse is automatically restarted once.

The spring plug-in address is:
Http://springframework.sourceforge.net/spring-ide/eclipse/updatesite/
The installation configuration is similar to the installation of hibernate plugin.

Spring address:
Http://www.springframework.org/

The following shows a simple single table operation, so that we are familiar with the development process. We chose the MySQL database for this application. First, we created a new database in MySQL as hibernatetest, and then created a data table named "person", which contains four fields: ID, name, sex, and address, the table creation statement is as follows:

# Host: localhost# Database: hibernatetest# Table: 'person'# CREATE TABLE `person` (  `ID` int(11) NOT NULL auto_increment,  `Name` varchar(20) NOT NULL default '',  `Sex` char(1) default NULL,  `Address` varchar(200) default NULL,  PRIMARY KEY  (`ID`)) TYPE=MyISAM; 

Create a common Java project: "file-> Project-> new project-> JAVA Project

Enter the Project name: hibernatemysql

Note that all lib files of Hibernate are added, including the hibernate2.jar files under Hibernate and all. Jar files under the lib directory;
There are JDBC driver files to be added to MySQL, such as mysql-connector-java-3.0.14-production-bin.jar
The driver chooses to load the file. The file name varies depending on the version :)

Next we need to add a hibernate configuration file to the project, select new> Other> hibernate configuration file under the src directory, for example:

In the pop-up interface, you need to specify the database to be used and the information required to connect to the database. The corresponding database is MySQL, the URL and administrator account and password of the database are configured as follows:

Database Type: QLDriver Class:  org.git.mm.mysql.DriverDatabase URL: jdbc:mysql://localhost:3306/HibernateTestUsername: rootPassword: 

Click the Browse button and enter the driver in the pop-up box. The corresponding driver package appears below.
Select the file in the package where org. Git. Mm. MySQL. Driver is located and click OK.

In, we can also specify to obtain the connection from the JNDI data source, and click the datasource label for configuration.

After you click Finish, the system will generate a file named hibernate. cfg. the XML file contains basic configuration information. If advanced configuration is required, you can manually configure the file or edit it using other plug-ins, such as the XML editor of myeclipse.

To generate a ing file, first create a new package, and enter net. echochina. hibernatetest.

In this package, select new-> Other-> hibernate Mapping File, and click Refresh in the pop-up interface to list all data tables in the warehouse, select the person table we want to use, click the Browse button, and select the package where the pojo file we want to generate is located: net. echochina. hibernatetest

For example:

You can configure other HBM options in the Properties tab of the preceding interface, including file extension, aggregate column name, and Id generation rules. After that, the system will automatically generate a file named person. HBM. We can use this file to generate related stubs.

Right-click the person. HBM file and choose hibernate synchronizer-> Synchronize files

This operation will generate three packages and eight class files, which encapsulate the operation details of hibernate, so that we can focus on the development of business logic, read these files carefully to improve your understanding of Hibernate and increase your application skills.

Then, we need to add information about person in the hibernate configuration file, right-click person. HBM, and choose synchronizer-> Add mapping reference.

Now we can start to write our own program logic, and the code can be written in
In the net. echochina. hibernatetest. Dao. persondao class, the code of this class will not be modified by the plug-in.

First, add a new record to the database.
Add import net. echochina. hibernatetest. Person to the persondao class;
Then add a main method with the following content:

public static void main(String args[]){try{_RootDAO.initialize();PersonDAO persondao = new PersonDAO();Person person = new Person();person.setName("YuLimin");person.setSex("M");person.setAddress("http://Java.EchoChina.net");persondao.save(person);}catch(Exception e){e.printStackTrace();}}

// Test MySQL's Chinese problem: change the content as follows and change the setname parameter to Chinese:

Person. setname ("Yu Limin ");

Add the following two rows to the <session-factory/> block of the hibernate. cfg. xml file.

<Property name = "connection. useunicode"> true </property>
<Property name = "connection. characterencoding"> GBK </property>

In this way, data insertion and reading will not cause Chinese characters. If there are still Chinese characters, please google :)

We can see that the plug-in has encapsulated both session and transaction operations, and our code work has been greatly simplified. In addition, we can use the hibernate editor provided by the plug-in to edit HBM files, which is very convenient.
We also need to change the ID generation method to identity, right-click person. HBM, open with-> hibernate Editor

 

Change the ID generation method to identity.

To make the program run properly, you also need to make some modifications to the configuration file hibernate. cfg. xml.
Use the eclipse text editor to open the file, which contains the following content:

<!-- <property name="hibernate.transaction.factory_class">net.sf.hibernate.transaction.JTATransactionFactory</property><property name="jta.UserTransaction">java:comp/UserTransaction</property> -->

In our example, JTA is not used to control transactions, so you need to comment out the above content before the program can run normally.

Now you can start running. Click Run.

Select Java application in the configuration that appears, and enter persondao in name

Click the search button and select the persondao class in the net. echochina. hibernatetest package in the pop-up box.

Click Run to start running. If the preceding steps are correct, you can see that the data has been saved to the database.

If you need to redesign the data table structure in the actual development work, you only need. modify the HBM file, and then execute the synchronize and overwrite operations. The plug-in will generate the stub file again. We only need to modify the program logic, which is very convenient. With such a function plug-in, we can greatly free ourselves from writing configuration files and checking errors, thus improving our work efficiency.

Common hibernate tools

Hibernate synchronizer is a good auxiliary development tool. It is an Eclipse plug-in that can generate 80% of the Code, the first choice.
Myeclipse installation 3.8.3 is very powerful, mainly because it is paid and comprehensive. Like their struts components, it is essential to develop struts.
Hsql is a good tool for testing hsql. It is an Eclipse plug-in.
Hibern8ide (now renamed as: hibernateconsole) is a separately running hibernate tool that can run the Hibernate query language (hql) statement.
A tool used by middlegen to generate persistent objects.

Persistence Layer with EJB (CMP 2.0)
Persistence Layer with JDO
Persistence Layer with hibernate
Persistence Layer with Torque
Database GUI with JSP/struts

A must for http://spindle.sourceforge.net to develop tapestry

Auxiliary tool for writing spring application context file in http://springui.sourceforge.net

Author Profile
  Dev2dev ID: yulimin, Bea dev2dev Forum web application development Moderator.
Home page: http: // 202.101.111.1/123/
Blog: http://iAMin.BlogDriver.com

Author other articles

  • Bytecode outline of Eclipse plug-in
  • Eclipse plug-in-findbugs
  • Easyexplorer for Eclipse plug-ins
  • Implementors of Eclipse plug-ins
  • Sqlexplorer of Eclipse plug-in
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.