Detailed explanation of hibernate.hbm.xml

Keywords: Hibernate Java xml Attribute

stay hibernate In the mapping file of each table. hbm.xml can be generated by tools, such as MyEclipse, which provides tools for automatically generating mapping files. The basic structure of the configuration file is as follows:

Xml Code

  1. <?xmlversion="1.0"encoding='UTF-8'?>  
  2.   
  3. <!DOCTYPE hibernate-mapping PUBLIC   
  4.      "-//Hibernate/Hibernate Mapping DTD 3.0//EN"   
  5.      "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">  
  6. <hibernate-mappingpackage="Package name">  
  7.    <classname="Class name"table="Table name">  
  8.       <idname="Primary key java Field names in classes"column="Fields in corresponding tables"type="type ">  
  9.           <generatorclass="Primary key generation strategy"/>  
  10.       </id>  
  11.   
  12.           ……   
  13.     </class>  
  14. </hibernate-mapping>   

1. Primary key (id)
Hibernate's primary key generation strategies are as follows:
1) assigned
The primary key is generated by an external program and specified before save().
2) hilo
Through hi/lo algorithm The implementation of the primary key generation mechanism requires additionaldata base Tables or fields provide high-level value sources.
3) seqhilo
Similar to hi lo, the primary key generation mechanism implemented by hi/lo algorithm requires data base In Sequence, it is suitable for databases that support Sequence, such as Oracle.
4) increment
Primary keys increase in numerical order. The implementation mechanism of this method is to maintain a variable in the current application instance to preserve the current maximum value, and then add 1 as the primary key every time the primary key needs to be generated. The problem with this approach is that it cannot be used in clusters.
5) identity
The primary key generation mechanism provided by the database is adopted. Such as DB2,SQL Server,MySQL Primary key generation mechanism.
6) sequence
The sequence mechanism provided by the database is used to generate the primary key. For example, Sequence in Oralce.
7) native
Hibernate uses identity, hilo and sequence as the main key generation method according to the database used.
8) uuid.hex
Hibernate generates hexadecimal values based on 128-bit UUID algorithm (encoded in 32-length strings) as the primary key.
9) uuid.string
Similar to uuid.hex, the generated primary key is not encoded (length 16) and cannot be applied in PostgreSQL data.
10) foreign
Use the identifier of another associated object as the primary key.
Examples of primary key configuration are as follows:

  1. <idname="id"column="id"type="java.lang.Integer">  
  2.              <generatorclass="native"/>  
  3. </id>   

In addition, Hibernate's class can be extended to make its own primary key generation strategy, for example, http://www.advertisement.com/topic/93391.
2. property
Developers can open the Web site: http://hibernate.sourceforge .NET/hibernate-mapping-3.0.dtd
To view the dtd information of Hibernate 3.0, you can see that the definition of property is as follows:

Xml code
  1. <!ELEMENT property (meta*,(column|formula)*,type?)>  
  2. <!ATTLIST property name CDATA #REQUIRED>  
  3. <!ATTLIST property node CDATA #IMPLIED>  
  4. <!ATTLIST property access CDATA #IMPLIED>  
  5. <!ATTLIST property type CDATA #IMPLIED>  
  6. <!ATTLIST property column CDATA #IMPLIED>  
  7. <!ATTLIST property length CDATA #IMPLIED>  
  8. <!ATTLIST property precision CDATA #IMPLIED>  
  9. <!ATTLIST property scale CDATA #IMPLIED>  
  10. <!ATTLIST property not-null (true|false) #IMPLIED>  
  11. <!ATTLIST property unique (true|false) "false">  
  12. <!ATTLIST property unique-key CDATA #IMPLIED>  
  13. <!ATTLISTpropertyindexCDATA#IMPLIED>      
  14. <!-- include the columns spanned by this property in an index -->  
  15. <!ATTLIST property update (true|false) #IMPLIED>  
  16. <!ATTLIST property insert (true|false) #IMPLIED>  
  17. <!ATTLIST property optimistic-lock (true|false) "true">           
  18. <!-- only supported for properties of a class (not component) -->  
  19. <!ATTLIST property formula CDATA #IMPLIED>  
  20. <!ATTLIST property lazy (true|false) "false">  
  21. <!ATTLIST property generated (never|insert|always) "never">   

