Hibernate object configuration file *. HBM. xml

Source: Internet
Author: User
Tags cdata
Hibernate object configuration file *. HBM. xml

In hibernate, The ing files of various tables .... HBM. XML can be generated using tools. For example, when using myeclipse for development, it provides tools to automatically generate ing files. This section briefly describes the configurations of these configuration files.

The basic structure of the configuration file is as follows:

<? XML version = "1.0" encoding = 'utf-8'?>

<! Doctype hibernate-mapping public

"-// Hibernate/hibernate mapping DTD 3.0 // en"

Http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd>

<Hibernate-mapping package = "package name">

<Class name = "Class Name" table = "table name">

<ID name = "field name of the primary key in the Java class" column = "corresponding table field" type = "type">

<Generator class = "Primary Key Generation Policy"/>

</ID>

......

</Class>

</Hibernate-mapping>

1. Primary Key (ID)

The primary key generation policies of Hibernate are as follows:

1) assigned

The primary key is generated by an external program and specified before save.

2) HiLo

The primary key generation mechanism implemented by the HI/lo algorithm requires additional database tables or fields to provide a high value source.

3) seqhilo

Similar to hilo, the primary key generation mechanism implemented by the HI/LO algorithm requires sequence in the database, which is suitable for databases that support sequence, such as oracle.

4) Increment

The primary key increments in numerical order. The implementation mechanism of this method is to maintain a variable in the current application instance to save the current maximum value, and then add 1 as the primary key each time the primary key needs to be generated. This method may cause problems: it cannot be used in a cluster.

5) Identity

Use the primary key generation mechanism provided by the database. Such as the primary key generation mechanism in DB2, SQL Server, and MySQL.

6) sequence

Use the sequence mechanism provided by the database to generate the primary key. For example, sequence in oralce.

7) Native

Hibernate uses identity, Hilo, and sequence as the primary key generation method based on the database used.

8) UUID. HEX

Hibernate uses the 128-bit UUID algorithm to generate a hexadecimal value (represented by a 32-Bit String After encoding) as the primary key.

9) UUID. String

Similar to UUID. HEX, the generated primary key is not encoded (Length: 16) and cannot be used in the PostgreSQL database.

10) Foreign

Use the identifier of another associated object as the primary key.

The primary key configuration example is as follows:

<ID name = "ID" column = "ID" type = "Java. Lang. Integer">

<Generator class = "native"/>

</ID>

You can also extend the hibernate class to create your own primary key generation policy. For more information, see http://www.javaeye.com/topic/93391.

2. Common Property)

Developers can open web site: http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd

To view the DTD information of hibernate3.0. The definition of property is as follows:

<! Element property (meta *, (column | formula) *, type?)> <! ATTLIST property name CDATA # required> <! ATTLIST property node CDATA # implied> <! ATTLIST property access CDATA # implied> <! ATTLIST property type CDATA # implied> <! ATTLIST property column CDATA # implied>
<! ATTLIST property length CDATA # implied> <! ATTLIST property precision CDATA # implied> <! ATTLIST property scale CDATA # implied> <! ATTLIST property not-null (true | false) # implied> <! ATTLIST property unique (true | false) "false"> <! ATTLIST property unique-Key
CDATA # implied> <! ATTLIST property index CDATA # implied> <! -- Include the columns spanned by this property in an index --> <! ATTLIST property Update (true | false) # implied> <! ATTLIST property insert (true | false) # implied> <! ATTLIST property optimistic-Lock
(True | false) "true"> <! -- Only supported for properties of a class (not component) --> <! ATTLIST property formula CDATA # implied> <! ATTLIST property lazy (true | false) "false"> <! ATTLIST property generated (never | insert | always) "never">

The following attributes are commonly used: Name (corresponding Java class attribute name), column (corresponding table field), and tyope (attribute type, eg. java. lang. string), not-null (set whether this attribute is null. If it is true, it indicates that it is not null. The default value is false), and length (the length limit of the field ).

Eg1. <property name = "accessname" column = "accessname" type = "Java. Lang. String" not-null = "true"/>

Eg2. <property name = "state" column = "state" type = "Java. Lang. Byte" not-null = "true"/>

