Eclipse reverse generates hibernate entity classes from the database

Source: Internet
Author: User

To create a project, you must first design a database table and then create an object class (VO) based on the database design. However, after working on a project in the company, I realized that, it is not said that after the database design is completed, you have to "Create and change yourself" to VO. This means that during the project design, you can either create an entity class based on the requirement analysis, and generate a database table from the forward direction. Alternatively, you can design a database table first and then generate an entity class in reverse order. It is not said that the design of either party is completed, and then it takes time to match the design of the other party.

The reason is:

1. 1.5 times of work, a waste of time. (Time is important to the company)

2. The mappings between the two sides cannot be consistent. Because both sides are self-designed, We need to manually ensure that the ing relationships are unified, which poses a hidden risk.

3. both development and maintenance are unfavorable. If either party needs to change to another one for some reason, it is often necessary to change the two sides. In this case, the possibility of inconsistent mappings is not only greater, but also inefficient. However, if positive and reverse engineering is used, it is easy to solve the problem.

Therefore, for project reasons, I studied how to reverse generate the hibernate entity class from the database.

In myeclipse, the direct and reverse engineering operations are relatively simple, and if it is eclipse, it will be more troublesome, because you must configure the hibernate environment first (Similarly, if you want to generate entity classes of other persistence layer frameworks, you also need to configure them first ).

The method for configuring the hibernate environment in eclipse is not described here. Go to the Internet to search for it. I will provide a reference link:

Http://hi.baidu.com/java513/blog/item/6be18a3c5d79daf43c6d9742.html

The following describes how to reverse generate a hibernate object class from a database:

1. First, use the built-in data management in eclipse to connect your database:

 

Then select the database, Here oracle is used, and then give a name, such as myoracle.

Click in to create a new database driver configuration.

Select the database version. Here is the Oracle 10g version. You can name this configuration separately.

Select the jar package of the corresponding database version.

Enter the database connection configuration according to your own situation.

Next, complete the General tab content under properties, which is also the information for connecting to the database. After configuration, click test connection to test whether the database can be connected.

Click Next, confirm the correctness, and click Finish.

Data Management then displays the database resource manager of myoracle. The following changes indicate that the database is successfully configured.

2. Generate a hibernate object class

Open the hibernate deployments window.

Right-click the window and click Add configuration...

Project select project (test), database connection select the configured myoracle (in fact, you can directly select [hibernate configured connection] to connect to the database through the hibernate configuration file, but this method is not necessarily successful, using data management alone is also not necessarily successful. The reason is not very clear, so we finally adopt the combination of data management + hibernate configuration files, which is more secure)

Create a configuration file (hibernate. properties and hibernate. cfg. XML) for hibernate anywhere in the project (test ).

Create hibernate. properties:

Create hibernate. cfg. xml:

Return to the edit configuration form, select encoding in the common tab to select UTF-8, Other Default, click OK

In this way, a configuration information is added to the hibernate configuration, as shown in figure

In the eclipse menu bar, right-click the blank area in the toolbar and select customize perspective.

After the following form is displayed, select the command groups availability tab, select hibernate code generation, and click OK.

Then the corresponding button appears in the toolbar.

Click this button and select hibernate code generation deployments in the figure

The following window is displayed, as shown in the following figure. Right-click the window and select new

The following window is displayed. Select the output project address, enter the package name, check reverse engineer from JDBC connection, and create hibernate. reveng. xml.

Create hibernate. reveng. xml:

In the console configuration, select the configured Hibernate and click the refresh button. All schemas of the database are displayed. Find your database and include it in the right window, then click Finish to complete hibernate. reveng. XML creation.

Select the exporters tab, check domain code and hibernate XML mappings, and then click the run button.

The final result is as follows:

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.