February 06, 2007

Automatic Table Naming with Hibernate

This article, "Hibernate: Simplify Mapping Documents With Naming Strategies", explains how to use Hibernate's DefaultNamingStrategy to influence the way it derives table names from class names. This can simplify Hibernate mapping files.

In my case, while I'm developing a new application, I'm using the database schema of another application. I would like to prefix all the table names used by my new application to make them easy to identify. I'd like to prefix them with "appname_".

I think the class demonstrated in the article should help me. I think I would rewrite the class' constructor to accept a string parameter of wha I want the table name prefix to be.

Posted by lsloan at 12:41 PM | Comments (0)

January 12, 2007

Using Two Databases with Hibernate

JavaRanch Big Moose Saloon: how to use 2 database in hibrnates.

Just use two configurations. E.g. :

code:


Properties databaseAProperties = // get properties however you like
Properties databaseBProperties = // get properties however you like

SessionFactory databaseA
= new Configuration().configure("databaseA.cfg.xml")
.setProperties(databaseAProperties)
.buildSessionFactory();
SessionFactory databaseB
= new Configuration().configure("databaseB.cfg.xml")
.setProperties(databaseBProperties)
.buildSessionFactory();

Posted by lsloan at 11:03 AM | Comments (0)

January 11, 2007

Hibernate Configuration "Resources"

I'm thinking of using Hibernate in a Java web application to connect to two different databases at the same time. So I looked up Configuration (Hibernate API Documentation) to find this:

configure(String resource)
Use the mappings and properties specified in the given application resource.

Maybe it's because I haven't been using Java very long and I don't know the jargon and common practices very well, but that doesn't look like I should use it to specify a filename. I thought the resource String was supposed to be a string already in memory that contains the configuration.

So I wasted time trying other methods like configure() that takes a File object (didn't work for me) and both versions of addFile (I found later it reads a mapping, not a config).

I learned that I was naive to believe that the API documentation produced by Javadoc would be complete and illuminating. I finally downloaded the Hibernate documentation and I found the version of configure() that takes a String expects the name of the file containing the configuration in that string, not the configuration itself. So it's exactly what I wanted. It wasn't explained in so many words, though. It was in a code sample, but I was able to figure it out quickly.

What's more, I found that I didn't need to specify the full path to the file, either. As long as the file I named is in the classpath, it will be found. That's a big help.

I'm still disappointed with the Hibernate documentation thus far.

Posted by lsloan at 02:30 PM | Comments (0)