Eg3. <property name = "Description" column = "Description" type = "Java. Lang. String"/>

3. One-to-one relationship... /> And <set…> </Set>)

A one-to-multiple relationship is generally used when a table is associated with another table with a foreign key. For example, if the organization ID of a user table is associated with a foreign key of the Organization table, the "one" is the organization table, "Multiple" is a user table, because an organization can contain multiple users, and a user can only belong to one organization.

For a one-to-multiple relationship and multiple-to-one relationship, you must... Configure HBM. xml. In this case, add <set…> </Set> element, because it contains multiple "multiple" objects, the general format is as follows:

<Set name = "attributes of the Java ing Class" inverse = "true" lazy = "true">

<Key column = "corresponding fields in the table"/>

<One-to-multiple class = "multi-party class"/>

</Set>

Eg.

<Set name = "userset" inverse = "true" lazy = "true">

<Key column = "orgid"/>

<One-to-learn class = "user"/>

</Set>

"Multiple" parties (for example, users) belong to a "one" Party object. The general format is as follows:

<Attributes-to-one name = "attributes of the Java ing Class" column = "fields in the table" class = "Class Name" not-null = "true"/>

Eg.

<Role-to-one name = "org" column = "orgid" class = "Organization" not-null = "true"/>

4. One-to-one relationship (<one-to-one... />)

A one-to-one relationship is relatively rare for a one-to-many relationship, but it is also used in some cases. For example, there is a user's basic information table and a user's password table (passwd) there is a one-to-one relationship. Next, let's take a look at the one-to-one relationship configuration in hibernate.

