Develop hibernate projects with eclipse reverse engineering

Source: Internet
Author: User

At the project design time, either build the entity class based on the requirement analysis, and then generate the database table, or make the database table design first, and then reverse-generate the entity class. That is, the design of the party will not be completed and then take the time to match the establishment of the other party, the reason for this is:

1, if you do not use forward or reverse engineering, it will be 1.5 times times the workload, wasting time

2. There is no guarantee that the mappings on both sides are consistent. Because both sides are designed by themselves, it is necessary to artificially ensure the unity of the mapping relationship, which will leave hidden trouble

3, development, maintenance are unfavorable, because if any party needs to be modified for some reason, then often on both sides to modify, so that the likelihood of inconsistent mapping is not only greater, and efficiency underground

Connect to the database in eclipse

Although eclipse comes with the database Manager (data Management), here I am using plug-in dbviewer, download the plugin into the plugins directory, restart Eclipse, click Window-show View-other-dbviewer plugin-db Tree View, as follows:

Right-click Dbviewerplugin-add, as follows:

In "Database Define name" for the databases to be connected to a name, click the Add file under the JDBC Driver option added to the database connection jar, click Next, as follows:

Fill in the Database configuration information, click Test Connection testing whether to connect to the database, as follows:

Click Finish to complete the database connection as follows:

Download and install the Hibernate tools plugin

Select Help-eclipse Marketplace, enter JBoss tools in the Search box, and click the Install option in JBoss Tools 4.1.2 Fianl as follows:

Here we only need hibernate Tools, so just tick this option, click Confirm, as follows:

The installation process will pop up two prompt boxes, one is to prompt the certificate security, the other is prompted to restart Eclipse, we all click OK.

Developing Hibernate projects

1. Automatically generate Hibernate.cfg.xml files

Start by creating a Java project and importing the hibernate package (you can create a new Lib folder, place the jar package that hibernate must have and the jar package for the database connection to the folder, and then right--add to Build Path).

Right-click the project-new-other and create a new Hibernate.cfg.xml file, as follows:

Since we have already connected the database in eclipse, we can click "Get values from Connection" When configuring the Hibernate.cfg.xml file to read the database connection information previously configured, as follows:

Then select Database dialect and click Finish. In the SRC directory, you will see the auto-generated hibernate.cfg.xml file, as follows:

2. Add Hibernate console Configuration

Right-click on the project-new-other, search hibernate, select Hibernate Console Configuration, and tap Next, as follows:

In name, specify the name of the Hibernate console configuration, select the project in project, and in database connection, select the previously configured DB connection, in the configuration In file, select the previously generated Hibernate.cfg.xml file, click Finish, as follows:

3. Automatically generate Hibernate.reveng.xml files

Right-click on the project-new-other, search hibernate, select Hibernate Reverse Engineering File, tap Next, as follows:

In the console configuration, select the name of the previous configuration (that is, the name entered in Hibernate console configuration), and click Refresh to flush out all schemas in the previously connected database. Clicking on include adds the specified database or table to table filters, where we do not filter any tables, as follows:

Click Finish, there will be a hibernate.reveng.xml file under the project, open the file, click the Table&columns tab, click Add, select the Hibernate console that you just configured Name of the configuration, click OK, select the table to be mapped, as follows:

4. Automatically generate *.hbm.xml files

Click Run-hibernate Code generation-hibernate Code genration configurations, as follows:

Main tab, in the console configuration, select the name configured in the previous Hibernate console configuration and select the location of the automatically generated domain class placement in the output directory. Here we need to build a package (Com.kevin.domain) in the SRC directory to hold the domain class, check the reverse engineer from JDBC Connection, In the Reveng.xml, select the Hibernate.reveng.xml file that you just generated automatically, as follows:

Exporters tab, tick domain code (. java) and Hibernate XML Mappings (. hbm.xml) as follows:

Click Run, you can see the automatically generated domain class and the *.hbm.xml file in the specified directory, as follows:

Note:

1, there is an error because the domain class does not specify the package location, open the Employee.java file, add the package location (such as packages com.kevin.domain;)

2, because the Hibernate.cfg.xml file is not updated automatically at this time, we need to manually add the Object relational mapping file that needs to be managed, as follows:

3, open the Employee.hbm.xml file, you will find that it does not specify the location of the domain class to map, so we need to manually add, that is, package= "Com.kevin.domain", as follows:

4. The type attribute value in the Employee.hbm.xml file can be of type Java (for example, java.lang.String) or hibernate type (such as String), and Java type is generally used.

Develop hibernate projects with eclipse reverse engineering

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.