Hibernate automatically generates mapping files

Source: Internet
Author: User

The mapping file is the key to the O/R mapping, equivalent to the control center. Manually configuring the mapping file is time-consuming when there are many database tables. In order to develop the program quickly and focus the developer's attention on the business logic, hibernate officially provides the Middlegen tool, which makes it easy to generate mapping files. Download here: Http://prdownloads.sourceforge.net/hibernate/?sort_by=date&sort=desc

Middlegen is based on a database table to generate the mapping file, so configure it with the database. The specific configuration is as follows:

[1] Modifying the database configuration file:

Entering the Config/database subdirectory under the Middlegen directory, you can see the configuration files with many different databases. Depending on our database selection, for example I am using MySQL, so open the Mysql.xml file. The contents of the file are as follows:

<property name= "Database.script.file" value= "/${src.dir}/sql/${name}-mysql.sql"/>

<!--Configure the JDBC driver file here, be sure to have that driver in the Middlegen Lib directory.

<property name= "Database.driver.file" value= "/${lib.dir}/mm.mysql-2.0.14-bin.jar"/>

<property name= "Database.driver.classpath" value= "/${database.driver.file}"/>

<property name= "Database.driver" value= "/org.gjt.mm.mysql.driver"/>

<!--configuration Database url-->

<property name= "Database.url" value= "Jdbc:mysql:///sports"/>

<!--user name--

<property name= "Database.userid" value= "hiswing"/>

<!--password--

<property name= "Database.password" value= "123"/>

<property name= "Database.schema" value= ""/>

<property name= "Database.catalog" value= ""/>

<property name= "jboss.datasource.mapping" value= "MySQL"/>

[2] Modify the Build.xml file:

The file is placed under the root directory of the Middlegen. It is the ant build file for middlegen-hibernate. Middlegen-hibernate is the mapping file generated by the specific parameter data of the Build.xml file. The changes are as follows:

<!--The specified database configuration file, that is, the file in [1], the default is ./config/database/hsqldb.xml-->

<! DOCTYPE Project [

<! ENTITY database SYSTEM "File:./config/database/mysql.xml" >

]>

<!--Configuration Application Name, default is Airline-->

<property name= "name" value= "Sports"/>

<!--config file output directory, default is ${build.dir}/gen-src-->

<property name= "Build.gen-src.dir" value= "E:\Programme\java\sports"/>

<!--Configure a package corresponding to JavaBean (PO), package= "${name}.hibernate" by default

<!--Genxdoclettags is set to True when the generated code will contain Xdoclet tag, which can be used to adjust the mapping file--


destination= "${build.gen-src.dir}"

Package= "Com.cuitao.sports.po"

Genxdoclettags= "true"

Genintergratedcompositekeys= "false"

Javatypemapper= "Middlegen.plugins.hibernate.HibernateJavaTypeMapper"

/>

At this point, the Middlegen-hibernate configuration is complete. Next, you'll use Ant to help us start middlegen-hibernate. (can be downloaded from Apache website)

Use MS-DOS to enter the root directory of Middlegen and run Ant, which will start the Middlegen-hibernate interface. Before you do this, don't forget to start the database:)

Class name of Domain class Name:po

Key Generator: Primary key generator. Some of these options, the general choice of Uuid.hex mode to generate the primary key will provide the best performance and database platform adaptability.

Schema name: Database schema name

Persister: Custom durable class implementation class name. If the system has a persistence layer implementation mechanism other than hibernate, such as getting a dataset from a stored procedure or even fetching data from LDAP to populate our PO.

Enable proxies: Agent for lazy loading [lazy Loading].

Dynamic Update: When the item is selected, generating an Update SQL will not contain the field properties that have not changed, which can improve the efficiency of SQL execution.

Implement the Lifecyle interface: whether to implement the Lifecyle interface. After implementing the Lifecyle interface, we can trigger the specified operation before the database operation.

The above configuration is for the class. The following will be configured for the property.

Hibernate Mapping Specialty: Map type key= primary key, property= attribute.

Java Property Name: The attribute name of the PO that corresponds to the database field.

Java type: The data type of the PO attribute that corresponds to the database field.

Finally, click the Generate button at the top of the window, and Middlegen will automatically generate the Hibernate mapping files for those database tables. The mapping file is a. hbm.xml suffix, and a mapping file corresponds to a table of the database.

Hibernate automatically generates mapping files

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.