Java Reference
In-Depth Information
addCacheableFile(File) : Saves time when Hibernate loads XML mapping files at
startup by caching XML mapping documents on the file system as serialized DOM
Document objects. This improves performance after the first load. Takes a File object
that points to the XML mapping document, not the .bin cache file.
addCacheableFile(String) : Same as addCacheableFile(File) , except this method
takes a path to the file. Hibernate constructs a File object out of the path and then
passes it to addCacheableFile(File) .
addDocument(Document) : Takes a valid DOM Document object containing the XML.
The addJar() and addDirectory() methods are the most convenient, because they allow
you to load all of your Hibernate mapping documents at one time. Both of these methods
simplify code configuration, layout, and refactoring, because you do not have to separately
maintain code that configures each document. We find that it is easy to create a mapping
document and then forget to add it to your application's Hibernate initialization code; using
either of these methods helps to avoid that problem.
As an alternative to specifying the locations of the mapping information in the code, you
can instead use the <mapping> element in the hibernate.cfg.xml XML configuration file. The
<mapping> element has four possible attributes— jar , resource , file , and class —which map
to the addJar() , addResource() , addFile() , and addClass() methods on the Configuration
object.
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM å
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<mapping jar="hibernate-mappings.jar"/>
<mapping resource="com/apress/hibernate/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Whether you use the XML configuration file or directly specify the mapping files in code is
up to you—we suggest that you stick to the approach that you are most comfortable with.
Naming Strategy
If your project has an existing standard for naming database tables or columns, or you would
like to specify exactly how Hibernate maps Java class names to database table names, you can
use Hibernate's naming strategy functionality. Custom naming strategies specify how Hiber-
nate maps Java class names to database table names, properties to column names, and the
name of a table used to hold a collection of properties for a Java class. A naming strategy may
also override the table names and column names specified in the Hibernate mapping docu-
ments—for instance, you might use this to enforce a consistent application-wide prefix to
table names.
Although you can explicitly specify the names of all of the tables and columns in the map-
ping document, if you have clear and consistent rules for naming already, implementing a
custom naming strategy can save a lot of time and frustration. Equally, if you decide to add
Search WWH ::




Custom Search