Ing between XDoclet and Hibernate

Source: Internet
Author: User

Integrates the automatic generation mechanism of XDoclet ing files in pojo, and provides the functions of saving manual encoding and exporting from the database.
The third option of the basic code.

XDoclet has been widely used in EJB development. In its latest version, it contains
The subclass library hibernate doclet contains the ant build support required to generate the hibernate ing file and
Java Doc tag support.
The basic principle of XDoclet implementation is to add a specific javadoc tag to the Java code to add a specific
And then use the XDoclet tool to analyze the javadoc tag in the code.
XDoclet.
In hibernate-doclet, by introducing the hibernate-related javadoc tag, we can generate
The corresponding hibernate ing file.
The following code snippet demonstrates how to use hibernate-doclet:
/**
*@ Hibernate. Class
*Table = "Tuser"
*/
Public class Tuser implements serializable {
......
/**
*@ Hibernate. Property
*Column = "name"
*Length = "50"
*Not-null = "true"
*
* @ Return string
*/
Public String getname (){
Return this. Name;
}
......
}
The above uses hibernate-doclet to describe the ing between pojo (Tuser) and its corresponding table (Tuser ).
.

Two hibernate doclet tags are used., @ Hibernate. Class and @ hibernate. Property.
The two tags respectively describe the database table information corresponding to pojo and the database table field information corresponding to its fields.
Then hibernate doclet will generate a ing file based on the information:
<Hibernate-mapping>
<Class
Name = "net. xiaxin. XDoclet. Tuser"
Table = "Tuser"
>
<Property
Name = "name"
Type = "Java. Lang. String"
Column = "name"
Not-null = "true"
Length = "50"
>
</Class>
</Hibernate-mapping>
In this way, you only need to maintain the Java code, and you do not need to manually write a specific ing file to complete hibernate.
Basic code.

Next we will discuss the commonly used tags in hibernate doclet. For detailed tag reference, see
XDoclet official guide (Http://xdoclet.sourceforge.net/xdoclet/tags/hibernate-tags.html) And
Hibernate reference (Http://www.hibernate.org).

Common hibernate-doclet tag introduction:
1. class level:
1) @ hibernate. Class
Describes the poing between pojo and database tables, and specifies relevant running parameters.
The parameter description type is required.
Table name corresponding to the table class. Default Value: Text N
When dynamic-update generates an update SQL statement, it only contains the changed fields. Default Value: false bool n
When an insert SQL statement is generated by dynamic-insert, it only contains non-null fields. Default Value: false bool n
Proxy proxy class. Default Value: NULL Text N

The ID of the discriminator-value subclass, which is supported by polymorphism. Text N
Where data selection conditions. If you only need to process certain data in the database table, you can use this option to set the results set conditions. If the user table stores all

User data, while our system is only for Shanghai users, you can specify where = "location = 'shanghai'" Text N

Typical scenarios:
/**
* @ Hibernate. Class
* Table = "Tuser" (1)
* Dynamic-update = "true" (2)
* Dynamic-insert = "true" (3)
* Proxy = "" (4)
* Discriminator-value = "1" (5)
*/
Public class Tuser implements serializable {
......
}
In this example:
1. The table parameter specifies the database table "Tuser" corresponding to the current class (Tuser ".
2 The Dynamic-update parameter is set to include only the currently changed fields (Improving the Performance of DB update) When an update SQL statement is generated ).
3. When the dynamic-insert parameter is set to generate an insert SQL statement, only non-empty fields are included. (Improves dB insert performance)
4. The proxy parameter is null, indicating that the current class does not use proxy ). The role of the proxy class is to provide support for lazy. Loading. For more information, see lazy loading.
5. The discriminator-value parameter is set to "1 ". The purpose of the discriminator-value parameter is to support polymorphism. For more information, see @ hibernate. discriminator.

2) @ hibernate. Discriminator
@ Hibernate. discriminator is used to provide support for polymorphism.
The parameter description type is required.
Column is used to distinguish the field names of each subclass. Default Value: Text y
Hibernate type bool n corresponding to type
Length Field Length bool n
For example:
The Tuser class corresponds to the database table Tuser, and the user class has two Derived classes: SysAdmin and sysoperator.
In the Tuser table, user types are differentiated based on the user_type field.
To enable hibernate to automatically identify the corresponding class type based on user_type (for example, user_type = 1)
It is automatically mapped to the SysAdmin class, And user_type = 2 is automatically mapped to the sysoperator class), we need
Configure the ing file. In hibernate-doclet, the corresponding @ hibernate. discriminator identifier and @ hibernate. Class and @ hibernate. Subclass discriminator-value attributes are matched.

