Eclipse uses Hibernate tools to generate Hibernate mapping files and PO classes

Source: Internet
Author: User
Tags generator mysql in uuid jboss

As the saying goes, "change is always quicker than plan." In the project, in our programmers are also often encountered problems. Because even if the demand investigation is more detailed, often will also change. In order to adapt to change, it is often one of our goals to construct easy to maintain and scalable projects.

When using the database, I believe many programmers will have the same trouble as me. Is the database changes, the project hibernate configuration of the chain reaction, mapping mapping needs to be modified, PO class needs to change. Using the Hibernate Tools in Jbosstools will solve this big problem for us. I. Development environment

JBoss Tools Download

Http://www.jboss.org/tools/download.html

Here I am choosing the way to install online. And the eclipse version is 3.6.2, so my choice is: http://www.jboss.org/tools/download/installation/update_3_2.html

(Outside: After installation, you will find Eclipse's performance is greatly increased.) In addition to HTML, JSP and other editors, there are many Java plug-in plug-ins)

Second, configure Hibernate configurations 1. Open Control Window

Click Window-open view-other

Open Hibernate configurations 2.add configuration under the Hibernate folder

Configure Database Connection Resources


The MySQL in the figure is the database connection I created previously, and if not configured previously, you can click New and configure the database connection.

Check the database to see if the test is normal.


Three, Hibernate code generator 1. Add Hibernate Code Generation button

Click Window-customize Perspective


2. Click Hibernate Code Generation Configuration


3. Create, manage code generation configuration


Console configuration: Select the database connection we created in the 2nd. Output Directory: Configure the directory after execution, generate HBM, PO class, and so on. Package: Package Name, please enter the PO Class store path. Reveng.xml: Generates important configuration files for mapping and PO classes. Reveng.xml can be created here if it is not generated.
3.hibernate.reveng.xml

Reveng.xml file allows us to control the various properties of the HBM and PO classes after the plug-in is generated.

The following are the references:

  XML code   <?xml version= "1.0"  encoding= "UTF-8"?>   <! doctype hibernate-reverse-engineering public  "-//hibernate/hibernate reverse  engineering dtd 3.0//en " " http://hibernate.sourceforge.net/ Hibernate-reverse-engineering-3.0.dtd " >     

1. Through type-mapping code blocks, we are free to map out the corresponding fields in the database fields and hibernate.

XML code <sql-type jdbc-type= "VARCHAR" length= "1" hibernate-type= "Yes_no"/>

A field that varchar type and length 1 in the database is mapped to a Boolean property of the Po class.

XML code <sql-type jdbc-type= "VARCHAR" Length= "not-null=" false "Hibernate-type=" Cn.com.timekey.drugmon Itor.po.type.RoleEnumType "/>

In the same way, this means that the varchar type in the database, the length of 21 of the field is mapped to my custom hibernate type.

2. Through the Table-filter code block, we can filter and specify the target table of the script.

XML code <table-filter match-catalog= "CMM" match-name= ". *"/>

This means: All tables under the CMM database.

In some cases, our script does not perform all of the tables, and it's easy to help us achieve this.

3.table code block, provided to us to overwrite the generated mapping configuration.

XML code <table name= "user" catalog= "CMM" > <primary-key> <generator class= "Uuid.hex"/> <key-column name= "ID"/> </primary-key> </table>

Here I believe you should be more easily guessed, is to play the primary key generation rule in the specified user table is Uuid.hex.

4. Custom FOREIGN Key configuration

XML code <foreign-key constraint-name= "Prescription_drug_ibfk_3" > <column-ref local-column= "DRUG_ID"/> <many-to-one property= "Drug" cascade= "save-update"/> </foreign-key>

Also within the <table> code block.

Constraint-name: Constraint name, the constraint name corresponding to the foreign key.

Column-ref: Configure the Foreign key field name.

Many-to-one: Configure the foreign key parameter, including the property name, and so on, for example, the level of adding cascade is save-update. The default is null, that is, none.

More Reveng.xml instructions can refer to: http://docs.jboss.org/tools/3.2.0.GA/en/hibernatetools/html_single/index.html#hibernaterevengxmlfile

4. Select the exported file


Here I choose to generate mapping HBM mapping file and PO class. DAO classes are typically used in their own, and often in environments that are integrated with spring, so they are not very useful. Cfg.xml file is also a bit of use, import hbm.xml mapping file, you can refer to this file to copy.

5. Run the configuration file

All OK after that, press the run key to generate the mapping mapping file and the PO class we want.

PostScript

The generated PO class does not generate a serial number I am rather tangled, but from a developer's point of view, this is understandable.


http://kennylee26.iteye.com/blog/1039542

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.