XDoclet with hibernate mapping

xiaoxiao2021-04-05  276

The mapping file automatic generation mechanism in POJO is fused, providing a third option that exports the base code from the database.

XDoclet has been widely used in EJB development, in its latest version, contains a sub-library Hibernate Doclet for Hibernate, which contains the ANT build support required to generate the Hibernate mapping file and Java Doc TAG support. The basic principle of XDoclet is to add specific additional semantics to the Java code, and then analyze the JavaDoc TAG in the code, automatically generate configuration files, xDoclets, and XDoclets, the XDoclets, and XDoclets, which are automatically generated by XDoclet tools. In Hibernate-Doclets, we can generate a corresponding Hibernate mapping file by code to generate a corresponding Hibernate mapping file by code. Below is a code snippet, demonstrating the use of hibernate-doclet: / *** @ hibernate.class * Table = "tuse" * / public class tusements serializable {... / *** @Hibernate. Property * column = "name" * length = "50" * not-null = "true" ** @return string * / public string getName () {return this.name;} ...} Hibernate-Doclet describes an example of mapping between POJO (TUSER) and its corresponding table (TUSER).

Among them, two hibernate doclet tags, @ hibernate.class, and @ hibernate.property. These two TAGs describe the database table information corresponding to the POJO, and the library table field information corresponding to the field. After the Hibernate Doclet generates mapping files based on this information: This we only need to maintain Java code without having to manually write a specific mapping file to complete the Hibernate foundation Code.

Below we are discussed on the TAG commonly used in Hibernate Doclets, for details on TAG, see the official guide (http://xdoclet.sourceforge.net/xdoclet/tags/Hibernate-tags.html) and Hibernate Reference (http) : //www.hibernate.org).

Common Hibernate-Doclet Tag: 1. Class Level: 1) @ hibernate.class describes the mapping relationship between POJO and database tables and specifies the relevant running parameters. The parameter description type must be a table name corresponding to the Table class: When the current class name text nDynamic-update generates the UPDATE SQL, only the variable field, the default value: false bool nDynamic-insert generates INSERT SQL, only non-empty (NULL) field, default: false Bool NProxy agent class, default: empty TEXT NDISCRIMINATOR-VALUE subclusion identifier for polymorphism support. TEXT NWHERE Data Selection Conditions If you only need to process some specific data in the library table, you can set the result set limit. Such as the user's table holds all the country

The user's data, and our system is just a Shanghai user, you can specify where = "location = 'Shanghai'" Text N

Typical scenario: / *** @ hibernate.class * Table = "tuse" (1) * Dynamic-update = "true" (2) * Dynamic-insert = "true" (3) * proxy = "" (4) * Discriminator-value = "1" (5) * / public class tusements serializable {...} In this example: 1 Table parameter Specifies the current class (TUSER) corresponding to the database table "Tuser". 2 Dynamic-Update Parameter Set to generate Update SQL, only include the currently changing field (improve DB Update performance). 3 Dynamic-INSERT parameters are set to generate Insert SQL, including only current non-empty fields. (Improve DB INSERT Performance) 4 Proxy parameters are empty, indicating that the current class does not use the agent (Proxy). The role of the agency class is to provide support for lazy.loading, see About Lazy Loading. 5 Discriminator-value parameter is set to "1". The purpose of the Discriminator-Value parameter is to support polymorphism. See the description of @ hibernate.discriminator. 2) @ hibernate.discriminator @ hibernate.discriminator (identifier) ​​is used to provide polymorphism support. The parameter description type must be coluMN to distinguish the field names of each subclass. Default: The current class name text ytype corresponds to the Hibernate type Bool Nlength field length Bool N as: Tuser class corresponds to the database table Tuser, and the User class has two derived class sysadmin, sysoperator. In the Tuser table, distinguish the user type according to the USER_TYPE field. In order to let Hibernate automatically identify the corresponding Class type according to USER_TYPE (such as user_type == 1, the user_type == 2 automatically maps to the Sysoperator class), we need to configure in the mapping file, and in Hibernate- In the Doclet, the corresponding is @ hibernate.discriminator ID and @ hibernate.class, and @ hibernate.subclass Discriminator-Value properties.