The configuration of the master table (for example, the user's basic information table) is as follows:

<One-to-one name = "attribute name of the child table object of the main table object" class = "Class Name of the child table object" cascade = "Save-Update"/>

Eg. <one-to-one name = "password" class = "com. Amigo. Dao. pojo. passwd" cascade = "Save-Update"/>

The sub-table (e.g. user's password table) is configured as follows:

<One-to-one name = "attribute name of the primary table object in the sub-Table object" class = "Class Name of the primary table object" constrained = "true"/>

Eg. <one-to-one name = "user" class = "com. Amigo. Dao. pojo. User" constrained = "true"/>

5. many-to-many relationships... />)

During database design, many-to-many relationships are generally converted into two one-to-many (or many-to-one) relationships. For example, in a role-based permission system, the relationship between a user and a role is a typical multi-to-many relationship, that is, a user can have multiple roles, and a role can be owned by multiple users, A user-role Association table will be added. This table is associated with the user table and the role table with foreign keys.

This section describes how to configure multiple-to-multiple relationships without decomposition in hibernate. The format is as follows:

<Set name = "Java object attribute name" table = "table name" cascade = "all" outer-join = "false">

<Key column = "table fields"/>

<Role-to-operate class = "object class of another table" column = "Field of another table"/>

</Set>

Eg. The multiple-to-multiple relationship can be expressed:

T_user:

<Set name = "roleset" table = "t_user" cascade = "all" outer-join = "false">

<Key column = "roleid"/>

<Role-to-operate class = "com. Amigo. Dao. pojo. role" column = "roleid"/>

</Set>

T_role:

<Set name = "userset" table = "t_role" cascade = "all" outer-join = "false">

<Key column = "roleid"/>

<Role-to-operate class = "com. Amigo. Dao. pojo. User" column = "roleid"/>

</Set>

6. Complete instance

In this section, we will give some examples of. HBM. xml ing files, so that developers can have a perceptual knowledge of them. Next we will introduce an example of User table (tbl_user), user-role Association Table (tbl_user_role), role table (tbl_role), and Organization table (tbl_organization.

(1) tbl_user

<? XML version = "1.0" encoding = 'utf-8'?>

<! Doctype hibernate-mapping public

"-// Hibernate/hibernate mapping DTD 3.0 // en"

Http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd>

<Hibernate-mapping package = "com. Amigo. Dao. pojo">

<Class name = "user" table = "tbl_user">

<ID name = "loginname" column = "loginname" type = "Java. Lang. String">

<Generator class = "assigned"/>

</ID>

<Property name = "name" column = "name" type = "Java. Lang. String" not-null = "true"/>

<Property name = "password" column = "password" type = "Java. Lang. String" not-null = "true"/>

<Property name = "mobile" column = "mobile" type = "Java. Lang. String"/>

<Property name = "telephone" column = "telephone" type = "Java. Lang. String"/>

<Property name = "email" column = "email" type = "Java. Lang. String"/>

<Property name = "createtime" column = "createtime" type = "Java. util. Date" not-null = "true"/>

<Property name = "lastlogintime" column = "lastlogintime" type = "Java. util. Date"/>

<Property name = "logintimes" column = "logintimes" type = "Java. Lang. Long" not-null = "true"/>

<Property name = "state" column = "state" type = "Java. Lang. Byte" not-null = "true"/>

<Property name = "Description" column = "Description" type = "Java. Lang. String"/>

<Role-to-one name = "Organization" column = "orgid" class = "Organization" not-null = "true"/>

<Set name = "userroleset" inverse = "true" cascade = "all-delete-Orphan" lazy = "true">

<Key column = "loginname"/>

<One-to-define class = "userrole"/>

</Set>

</Hibernate-mapping>

(2) tbl_organization

<? XML version = "1.0" encoding = 'utf-8'?>

<! Doctype hibernate-mapping public

"-// Hibernate/hibernate mapping DTD 3.0 // en"

Http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd>

<Hibernate-mapping package = "com. Amigo. Dao. pojo">

<Class name = "Organization" table = "tbl_organization">

<ID name = "orgid" column = "orgid" type = "Java. Lang. Long">

<Generator class = "native"/>

</ID>

<Property name = "parentorgid" column = "parentorgid" type = "Java. Lang. Long" not-null = "true"/>

<Property name = "orgname" column = "orgname" type = "Java. Lang. String" not-null = "true"/>

<Property name = "orgfullname" column = "orgfullname" type = "Java. Lang. String"/>

<Property name = "orglevel" column = "orglevel" type = "Java. Lang. Integer" not-null = "true"/>

<Property name = "state" column = "state" type = "Java. Lang. Byte" not-null = "true"/>

<Property name = "Description" column = "Description" type = "Java. Lang. String"/>

<Property name = "creator" column = "creator" type = "Java. Lang. String"/>

<Property name = "createtime" column = "createtime" type = "Java. util. Date"/>

<Set name = "userset" inverse = "true" lazy = "true">

<Key column = "orgid"/>

<One-to-learn class = "user"/>

</Set>

</Class>

</Hibernate-mapping>

(3) tbl_user_role

<? XML version = "1.0" encoding = 'utf-8'?>

<! Doctype hibernate-mapping public

"-// Hibernate/hibernate mapping DTD 3.0 // en"

Http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd>

<Hibernate-mapping package = "com. Cotel. netvote. Dao. Model">

<Class name = "userrole" table = "tbl_user_role">

<ID name = "urid" column = "urid" type = "Java. Lang. Integer">

<Generator class = "native"/>

</ID>

<Role-to-one name = "role" column = "roleid" class = "role" not-null = "true"/>

<Role-to-one name = "user" column = "loginname" class = "user" not-null = "true"/>

</Class>

</Hibernate-mapping>

(4) TBL _ Role

<? XML version = "1.0" encoding = 'utf-8'?>

<! Doctype hibernate-mapping public

"-// Hibernate/hibernate mapping DTD 3.0 // en"

Http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd>

<Hibernate-mapping package = "com. Cotel. netvote. Dao. Model">

<Class name = "role" table = "tbl_role">

<ID name = "roleid" column = "roleid" type = "Java. Lang. Integer">

<Generator class = "native"/>

</ID>

<Property name = "rolename" column = "rolename" type = "Java. Lang. String" not-null = "true"/>

<Property name = "createdate" column = "createdate" type = "Java. util. Date" not-null = "true"/>

<Property name = "Description" column = "Description" type = "Java. Lang. String"/>

<Set name = "userroleset" inverse = "true" lazy = "true" cascade = "all">

<Key column = "roleid"/>

<One-to-define class = "userrole"/>

</Set>

</Class>

</Hibernate-mapping>

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.