Among its attributes, name is commonly used. Java Class attribute name, column (field in the corresponding table), type (type of attribute, eg.java.lang.String), not-null (set whether the attribute is empty, true denotes non-empty, default false) and length (field length limit). Examples are as follows:

Xml code
  1. <propertyname="accessname"column="accessName"type="java.lang.String"not-null="true"/>  
  2. <propertyname="state"column="state"type="java.lang.Byte"not-null="true"/>  
  3. <propertyname="description"column="description"type="java.lang.String"/>  

3. One-to-one relationship (<many-to-one... And > <set... ></set>)

One-to-many relationship is usually used when one table has foreign key association with another table. For example, if the organizational id of a user table has foreign key association with an organizational table, the "one" side is an organizational table and the "many" side is a user table, because an organization can contain multiple users, while a user can only belong to one organization.

For both parties with one-to-many relationship and many-to-one relationship, it is necessary to ___________ Configuration is done in hbm.xml, when the "one" side (e. g. organization) needs to add < set () to the mapping file. > The </set> element, because it contains multiple "multi" objects, has the following general format:

  1. <setname="java The corresponding attributes in the mapping class"inverse="true"lazy="true">  
  2.   <keycolumn="The corresponding fields in the table"/>  
  3.      <one-to-manyclass="Multiparty classes"/>  
  4. </set>  
  5.   
  6. <!-- Example -->  
  7. <setname="userSet"inverse="true"lazy="true">  
  8.       <keycolumn="orgId"/>  
  9.       <one-to-manyclass="User"/>  
  10. </set>   

The "multi" party (e.g. user) belongs to a "one" party object. The general format is as follows:

  1. <many-to-onename="java The corresponding attributes in the mapping class"column="The corresponding fields in the table"class="Class name"not-null="true"/>  
  2.   
  3. <!-- Example -->  
  4. <many-to-onename="org"column="orgId"class="Organization"not-null="true"/>   

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

One-to-one relationship is relatively rare for one-to-many relationship, but it is also used in some cases, such as having a user's basic information table (USER) and a user's password table (PASSWD). Let's look at the one-to-one relationship configuration in Hibernate.

Xml code
  1. <!-- Configuration of the main table (eg. User's basic information table) -->.
  2.   
  3. <one-to-one name= "attribute name of the neutron table object of the main table object" class= "class name of the child table object" cascade= "save-update"/>.
  4.   
  5. <one-to-onename="password"class="com.amigo.dao.pojo.Passwd"cascade="save-update"/>  
  6.   
  7. <! - Configuration of subtables (eg. User's password table) --> ___________
  8.   
  9. <one-to-one name= "attribute name of main table object in sub-table object" class= "class name of main table object" constrained= "true"/>.
  10.   
  11. <one-to-onename="user"class="com.amigo.dao.pojo.User "constrained="true"/>   

5. Many-to-many relationships (<many-to-many (<many-to-many) / >)

In database design, many-to-many relationship is usually transformed into two-to-many (or more-to-one) relationships. For example, in role-based permission system, the relationship between users and roles is a typical one-to-many relationship, that is, a user can have multiple roles, and a role can be owned by multiple users. Generally, a user and role are added to the design. Association tables, which have foreign key associations with user tables and role tables.

In this section, we describe how to configure a non-decomposed many-to-many relationship in Hibernate. The format is as follows:

Xml code
  1. <setname="java Object's attribute name"table="Table name"cascade="all"outer-join="false">     
  2.     <keycolumn="Corresponding fields of tables"/>     
  3.     <many-to-manyclass="Object class of another table"column="Fields of another table"/>     
  4. </set>     
  5.   
  6. <!-- t_user square -->  
  7. <setname="roleSet"table="t_user"cascade="all"outer-join="false">     
  8.     <keycolumn="roleId"/>     
  9.     <many-to-manyclass="com.amigo.dao.pojo.Role"column="roleId"/>     
  10. </set>     
  11.   
  12. <!-- t_role square -->  
  13. <setname="userSet"table="t_role"cascade="all"outer-join="false">     
  14.     <keycolumn="roleId"/>     
  15.     <many-to-manyclass="com.amigo.dao.pojo.User"column="roleId"/>     
  16. </set>

Posted by satanclaus on Tue, 26 Mar 2019 11:27:30 -0700