Hibernate tool: hbm2java or hbm2ddl

Source: Internet
Author: User
To simplify development, Hibernate provides four tools: hbm2java, hbm2ddl, XDoclet, and middlegen. I think hbm2java (generates Java source files based on the ing file) and hbm2ddl (generates database files based on the ing file. These two tools are very practical. there is also XDoclet (which generates ing files based on Java source files marked with XDoclet ). middlegen (the tool for generating ing files based on database files is also good. but I have never used it. so I will not talk about it here. now let's create an object-link ing file to demonstrate how to use these two tools. first, create an XML ing file. the name is demo. HBM. XML

<? XML version = "1.0" encoding = "UTF-8"?>
<! Doctype hibernate-mapping public "-// hibernate/hibernate mapping DTD 2.0 // en" "hibernate-mapping-2.0.dtd">
<Hibernate-mapping>
<Class name = "hibernate. Demo" table = "demos">
<Meta attribute = "Class-description"> tooldemo </meta>
<Meta attribute = "Class-scope"> Public </meta>

<ID name = "ID" type = "long" column = "ID">
<Meta attribute = "scope-set"> protected </meta>
<Generator class = "native"/>
</ID>

<Property name = "name" type = "string">
<Meta attribute = "Finder-method"> findbyname </meta>
<Meta attribute = "use-in-tostring"> true </meta>
<Column name = "name" length = "15" not-null = "true" unique = "true"/>
</Property>

<Property name = "registeredtime" type = "timestamp">
<Meta attribute = "field-description"> when the demo </meta>
<Meta attribute = "use-in-tostring"> true </meta>
<Column name = "registered_time" Index = "idx_registered_time" SQL-type = "timestamp"/>
</Property>

<Property name = "Age" type = "int">
<Meta attribute = "field-description"> How old is the demo </meta>
<Meta attribute = "use-in-tostring"> true </meta>
<Column name = "Age" Check = "age> 10" not-null = "true"/>
</Property>

<Property name = "sex" column = "sex" type = "char"> </property>

<Property name = "married" type = "Boolean" column = "is_married">
<Meta attribute = "field-description"> is the demo married </meta>
<Meta attribute = "use-in-tostring"> true </meta>
</Property>

<Property name = "Description" type = "string">
<Meta attribute = "use-in-tostring"> true </meta>
<Column name = "Description" SQL-type = "text"/>
</Property>
</Class>
</Hibernate-mapping>

The preceding XML file is explained. The <meta> element is used to precisely control the content of the Java source file.
<Meta attribute = "Class-description"> tooldemo </meta> the comment of the source file class is used to generate javadoc.
<Meta attribute = "Class-scope"> Public </meta>: the modifier of the class.
<Meta attribute = "extends"> hibernate. Tool </meta> declares the class to which this class belongs. This class inherits the tool class under the hibernate package.
<Meta attribute = "field-description"> is the demo married </meta>. It is also a comment for generating javadoc.
<Meta attribute = "use-in-tostring"> true </meta> specifies whether the string returned by the tostring () method of the class contains this attribute.
<Meta attribute = "scope-set"> protected </meta> specifies the get or set method modifier for the attributes of the class, including static public final privete and so on.

The usage of all the attributes of the <meta> element is listed below.
Class-description specifies the class annotation, javadoc
Field-description specifies the annotation of the class property, javadoc
If the interface is true, it indicates that the generated interface is not a class. The default value is false.
Implements specifies the interface to be implemented by the class
Extends specifies the class to be inherited
Generated-class regenerate Class Name
Scope-class specifies the class modifier. The default value is public.
Scope-set: modifies the set method. The default value is public.
Scope-Get: modifies the get method. The default value is public.
Scope-field: modifies the attributes of a class. The default value is public.
If use-in-tostring is true, this attribute is included in the tostring () method of the class.
If the value of Gen-property is false, this attribute is not generated in the class. The default value is true.
Finder-method: Set the find method name

All attributes and usage of the <column> element are listed below
Name: Set the name of the table field.
Length: Set the length of a table field.
Not-null if true indicates that the field cannot be null. The default value is false.
If Unique is true, set the field uniqueness constraint. The default value is false.
Index to create an index for one or more fields
Unique-key: Set unique constraints for multiple fields
Freign-key is the name of the foreign key constraint.
SQL-type: Set the SQL type of the field
Check sets SQL check Constraints

The above is the basic knowledge. Remember, it's okay that hbm2ddl Is In The Hibernate software package, while hbm2java tool is in the hibernate extension package. I really don't understand why Hibernate is doing this, so it is troublesome, the two tools used together should be put in two separate packages ah, on www.hibernate.org can be downloaded to a separate hibernate extension package, hibernate-tools.jar put under our classpath, and then create build. you can use ant to run the two tools in XML.

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.