In this example, the database table corresponding to the customer class is named MERs. The DDL definition in the MySQL database is as follows:
Create Table customers (
Id bigint not null primary key,
Name varchar (15) not null,
Email varchar (128) not null,
Password varchar (8) Not null,
Phone int,
Address varchar (255 ),
Sex char (1 ),
Is_married bit,
Description text,
Image blob,
Birthday date,
Registered_time Timestamp
);
The MERs table has an ID field, which is the primary key of the table and corresponds to the ID attribute of the customer class. Fields in the MERs table use a variety of SQL types. For more information, see table 2-2.
SQL type used by fields in Table 2-2 MERs
2.4 create an object-link ing File
HibernateUse an XML file to specify the ing between objects and relational data. At runtime, Hibernate generates various SQL statements based on the ing file. In this example, a user named customer is created. HBM. XML file, which is used to map the customer class to the MERs table. This file should be consistent with the customer. class files are stored in the same directory. 2-3 is the code of the customer. HBM. xml file.
String 3
Routine 2-3 customer. HBM. xml
// Hibernate/hibernate mapping DTD 2.0.
// En"
Http://hibernate.sourceforge.net
/Hibernate-mapping-2.0.dtd ">
Table = "customers">
Column = "name" type = "string"
Not-null = "true"/> Column = "email" type = "string"
Not-null = "true"/> Column = "password" type = "string"
String 6
Not-null = "true"/> Column = "phone" type = "int"/> Column = "Address" type = "string"/> Column = "sex" type = "character"/> Column = "is_married" type = "Boolean"/> Column = "Description" type = "text"/> Column = "image" type = "binary"/> Column = "Birthday" type = "date"/> Column = "registered_time"
Type = "timestamp"/>
String 9
2.4.1 document type definition (DTD) of the ing File)
The DTD (Document Type Definition, document type definition) is declared at the beginning of the customer. HBM. xml file of the 2-3 routine. It defines the syntax and format of the XML file. The XML Parser of hibernate checks the syntax of the XML file according to the DTD.
Each XML file has its own DTD file. The download URL of the DTD used by the object-link ing file of Hibernate is http: // hibernate.sourceforge.net/hibernate-mapping-2.0.dtd. In addition, hibernate-mapping-2.0.dtd files are also provided under the srcnetsfhibernate directory of the hibernate software package. In this file, the code that describes the top-level elements is as follows:
Import *, (class | subclass | joined-subclass )*,
Query *,
SQL-query *)>
The code for describing the child element of the top-level element is as follows:
Meta *,
(Cache | JCs-Cache )?,
(ID | composite-ID ),
Discriminator ?,
(Version | timestamp )?,
(Property | allow-to-one | one-to-one
| Component | dynamic-component | any
| Map | set | list | bag | idbag | Array
String 3
| Primitive-array )*,
(Subclass *) | (Joined-subclass *))
)>
An element is the root element of the object-link ing file. Other elements (that is, elements within the brackets in the above DTD code, such as child elements) must be embedded within the element. Many child elements are nested in the element.
In the above DTD code, a series of special symbols are used to modify elements. Table 2-3 describes the functions of these symbols. If you are not familiar with the syntax of an element when creating your own object-link ing file, refer to the DTD file.
Table 2-3 functions of special symbols in DTD
As can be seen from table 2-3, in the element, sub-elements such as, and can not exist, or exist once or multiple times. In the element, sub-elements must exist and can only exist once, the element may not exist, or exist once or multiple times.
In addition, in the ing file, the definition of each seed element in the parent element must conform to the specific sequence. For example, you can see from the element DTD that you must define the child element first and then define the child element. The following ing code reverses the position of the child element:
Table = "customers">
String 1
Column = "name" type = "string"
Not-null = "true"/> Column = "email"
Type = "string" not-null = "true"/>
......
The XML Parser of hibernate will throw mappingexception at runtime:
[Java] 21:27:51, 610 error xmlhelper:
48-error parsing XML:
XML inputstream (24)
The content of element type "class"
Must match "(meta *, (Cache | JCs-Cache )?,
(
Id | composite-ID ),
Discriminator ?, (Version | timestamp )?,
(Property | upload-to-one | one-to-one | component |
Dynamic-component | any | map | set
| List | bag | idbag | array | primitive-array )*,
(Subclass * | joined-subclass *))".
String 7
[Java] net. SF. hibernate. mappingexception:
Error reading Resource:
Mypack/customer. HBM. xml
At net. SF. hibernate. cfg. configuration. addclass
(Configuration. Java: 357)
2.4.2 map the customer persistence class to the MERs table
The customer. HBM. xml file of routine 2-3 is used to map the customer class. To map multiple persistence classes, you can map all classes in the same ing file, or create a separate ing file for each class. The ing file has the same name as the class, the extension is "HBM. XML ". The latter approach is more recommendable, because in team development, this facilitates the management and maintenance of ing files.
The element specifies the ing between classes and tables. Its name attribute sets the class name and the table attribute sets the table name. The following code table and the table corresponding to the customer class are the MERs table:
Table = "customers">
If the table attribute of the element is not set, Hibernate uses the class name as the table name. That is, by default, the table corresponding to the mypack. Customer class is the customer table.
An element contains a child element and multiple child elements. The child element sets the oId ing between the persistence class OID and the table's primary key. The following code indicates that the ID attribute of the customer class corresponds to the ID field in the MERs table.
String 1
The child element of the element specifies the object identifier generator, which generates a unique identifier for the oId. Chapter 1 (ing object identifiers) describes in detail the usage of various object identifiers provided by hibernate.
The child element sets the ing between class attributes and table fields. Child elements mainly include the name, type, column, and not-null attributes.
1. Name attribute of the element
The name attribute of the element specifies the name of the property of the persistence class.
2. Type attribute of the element
The Type attribute of the element specifies the hibernate ing type. The Hibernate ing type bridges the Java and SQL types. Table 2-4 lists the Java type, Hibernate ing type, and SQL type of fields in the customers table of the attributes of the customer class.
Table 2-4 mappings between Java, hibernate, and SQL types
From table 2-4, if the property of the customer class is Java. lang. string type, and the corresponding customers table field is varchar type, you should set the hibernate ing type to string, for example:
String 4
Column = "name" type = "string"
Not-null = "true"/>
If the property of the customer class is of the Java. Lang. string type and the corresponding customers table field is of the text type, set the hibernate ing type to text, for example:
Column = "Description" type = "text"/>
If the attributes of the customer class are of the byte [] type and the fields in the corresponding MERs table are of the Blob type, set the hibernate ing type to binary. For example:
Type = "binary"/>
If the ing type is not explicitly set, Hibernate uses the Java reflection mechanism to first identify
From: http://www.javah.net/Hibernate/20070406/93.html