Typical scenario: / **** @ hibernate.class * Table = "tuse" * Dynamic-update = "true" * Dynamic-insert = "true" ** @ hibernate.discriminator column = "user_type" type = "integer" * / Public Class Tuser IMPLEments Serializable {...} Root Tuser, specified with the "User_Type" field as the identification field by @ hibernate.discriminator. / *** @ hibernate.subclass * Discriminator-value = "1" * / public class sysadmin extends tuser {...} / *** @ hibernate.subclass * Discriminator-value = "2" * / public Class Sysoperator Extends Tuser {......} Sysadmin and sysoperator are inherited from Tuser, and its Discriminator-Value is set to "1" and "2", and the runtime hibernate will be based on its USER_TYPE when reading the T_User table The field is determined if it is 1, map to the sysadmin class, if 2 is mapped to the Sysoperator class. In the above example, when describing sysadmin and sysoperator, we introduced a Tag: @ hibernate.subclass, as the name suggestions, @ hibernate.subclass and @ hibernate.class Different, @ hibernate.subclass description is a subclass, actually On the two TAGs, there is no difference in the different names.

2. Method Level: 1) @ hibernate.id Describes the mapping relationship between key fields and database table primary keys in POJO. The parameter description type must be a column primary key field name, default: The current class name text ntype field type. Hibernate always uses the object type data type as a field type, such as INT corresponds to Integer, so set the ID to the basic type [such as int] to avoid the idea of ​​the overhead of the object, even if it is set to the basic type, Hibernate still uses object type data to handle it, just converts the data to a basic type when returning data. Text Nlength Field Length Text Nunsaved-Value is used for the determination value that the object has been saved. See the discussion of the "Data Access" section for details. Text NGenerator-Class primary key generation method (see related instructions in Hibernate QuickStart) Value to any of the following: Assigned, Hilo, Seqhilo, Increment, Identity, Sequence, Native, UUID.HEX, UUID. String, Foreign TEXT Y2) @ hibernate.property Describes the mapping relationship between the properties and database table fields in POJO. Parameter Description Types Must Column Database Table Field Name, Default: Current Class Name Text NTYPE Field Type Text NLENGTH Field Length Text NNOT-NULL field Allows whether the BOOL NUNIQUE field is unique (whether to allow duplicate values) BOOL NINSERT INSERT Whether Contains this field data, default: True Bool NUPDATE UPDATE operation When you include this field data, default: True Bool N

Typical scene: / *** @ hibernate.property * column = "name" * length = "50" * not-null = "true" ** @return string * / public string getname () {Return this.name;} Note: When writing code, set the POJO's getter / setter method to public, if set to private, Hibernate will not optimize the attribute access, can only be operated using traditional reflex mechanisms This will result in a large number of performance overhead (especially the Sun JDK version before 1.4 and the IBM JDK, the system overhead brought about by the reflection is considerable). NOTICE: **************** How to generate mapping files using XDoclet

The code containing the xdoclet tag must be processed by the XDoclet program to generate a corresponding mapping file. The XDoclet's processing module can be loaded through Ant. The following is a simple Hibernate XDoclet Ant build script (note the actual situation when actual) Adjusted with ClassPath settings): In addition to the Hibernate Doclet Tag we introduced above, others also: Class level; @ hibernate.cache @ hibernate.jcs-cache @ hibernate.joid-subclass @ hibernate.joined-subclass-key @ hibernate.querymeth @ hibernate.Array @ Hiberna te.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-man-to-many @

转载请注明原文地址:https://www.9cbs.com/read-132191.html

New Post(0)