<meta attribute="???">xxxx</meta>的用法 - hibernate配置文件生成java类
Chapter 7. Controlling POJO code generation
When using the <hbm2java>
tag or the Eclipse plugin to generate POJO Java code you have the ability to control certain aspects of the code generation process. This is primarily done with the <meta>
tag in the mapping files. The following section describes the possible <meta>
tags and their use.
The following example shows how to use various <meta>
attributes and the resulting Java code.
<class name="Person"> <meta attribute="class-description"> Javadoc for the Person class @author Frodo </meta> <meta attribute="implements">IAuditable</meta> <id name="id" type="long"> <meta attribute="scope-set">protected</meta> <generator class="increment"/> </id> <property name="name" type="string"> <meta attribute="field-description">The name of the person</meta> </property> </class>
// default package import java.io.Serializable; import org.apache.commons.lang.builder.EqualsBuilder; import org.apache.commons.lang.builder.HashCodeBuilder; import org.apache.commons.lang.builder.ToStringBuilder; /** * Javadoc for the Person class * @author Frodo */ public class Person implements Serializable, IAuditable { public Long id; public String name; public Person(java.lang.String name) { this.name = name; } public Person() { } public java.lang.Long getId() { return this.id; } protected void setId(java.lang.Long id) { this.id = id; } /** * The name of the person */ public java.lang.String getName() { return this.name; } public void setName(java.lang.String name) { this.name = name; } }
Table 7.1. Supported meta tags
Attribute | Description |
---|---|
class-description | inserted into the Javadoc for classes |
field-description | inserted into the Javadoc for fields and properties |
interface | If true, an interface is generated instead of an class. |
implements | interface the class should implement |
extends | class that the current class should extend (ignored for subclasses) |
generated-class | overrule the name of the actual class generated |
scope-class | scope for class |
scope-set | scope for setter method |
scope-get | scope for getter method |
scope-field | scope for actual field |
default-value | Default initialization value for a field. |
use-in-tostring | Include this property in the toString() method. |
use-in-equals | Include this property in the equals() and hashCode() methods. If no use-in-equals is specified, no equals or hashcode method will be generated. |
gen-property | Property will not be generated if false (use with care). |
property-type | Overrides the default type of property. Use this with any tag's to specify the concrete type instead of just Object . |
class-code | Extra code that will inserted at the end of the class |
extra-import | Extra import that will inserted at the end of all other imports |
Attributes declared via the <meta>
tag "inherited"
inside an hbm.xml
file by default.
What does that mean? As an example if you want to have all your classes implement IAuditable
then you just add <meta attribute="implements">IAuditable</meta>
in the top of the hbm.xml
file, just after <hibernate-mapping>
. Now all classes defined in that hbm.xml
file will implement IAuditable
.
Note:
This applies to all <meta>
-tags. Thus it can also be used to specify that all fields should be declareprotected
, instead of the default private
. This is done by adding<meta attribute="scope-field">protected</meta>
just under the <class>
tag, and all fields of that class will be protected
.
To avoid having a <meta>
tag inherited then you can specify inherit = "false"
for the attribute. For example<meta attribute = "scope-class" inherit = "false">public abstract</meta>
will restrict the "class-scope"
to the current class, not the subclasses.
The following are some good practices to employ when using <meta>
attributes.
<hibernate-mapping> <class name="Person"> <meta attribute="use-in-tostring">true</meta> <meta attribute="use-in-equals">true</meta> ... </class> </hibernate-mapping>
<hibernate-mapping> <class name="events.Event" table="EVENTS"> <meta attribute="use-in-tostring">true</meta> <meta attribute="use-in-equals">true</meta> <id name="id" column="EVENT_ID"> <generator class="native"/> </id> <property name="date" type="timestamp" column="EVENT_DATE"/> <property name="title"/> <set name="participants" table="PERSON_EVENT" inverse="true"> <key column="EVENT_ID"/> <many-to-many column="PERSON_ID" class="events.Person"/> </set> </class> </hibernate-mapping>
Instead it is recommended that the meta
attributes are defined at the property level, like so:
<hibernate-mapping> <class name="events.Event" table="EVENTS"> <id name="id" column="EVENT_ID"> <meta attribute="use-in-tostring">true</meta> <generator class="native"/> </id> <property name="date" type="timestamp" column="EVENT_DATE"/> <property name="title"> <meta attribute="use-in-tostring">true</meta> <meta attribute="use-in-equals">true</meta> </property> <set name="participants" table="PERSON_EVENT" inverse="true"> <key column="EVENT_ID"/> <many-to-many column="PERSON_ID" class="events.Person"/> </set> </class> </hibernate-mapping>
<hibernate-mapping> <class name="Person"> <meta attribute="class-description"> Javadoc for the Person class @author Frodo </meta> <meta attribute="implements">IAuditable</meta> <id name="id" type="long"> <meta attribute="scope-set">protected</meta> <meta attribute="use-in-tostring">true</meta> <generator class="increment"/> </id> <property name="name" type="string"> <meta attribute="field-description">The name of the person</meta> <meta attribute="use-in-tostring">true</meta> </property> </class> </hibernate-mapping>
On www.hibernate.org you can find more in-depth explanation on the subject of equals()
and hashcode()
methods.
As its name indicates, this file is used to generate property accessors for POJOs.
The contents of the file will be something like this:
<#foreach property in pojo.getAllPropertiesIterator()> ${pojo.getPropertyGetModifiers(property)} ${pojo.getJavaTypeName(property, jdk5)} ${pojo.getGetterSignature(property)}() { return this.${property.name}; } ${pojo.getPropertySetModifiers(property)} void set${pojo.getPropertyName(property)} (${pojo.getJavaTypeName(property, jdk5)} ${property.name}) { this.${property.name} = ${property.name}; } </#foreach>
<#foreach property in pojo.getAllPropertiesIterator()> ${pojo.getPropertyGetModifiers(property)} ${pojo.getJavaTypeName(property, jdk5)} ${pojo.getGetterSignature(property)}() { return this.${property.name}; } ${pojo.getPropertySetModifiers(property)} void set${pojo.getPropertyName(property)} (${pojo.getJavaTypeName(property, jdk5)} ${property.name}) { <#if pojo.hasMetaAttribute(property, "pre-cond")> ${c2j.getMetaAsString(property, "pre-cond","\n")} </#if> this.${property.name} = ${property.name}; <#if pojo.hasMetaAttribute(property, "post-cond")> ${c2j.getMetaAsString(property, "post-cond","\n")} </#if> } </#foreach>
<hibernate-mapping> <class name="Person"> <id name="id" type="long"> <generator class="increment"/> </id> <property name="firstName" type="string"> <meta attribute="pre-cond"> if ((firstName != null) && (firstName.length() == 0) ) { throw new IllegalArgumentException("firstName can not be an empty String"); } </meta> </property> </class> </hibernate-mapping>
<target name="hbm2java"> <taskdef name="hibernatetool" classname="org.hibernate.tool.ant.HibernateToolTask" classpathref="lib.classpath"/> <hibernatetool destdir="${hbm2java.dest.dir}" templatepath="${hbm.template.path}"> <classpath> <path refid="pojo.classpath"/> </classpath> <configuration> <fileset dir="${hbm2java.src.dir}"> <include name="**/*.hbm.xml"/> </fileset> </configuration> <hbm2java/> </hibernatetool> </target>
Invoking the target <hbm2java>
will generate file Person.java
in ${hbm2java.dest.dir}
:
// default package import java.io.Serializable; public class Person implements Serializable { public Long id; public String name; public Person(java.lang.String name) { this.name = name; } public Person() { } public java.lang.Long getId() { return this.id; } public void setId(java.lang.Long id) { this.id = id; } public java.lang.String getName() { return this.name; } public void setName(java.lang.String name) { if ((name != null) && (name.length() == 0)) { throw new IllegalArgumentException("name can not be an empty String"); } this.name = name; } }
In Chapter 4, Eclipse Plugins you've learned about a set of wizards for creating Mapping files, Configuration files, Console Configurations, become familiar with Mapping and Configuration files editors, tooling for organizing and controlling Reverse Engineering, Hibernate Console and Mapping diagrams.
The rest chapters have explored the use of the Hibernate Tools™ via Ant tasks.
Please visit JBoss Tools Users Forum to leave questions or/and suggestions on the topic. Your feedback is always appreciated.