Configuration and Mapping

Hibernate applications need two separate sets of configuration information. The first is global metadata that connects the Hibernate framework to your specific persistence store and provides the framework with any information about finding, connecting to, and manipulating that store. The second is the mappings that connect your domain objects to the tables where they will be persisted, also defining the relationships between your classes and how those relationships will manifest in the database.

Global Configuration

Hibernate needs to be told where to find the database that your application will be using and how to connect to it. Additionally, you can configure a variety of services to be provided to your application from the Hibernate framework. You can do this programmatically using the Configuration and Hibernate classes provided in the Hibernate APIs (see "The Hibernate API" later in this chapter) or you can do it declaratively.

The declarative settings can be placed in one of two files:

  • hibernate.properties

  • hibernate.cfg.xml

Both formats offer the same features but with a different syntax. Creating either file and placing it within your application’s classpath will automatically configure Hibernate with the settings therein. If both files are present on the classpath, both are read in, and the hibernate.cfg.xml settings are used to override the settings in the hibernate.properties file. And, of course, you can also override any of these settings programmatically. ...

Get Java Enterprise in a Nutshell, Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.