Typical scenarios:
/**
*
* @ Hibernate. Class
* Table = "Tuser"
* Dynamic-update = "true"
* Dynamic-insert = "true"
*
* @ Hibernate. discriminator column = "user_type" type = "integer"
*/
Public class Tuser implements serializable {
......
}
In the root class Tuser, @ hibernate. discriminator specifies the field "user_type"
As the recognition field.
/**
* @ Hibernate. Subclass
* Discriminator-value = "1"
*/
Public class SysAdmin extends Tuser {
......
}
/**
* @ Hibernate. Subclass
* Discriminator-value = "2"
*/
Public class sysoperator extends Tuser {
......
}
Both SysAdmin and sysoperator inherit from Tuser, and their discriminator-value are set separately.
For "1" and "2", during runtime, when hibernate reads data from the t_user table, it performs
If it is 1, it is mapped to the SysAdmin class. If it is 2, it is mapped to the sysoperator class.

In the preceding example, when SysAdmin and sysoperator are described, a tag is introduced:
@ Hibernate. subclass, as the name suggests, @ hibernate. subclass and @ hibernate. Class
The difference is that @ hibernate. Subclass describes a subclass. In fact, the two tags
Except for different names, there is no difference.

2. Method layer:
1) @ hibernate. ID
Describes the ing between the key fields in pojo and the primary keys in the database table.
The parameter description type is required.
Column primary key field name, default value: current Class Name text n
Type field type. Hibernate always uses the object data type as the field type, for example, int corresponds to integer, so here we set the ID as the basic type [such as int] to avoid
The idea of creating overhead is meaningless. Even if it is set as the basic type, Hibernate still uses Object-type data to process it internally, it is only converted to the basic type when the data is returned. Text N
Length Field Length Text N
Unsaved-value is used to determine whether the object has been saved. For more information, see "data access. Text N
The generator-class primary key generation method (For details, refer to the description of middlegen in hibernate Quickstart) can be any of the following values:
Assigned, Hilo, seqhilo, increment, identity, sequence, native, UUID. HEX, UUID. String, foreign text y

2) @ hibernate. Property
Describes the ing between attributes in pojo and database table fields.
The parameter description type is required.
Column database table field name, default value: current Class Name text n
Type field type text n
Length Field Length Text N
Whether the not-null field can be empty bool n
Whether the unique field is unique (whether repeated values are allowed) bool n
Whether the insert operation contains the data of this field. Default Value: True bool n
Whether the update operation contains the data of this field. Default Value: True bool n

Typical scenarios:
/**
* @ Hibernate. Property
* Column = "name"
* Length = "50"
* Not-null = "true"
*
* @ Return string
*/
Public String getname (){
Return this. Name;
}
Note: When writing code, set the getter/setter method of pojo to public. If
If it is set to private, Hibernate will not be able to optimize the access to attributes. Instead, it can only use the traditional reflection mechanism.
This will cause a lot of performance overhead (especially in the Sun JDK version earlier than 1.4 and ibm jdk,
Reflection brings considerable system overhead ).

Notice: ****************** how to use XDoclet to generate a ing File

The code containing the XDoclet tag must be processed by the XDoclet program to generate the corresponding ing file,
The XDoclet processing module can be loaded through ant. Below is a simple ant of hibernate XDoclet.
Build the script (note that you need to adjust the path and classpath settings according to the actual situation ):
<? XML version = "1.0"?>
<Project name = "hibernate" default = "hibernate" basedir = ".">
<Property name = "XDoclet. Lib. Home" value = "C:/xdoclet-1.2.1/lib"/>
<Target name = "hibernate" depends = "" Description = "generates hibernate class descriptor files.">
<Taskdef name = "hibernatedoclet" classname = "XDoclet. modules. hibernate. hibernatedoclettask">
<Classpath>
<Fileset dir = "$ {XDoclet. Lib. Home}">
<Include name = "*. Jar"/>
</Fileset>
</Classpath>
</Taskdef>
<Hibernatedoclet destdir = "./src/" excludedtags = "@ version, @ author, @ todo" force = "true" verbose = "true" mergedir = ".">
<Fileset dir = "./src/">
<Include name = "**/hibernate/sample/*. Java"/>
</Fileset>
<Hibernate version = "2.0"/>
</Hibernatedoclet>
</Target>
</Project>
In addition to the hibernate doclet tag we introduced above, there are also:
Class level;
@ Hibernate. Cache
@ Hibernate. JCs-Cache
@ Hibernate. Joined-subclass
@ Hibernate. Joined-subclass-Key
@ Hibernate. Query
Method Layer
@ Hibernate. Array
@ Hibernate. Bag
@ Hibernate. Collection-Cache
@ Hibernate. Collection-composite-Element
@ Hibernate. Collection-Element
@ Hibernate. Collection-Index
@ Hibernate. Collection-JCs-Cache
@ Hibernate. Collection-Key
@ Hibernate. Collection-key-Column
@ Hibernate. Collection-transform-to-Sequence
@ Hibernate. Collection-one-to-least
@ Hibernate. Column
@ Hibernate. Component
@ Hibernate. Generator-Param
@ Hibernate. Index-example-to-Example
@ Hibernate. List
@ Hibernate. Allow-to-one
@ Hibernate. Map
@ Hibernate. One-to-one
@ Hibernate. Primitive-array
@ Hibernate. Set
@ Hibernate. Timestamp
@ Hibernate. Version
For detailed tag description, see tag description 1 on the XDoclet official website.

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.