Hibernate Schema Update Problems

For a while I’ve been having problems trying to get a SchemaUpdate working with Hibernate. Turns out the problem was me, not Hibernate. Newly created tables had a blank ACL, and I just needed to set them.

I’ve been using Hibernate for relational persistence for a while now, and I have to say it’s been working out pretty well.

That was, until I went to do a SchemaUpdate.

The moment I did an alternation of a table, or created a new entity, things went sour, and I was unable to read my old data. Was it me, or was it Hibernate?

could not initialize a collection
PSQLException: ERROR: permission denied for relation newtablejustadded

It was me.

Turns out Hibernate’s update was working just fine. There was no magic versioning or class hashing going on, detecting the change to the database.

The problem was the ACL was blank for the newly created entity table (I was using Postgres).

By issuing this command, all was fine again:

GRANT INSERT, SELECT, UPDATE, DELETE, REFERENCES ON newtable TO GROUP agroupIwasusing;

Since it took a while to figure out what was going on, I thought I’d post this to help others that follow a similar, yet frustrating, path.

Hibernate: Duplicate Mapping and Imports

<GEEK BLOG ENTRY>
I ran into a very frustrating problem this evening, causing me to stay much later than I had intended, and to miss out on some fun socializing event that I was looking forward to. Unfortunately, there was little to no useful information on the Internet as Google was coming up with few and useless results.

I hope this post saves some poor soul from the same fate.

The Problem


I’m using the Hibernate library for persistence with a JBoss EJB using JPA. My code is sprinkled with annotations, my hibernate.cfg.xml file is clean, and I have no *.hbm.xml files. My code compiles. And it runs.

However.

When I try to access something that uses the Hibernate library, I get an odd message about “Duplicate Collection Role Mapping“.

The class in question contained a Set interface and a HashSet implementation for a member.

So, I commented out this container and tried again, hoping to simplify the problem.

This time I was greeted with a “[Mappings] duplicate import” and a “DuplicateMappingException: Duplicate class/entity mapping” set of error messages.

The only related web pages was a handful of archive with people asking similar questions in various online forums.

Almost always these fell into one of three responses:

  1. You’ve got a problem with the mapping element in your hibernate config file.
  2. You’ve got annotations and and class.hbm.xml file doing something wrong.
  3. This is an old bug in JBoss.

None of the symptoms existed in my case.

Here’s How I Solved It


Turns out that Hibernate makes the recommendation that you build a HibernateUtil helper class. Inside it, you’re supposed to make a singleton of the SessionFactory (and in the case of JBoss, you should use JNDI).

A co-worker had refactored the AnnotationConfiguration() to store a single copy, however, the routine that returned it happened to call .configure() on it before returning it each time. An honest mistake, which got integrated silently into my code when I pulled the latest version from version control.

Because .configure() was being called twice, to Hibernate it did look as if I had duplicate mapping directives in my hibernate config file.

Correcting the HibernateUtil method, which handled setting up and returning the AnnotationConfiguration solved the problem.

</GEEK BLOG ENTRY>