Detailed description of ing elements in the configuration file Object link ing is described in an XML document. Ing documents can be generated using tools, such as XDoclet, middlegen, and andromda. The following describes the ing elements from a ing example. The code of the ing file is as follows.
<? XML version = "1.0"?>
<! --
All XML ing files must define the following doctype.
Hibernate will first search for the DTD file in its classptah. -->
<! Doctype hibernate-mapping public
"-// Hibernate/hibernate mapping DTD 3.0 // en"
Http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd> <! --
Hibernate-mapping has several optional attributes:
The schema attribute specifies the schema name of the ing table.
The default-cascade attribute specifies that the default cascade style can be set to none, save, or update.
By default, the auto-import attribute allows you to use the true or false values of a class name with a non-fully qualified name in the query language.
The package attribute specifies the prefix of a package.
--> <Hibernate-mapping schema = "schemaname" default-cascade = "NONE"
Auto-import = "true" package = "test"> <! -- Define a persistent class using the class element -->
<Class name = "people" table = "person">
<! -- The ID element defines the ing between the attribute and the primary key field of the database table. -->
<ID name = "ID">
<! -- Used to generate a unique identifier for the persistence class instance -->
<Generator class = "native"/>
</ID>
<! -- Discriminator is a ing method that defines the inheritance relationship. --> <Discriminator column = "subclass" type = "character"/>
<! -- The property element declares a persistent, JavaBean-style attribute for the class -->
<Property name = "name" type = "string">
<Column name = "name" length = "64" not-null = "true"/>
</Property> <Property name = "sex"
Not-null = "true"
Update = "false"/> <! -- Multi-to-one ing -->
<Peer-to-one name = "friend"
Column = "friend_id"
Update = "false"/> <! -- Set the link --> <Set name = "friends" inverse = "true" Order-by = "ID">
<Key column = "friend_id"/>
<! -One-to-multiple ing -->
<One-to-learn class = "cat"/>
</Set>
</Class>
</Hibernate-mapping>
|
Component Application Method There are two types of components: component and dynamic component ). In the configuration file, the component element that is a sub-object establishes a ing relationship with the fields in the parent table. Then, components can declare their own attributes, components, or collections. The definition of the component element is as follows:
<component name="propertyName" class="className" insert="true|false"
upate="true|false" access="field|property|ClassName"> <property ...../>
<many-to-one .... />
........
</component>
|
In this Code, name refers to the attribute name, class refers to the class name, and insert refers to whether the mapped field appears in the SQL insert statement, upate indicates whether the mapped field exists in the SQL update statement, and access indicates the access attribute policy. Basic configurations of hiebernate The Hibernate database connection information is loaded from the configuration file. The configuration file of hibernate can be in either XML or properties. The default file name of the configuration file in properties form is hibernate. properties. The content of a configuration file in properties form is as follows:
# Specify the driver class used by the database
Hibernate. Connection. driver_class = com. MySQL. JDBC. Driver R
# Specifying database connection strings
Hibernate. Connection. url = JDBC: mysql: // localhost: 3306/DB
# Specify the database connection User Name
Hibernate. Connection. Username = user
# Specify the database connection password
Hibernate. Connection. Password = Password
# Specify the dialect used by the database
Hibernate. dialect = net. SF. hibernate. dialect. mysqldialect
# Specify whether to print SQL statements
Hibernate. show_ SQL = true
|
The configuration file contains a series of attribute configurations. hibernate connects to the Database Based on these attributes. In the xml configuration file, in addition to the basic hibernate configuration information, you can also specify a specific persistence class ing file, this avoids hard coding of configuration files of persistence classes in programs. The default file name of the configuration file in XML format is hibernate. cfg. xml. An example of an xml configuration file is as follows:
<? XML version = '1. 0' encoding = 'utf-8'?> <! Doctype hibernate-configuration public
"-// Hibernate/hibernate configuration DTD 3.0 // en"
Http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd> <Hibernate-configuration>
<Session-factory>
<! -- Display the executed SQL statement -->
<Property name = "show_ SQL"> true </property> <! -- Connection string -->
<Property name = "connection. url"> JDBC: mysql: // localhost: 3306/STU </property> <! -- Database connection username -->
<Property name = "connection. username"> root </property> <! -- Database User Password -->
<Property name = "connection. Password"> root </property> <! -- Database-driven -->
<Property name = "connection. driver_class"> com. MySQL. JDBC. Driver </property> <! -- Select the dialect to use -->
<Property name = "dialect"> org. hibernate. dialect. mysqldialect </property> <! -- Ing file -->
<Mapping Resource = "com/stuman/domain/admin. HBM. xml"/> <! -- Ing file -->
<Mapping Resource = "com/stuman/domain/student. HBM. xml"/> </Session-factory>
</Hibernate-configuration>
|
Properties configuration files and xml configuration files can be used at the same time. When two types of configuration files are used at the same time, the settings in the XML configuration file overwrite the same properties in the properties configuration file. Object Identifier In relational database tables, the primary key is used to identify records and ensure the uniqueness of each record. In Java, you can determine whether the two objects are equal by comparing whether the memory addresses of the objects referenced by the two variables are the same, or comparing whether the values of the objects referenced by the two variables are the same. To solve the differences between the two, Hibernate uses an object identifier (OID) to identify the uniqueness of an object. Oid is the equivalent of a primary key in a relational database in a Java object model. At runtime, Hibernate maintains the correspondence between Java objects and records in database tables based on the oId. The following code calls the load () method of the session three times to load the user objects whose oid is 1 or 3.
Transaction tx = session.beginTransaction();
User user1 = (User)session.load(User.class,new Long(1));
User user2 = (User)session.load(User.class,new Long(1));
User user3 = (User)session.load(User.class,new Long(3));
System.out.println( user1 == user2 );
System.out.println( user1 == user3 );
|
When the application executes the preceding code, it loads the user object whose oid is 1 for the first time, searches for the record whose ID is 1 from the database, and then creates the corresponding user instance, save it in the session cache, and assign the instance reference value to the variable user1. When loading an object whose oid is 1 for the second time, the reference of the instance whose oid is 1 in the session cache is directly assigned to the variable user2. Therefore, the result of expression user1 = user2 is true. You can use different policies to generate identifiers. Table 1 is the built-in identity generation policy of hibernate. Table 1: hibernate identity Generation Policy
Identifier Generator |
Description |
Increment |
Applies to the proxy primary key. It is automatically generated by hibernate in incremental mode. |
Identity |
Applies to the proxy primary key. The identifier generated by the underlying database. |
Sequence |
Applies to the proxy primary key. Hibernate generates identifiers Based on the sequence of the underlying database, which requires that the underlying database supports sequences. |
HiLo |
Applies to the proxy primary key. The identifier generated by the high/low algorithm of the hibernate branch. |
Seqhilo |
Applies to the proxy primary key. Use a high/low level algorithm to efficiently generate long, short, or Int type identifiers. |
Native |
Applies to the proxy primary key. Automatically Select Identity, sequence, or HiLo based on the way the underlying database automatically generates identifiers. |
UUID. HEX |
Applies to the proxy primary key. Hibernate uses the 128-bit UUID algorithm to generate an identifier. |
UUID. String |
Applies to the proxy primary key. UUID is encoded into a 16-character string. |
Assigned |
Applicable to natural primary keys. The Java application is responsible for generating identifiers. |
Foreign |
Applies to the proxy primary key. Use the identifier of another associated object. |
Hibernate ing type In the object/link ing file, Hibernate uses the ing type as a bridge between the Java type and the SQL type. There are two types of hibernate ing: built-in ing type and custom ing type. 1. built-in ing type Hibernate defines built-in ing types for all Java Native types and common Java types such as string and date. Table 2 lists the hibernate ing type, corresponding Java type, and corresponding standard SQL type. Table 2: built-in hibernate ing types
Hibernate ing type |
Java type |
Standard SQL type |
Size |
Integer/int |
Java. Lang. Integer/int |
Integer |
4 bytes |
Long |
Java. Lang. Long/long |
Bigint |
8 bytes |
Short |
Java. Lang. Short/short |
Smallint |
2 bytes |
Byte |
Java. Lang. byte/byte |
Tinyint |
1 byte |
Float |
Java. Lang. Float/float |
Float |
4 bytes |
Double |
Java. Lang. Double/double |
Double |
8 bytes |
Big_decimal |
Java. Math. bigdecimal |
Numeric |
|
Character |
Java. Lang. Character/Java. Lang. String/Char |
Char (1) |
Fixed Length character |
String |
Java. Lang. String |
Varchar |
Variable-length characters |
Boolean/yes_no/true_false |
Java. Lang. boolean/Boolean |
Bit |
Boolean Type |
Date |
Java. util. Date/Java. SQL. Date |
Date |
Date |
Timestamp |
Java. util. Date/Java. util. Timestamp |
Timestamp |
Date |
Calendar |
Java. util. Calendar |
Timestamp |
Date |
Calendar_date |
Java. util. Calendar |
Date |
Date |
Binary |
Byte [] |
Blob |
Blob |
Text |
Java. Lang. String |
Text |
Clob |
Serializable |
Any Java class that implements the java. Io. serializablej Interface |
Blob |
Blob |
Clob |
Java. SQL. clob |
Clob |
Clob |
Blob |
Java. SQL. blob |
Blob |
Blob |
Class |
Java. Lang. Class |
Varchar |
Fixed Length character |
Locale |
Java. util. locale |
Varchar |
Fixed Length character |
Timezone |
Java. util. timezone |
Varchar |
Fixed Length character |
Currency |
Java. util. Currency |
Varchar |
Fixed Length character |
2. Custom ing type Hibernate provides a custom ing type interface that allows you to create custom ing types programmatically. The user-defined ing type must implement the net. SF. hibernate. usertype or net. SF. hibernate. compositeusertype interface. For details about how to create a custom ing type, refer to the official hibernate documentation or relevant materials. |