Eclipse generates hibernate entity classes backwards from the database

Source: Internet
Author: User

Do the project must first make the database table design, and then based on the database design to establish the entity class (VO), which is a matter of course, but in the company to do projects, let me realize, did not say that both after the database design and then "own" to establish a change VO. This means that when the project is designed, either the entity class is built according to the requirement analysis, the database table is generated from the forward direction, or the database table is designed before the entity class is generated in reverse. Did not say that after the design of either side to take the time to match the design of the other side.

The reasons are:

1.1.5 times times the workload, wasting time. (Time is important for the company)

2. There is no guarantee that the mappings are consistent. Because both sides are designed by themselves, then need people to ensure that the mapping relationship is unified, there will be hidden trouble.

3. Development, maintenance is unfavorable. Because if either side needs to be replaced for some reason, it is often necessary to change both sides so that the likelihood of inconsistent mapping is not only greater, but inefficient. And if the use of reverse engineering, it is easy to solve.

So, because of the project, I studied the method of generating hibernate entity class by reverse database.

In MyEclipse, reverse engineering is relatively straightforward, and if it is eclipse, it can be cumbersome because the hibernate environment must be configured first (if you want to build an entity class for another persistence layer framework, you need to configure it first).

How eclipse configures Hibernate environment This is not described here, the Internet search, I give a reference link:

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

So here's the point: how to reverse-Generate hibernate entity classes from a database

1. First, use your own database Manager (data Management) in eclipse to connect to your database:

Then select the database, use Oracle here, and then give a name, such as MyOracle.

Click the button to create a new database-driven configuration.

Select the database version, which is the Oracle 10g version, and you can name it for that configuration.

Then select the appropriate database version of the jar package.

The following is to fill in the database connection configuration according to their own circumstances.

Next, complete the contents of the General tab under Properties, as well as connecting to the database, and then click the Test Connection button when you are finished configuring to connect to the database.

Then click Next, and then confirm the error, clicking Finish.

Data management will then appear myoracle Database Explorer, which changes as follows to indicate that the database configuration was successful.

2. Generating Hibernate entity classes

Open the Hibernate configurations window.

Then right-click on the Open window and click Add Configuration ...

Project Select item (test), Database connection Select the myoracle you just got ( you can actually SELECT [Hibernate configured connection] Connection to the database via Hibernate configuration file, but this way does not necessarily succeed, the use of data Management alone is also not necessarily successful, the reason is not very clear, so the final adoption of data Management + Hibernate configuration file, so it's more insurance.)

Create a new Hibernate-related profile (Hibernate.properties and Hibernate.cfg.xml) anywhere within the project (test).

New hibernate.properties:

New Hibernate.cfg.xml:

After returning to the Edit configuration form, select the Common tab encoding select UTF-8, other default, click OK button

In this way, Hibernate config adds a configuration message such as

Right-click in the blank of the toolbar in the Eclipse menu bar and select Customize perspective

After the form appears, select the Hibernate Code Generation by selecting the Command Groups Availability tab in the diagram, and then click OK.

The toolbar will then appear with the corresponding button.

Click the button and select Hibernate Code Generation configurations in the diagram

Then pop up the window below, press as action, right click after select New

Pop up the form below, select the project address for the output, fill in the package name, tick reverse engineer from JDBC Connection, and create Hibernate.reveng.xml

Create Hibernate.reveng.xml:

Console Configuration Select the hibernate you just configured, click the Refresh button, the database will appear all the schema, find your database, and then include it in the right window, Then click the Finish button to finish creating the Hibernate.reveng.xml.

After returning, select the Exporters tab, tick domain code and hibernate XML Mappings, then click the Run button

The resulting results are as follows:

Eclipse generates hibernate entity classes backwards from the database

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.