此内容没有您所选择的语言版本。
2.3.4. The Mapping File
Hibernate needs to know how to load and store objects of the persistent class. This is where the Hibernate mapping file comes into play. The mapping file tells Hibernate what table in the database it has to access, and what columns in that table it should use.
The basic structure of a mapping file looks like this:
Hibernate DTD is sophisticated. You can use it for auto-completion of XML mapping elements and attributes in your editor or IDE. Opening up the DTD file in your text editor is the easiest way to get an overview of all elements and attributes, and to view the defaults, as well as some comments. Hibernate will not load the DTD file from the web, but first look it up from the classpath of the application. The DTD file is included in
hibernate-core.jar
(it is also included in the hibernate3.jar
, if using the distribution bundle).
Important
We will omit the DTD declaration in future examples to shorten the code. It is, of course, not optional.
Between the two
hibernate-mapping
tags, include a class
element. All persistent entity classes (again, there might be dependent classes later on, which are not first-class entities) need a mapping to a table in the SQL database:
So far we have told Hibernate how to persist and load object of class
Event
to the table EVENTS
. Each instance is now represented by a row in that table. Now we can continue by mapping the unique identifier property to the tables primary key. As we do not want to care about handling this identifier, we configure Hibernate's identifier generation strategy for a surrogate primary key column:
The
id
element is the declaration of the identifier property. The name="id"
mapping attribute declares the name of the JavaBean property and tells Hibernate to use the getId()
and setId()
methods to access the property. The column attribute tells Hibernate which column of the EVENTS
table holds the primary key value.
The nested
generator
element specifies the identifier generation strategy (aka how are identifier values generated?). In this case we choose native
, which offers a level of portability depending on the configured database dialect. Hibernate supports database generated, globally unique, as well as application assigned, identifiers. Identifier value generation is also one of Hibernate's many extension points and you can plugin in your own strategy.
Note
native
is no longer considered the best strategy in terms of portability.
Lastly, we need to tell Hibernate about the remaining entity class properties. By default, no properties of the class are considered persistent:
Similar to the
id
element, the name
attribute of the property
element tells Hibernate which getter and setter methods to use. In this case, Hibernate will search for getDate()
, setDate()
, getTitle()
and setTitle()
methods.
Note
Why does the
date
property mapping include the column
attribute, but the title
does not? Without the column
attribute, Hibernate by default uses the property name as the column name. This works for title
, however, date
is a reserved keyword in most databases so you will need to map it to a different name.
The
title
mapping also lacks a type
attribute. The types declared and used in the mapping files are not Java data types; they are not SQL database types either. These types are called Hibernate mapping types, converters which can translate from Java to SQL data types and vice versa. Again, Hibernate will try to determine the correct conversion and mapping type itself if the type
attribute is not present in the mapping. In some cases this automatic detection using Reflection on the Java class might not have the default you expect or need. This is the case with the date
property. Hibernate cannot know if the property, which is of java.util.Date
, should map to a SQL date
, timestamp
, or time
column. Full date and time information is preserved by mapping the property with a timestamp
converter.
Note
Hibernate makes this mapping type determination using reflection when the mapping files are processed. This can take time and resources, so if startup performance is important you should consider explicitly defining the type to use.
Save this mapping file as
src/main/resources/org/hibernate/tutorial/domain/Event.hbm.xml
.