"Go" Eclipse generates Hibernate entity classes from the database backwards

Source: Internet
Author: User
Tags jboss

Original address: http://blog.csdn.net/wangpeng047/article/details/6877720

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://www.cnblogs.com/yeing/p/3770609.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:

"Go" Manually install Hibernate tools for Eclipse (Hibernate reverse engineering)

People who have used MyEclipse know that MyEclipse has integrated a hibernate can automatically generate mapping mappings based on database tables (XML

or annotation) and Java's Pojo class tools. In fact, Hibernate official website has to provide a tool like this, of course, now

The name is JBoss Tools, and Hibernate tools is just a small part of it.

For people like me who are myeclipse to the bloated and unacceptable, the use of eclipseee is a good choice, and now we

You can manually install the plug-in on eclipseee. My eclipse is the latest version, only 200 m after decompression (compare MyEclipse

The current latest version 8.5 installs to occupy 1G space ~)

The installation of Hibernate tools is started below:

1. Enter this page Http://jboss.org/tools/download/stable/3_1_GA.htmlJBoss Tools 3.1.1.GA Stable release This is the latest version, Since I just need hibernate tools, I don't have to download the rest of the section here. Select Hibernate tools 14.75M 3.3.1.v201006011046r-h111-ga for download

2, downloaded after the decompression to get two folders, features and plugins, cut to eclipse\myplugins\hibernatetools\, of course, Eclipse is not myplugins\ Hibernatetools This directory, you need to create a new, of course, you can also name yourself, but the next step to use the directory, so the best directory names do not have Chinese, and do not have spaces. (These steps are the same as the previous link installation of the Eclipse plug-in, which should be done by yourself)

3. Create a new directory in the Eclipse directory links (no new if already exists), create a new file in this directory Hibernatetools.link, the content is: Path=d:/program files/eclipse/myplugins/ Hibernatetools I use absolute paths here, you can also use relative paths.



4, restart Eclipse, in file-"new" other, see the following Hibernate configuration item is already installed successfully

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.