[ERP system design] [data module] 4. Hibernate Development Instance

Source: Internet
Author: User

Hibernate Development Instance

Currently, the widely used database systems are relational database systems, and the applications we develop at ordinary times adopt the object-oriented approach for development. Every time the program performs operations on the database, it must convert the object data into complex SQL statements before accessing and operating the database. This leads to a large number of code duplication and reduced program development efficiency. In addition, as an independent language, SQL has its own essence. Not all programmers can operate databases just as they are familiar with development languages. For general projects, special database developers are required to manage data. To address these problems, there have been some database management software on the market, but they all have their own advantages and disadvantages and there is no perfect solution yet.

(1) common relational database management software, such as Navicate:

Advantage: This type of software allows you to perform various operations on relational database data, so that you can easily modify the data table structure and data.

Disadvantages: This type of software is only suitable for specialized relational database designers, and it is inconvenient to use the designed data tables in object-oriented programs. There will be a large amount of repeated code in the program for operations on database tables, the software does not support object-oriented modeling.

(2) object-oriented database management software, such as the Hibernate Synchronizer plug-in:

Advantage: You can generate an object-oriented class from the database and then directly operate the data table in the program;

Disadvantage: only designed databases can be converted to object-oriented classes, and data tables can be operated. Data Table structures cannot be modified directly, and object-oriented modeling is not supported.

(3) Support for object-oriented database management software such as PowerDesiner

Advantage: supports object-oriented modeling and code generation.

Disadvantages: non-open-source products are expensive, and the software is huge. The functions are mainly reflected in the poor data management support for modeling, and it is not convenient to debug data during programming.

Based on the above introduction, we can see that although there are already a considerable number of database management software at home and abroad, there is not one suitable for our platform. Therefore, we have integrated the advantages of various database management software at home and abroad to make up for their shortcomings and designed our own database management plug-ins. Our plug-ins support object-oriented database modeling and can modify the model in a timely manner. The plug-in supports automatic generation of code. After the model is designed, Java classes, object relationship ing files, and Hibernate configuration files can be automatically generated so that other programs can directly access the database through the Hibernate plug-in. The plug-in also supports database data management. The selected design object can directly view the data mapped to the data table in the database and support various data operations. As a data layer management plug-in for building an ERP system platform, this plug-in fully simplifies the system construction process from the perspective of ERP system designers. Developers can directly perform object-oriented modeling without any knowledge of relational databases. Developers can directly manage the data table structure and data visually without a single SQL statement. All operations on relational databases are completed in the system background. In addition, after modeling, Code and related files can be automatically generated for development and use by other programs. For other projects, you only need to import the Java classes, ing files, and Hibernate configuration files generated in the database modeling section to directly access the database using Hibernate. This plug-in makes the database design of the entire project much easier and more efficient.

The following describes a simple example.

[This article undertakes ([ERP system design] [data module] 3 using the Eclipse plug-in DB viewer for MySQL database operations.

 

Conventions

Hibernate Synchronize: HS

Key Points

  • Understand Hibernate
  • Make preparations before using Hibernate
  • Use Hibernate Synchronizer for development in Eclipse
  • Summary development skills

Procedure:

1. Preparations

V download the Hibernate framework [this instance uses version 3.2.6] and decompress it to a folder.

 

 

V configure the Hibernate development environment in Eclipse

To facilitate the normal use of Hibernate in eclipse in the future, I will add the jar package in the Hibernate package to the java build path in Eclipse.

Start eclipse, select "window"> "Preferences", select "java"> "build path"> "User library" in the pop-up window, and click "new.

 

You can directly use the new user library when using Hibernate in the future.

 

V install the Hibernate Synchronizer plug-in

 

2. Use Hibernate Synchronize for development in Eclipse

 

V create a data table test

 

V create a java Project

Create a java project, enter the project name, and click Next. Do not click Finish directly]

 

Click the button on the right to add the required package for the project. After adding the package, click Finish.

V create a Hibernate Configuration File

Click the src folder under the project and create a new folder, as shown in figure

 

Click the file you want to create and then click Next. In the pop-up dialog box, select mysql as the database type, and set other attributes. In this dialog box, select com as the Driver Class. mysql. jdbc. driver, Database URL is set to jdbc: mysql: // localhost: 3306/Database name, my own Database name is stu, and others are not explained, because it is very simple and clear.

 

 

V create a mapping File

Create a ing file for the database. The ing file cannot be placed in the default package. Therefore, create a new package first. Click the newly created package to create the Hibernate ing file.

Enter the corresponding password and click Refresh. the table in the database is listed in the list box on the right of the table. Select the data table you want to operate on and select the Package of the file.

The generated code is as follows:

<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC"-//Hibernate/Hibernate Mapping DTD//EN""http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >

 

3. Use HS for synchronization

Use HS for synchronization to automatically generate Hibernate Persistence classes.

Select the Test. hbm. xml file, right-click the file, and choose Hibernate Synchronize → Synchronize Files from the shortcut menu. After synchronization, the directory structure of the project is as follows:

 

 

 

[The TestHibernate. java file in the directory is a post-test file, not automatically generated]

 

The Test. java and BaseTest. java files are generated simultaneously.

 

The next step is to create a ing relationship. Select Test. hbm. xml, right-click it, and choose Hibernate Synchronize → Add Mapping Reference in the pop-up menu.

 

 

 

4. Create and run instances

 

The Code is as follows:

package nba;import org.hibernate.HibernateException;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.Transaction;import org.hibernate.cfg.Configuration;import org.hibernate.classic.*;public class TestHibernate {public static void main(String[] args){try{SessionFactory sf=new Configuration().configure().buildSessionFactory();Session session=sf.openSession();Transaction tx=session.beginTransaction();for(int i=0;i<10;i++){Test TestData=new Test();TestData.setContent("this is a test");TestData.setTitle("kycool");session.save(TestData);}tx.commit();session.close();}catch(HibernateException e){e.printStackTrace();}}}

 

Running result:

The error message in the console prompts me that the configuration file is incorrect and the header information is lost.

Then add the following code:

<?xml version="1.0" encoding="utf-8"?><!DOCTYPE hibernate-configuration PUBLIC"-//Hibernate/Hibernate Configuration DTD//EN""http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd" >

 

Run again

This prompt is related to the id. In the source code, we can find that the class corresponding to the generator is vm, and mysql does not support this operation. Instead, it is changed to naive and then saved and run again.

 

Running successfully, but not perfect, because there are two warn

View Database

 

 

 

 

 

 

 

 

 

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.