Java Reference
In-Depth Information
a prefix to all database table names after the fact, it is easy to do so with a naming strategy,
while it would be a pain to correct these in every Hibernate mapping document.
n Note Using Hibernate with an existing well-specified database often means creating a custom naming
strategy for Hibernate. If the database tables have a prefix, it may be cleaner to implement a naming strategy
that adds that prefix than to specify the full table name with a prefix in every Hibernate mapping document.
A custom naming strategy must implement the org.hibernate.cfg.NamingStrategy inter-
face or extend one of the two provided naming strategy classes, org.hibernate.cfg.
DefaultNamingStrategy or org.hibernate.cfg.ImprovedNamingStrategy . The default naming
strategy simply returns the unqualified Java class name as the database table name. For
instance, the table name for the Java class com.hibernatebook.AccessGroups would be
AccessGroups . The column name would be the same as the property name, and the collection
table would have the same name as the property.
The improved naming strategy adds underscores in place of uppercase letters in mixed-
case table and column names, and then lowercases the name. For instance, the same com.
hibernatebook.AccessGroups Java class would correspond to a database table named
access_groups .
Neither of these naming strategies takes into account the case in which you have two
classes with the same name in different packages in your application. For instance, if you had
two classes, com.hibernatebook.webcast.Group and com.hibernatebook.security.Group , both
would default to a table named Group , which is not what you want. You would have to explic-
itly set the table name in the mapping of at least one class.
Once you have created a naming strategy, pass an instance of it to the Configuration
object's setNamingStrategy() method, as follows:
public Configuration setNamingStrategy(NamingStrategy namingStrategy)
You must call this method before building the session factory from the Configuration . For
example, here's the code for using the ImprovedNamingStrategy naming strategy:
Configuration conf = new Configuration()
conf.setNamingStrategy(ImprovedNamingStrategy.INSTANCE);
Using a Container-Managed Data Source
When running in an environment with a JNDI server, Hibernate can obtain a data source
through a JNDI lookup. You must use the hibernate.connection.datasource property to spec-
ify the JNDI name, and then you may set the optional hibernate.jndi.url and hibernate.
jndi.class properties to specify the location of the container's JNDI provider and the class
name of the container's implementation of the JNDI InitialContextFactory interface. Yo u
may also use the hibernate.connection.username and hibernate.connection.password prop-
erties to specify the database user your application uses. For example, your hibernate.
properties file might have these lines for a WebLogic 7.0 managed data source:
Search WWH ::




Custom Search