An interesting Hibernate 3.2 annotation gotcha

Here I am working on a project and using Hibernate Annotations 3.2 CR1 and Hibernate 3.2 RC2 (because the annotations CR1 won’t work with CR4) and I ran into a little gotcha. I had just created a few persistent classes and marked them with @Entity and then I setup the annotation configuration in my Ant script and ran it. Nothing happened. That’s not entirely true. The script ran but it never showed that it was processing any classes. Whaaaat? So I try running it by manually creating the configuration and running the SchemaExport class myself. Still nothing. So I start debugging and I see my classes getting added to the AnnotationConfiguration instance but when it comes time to process them, they’re gone. Same instance of AnnotationConfiguration so I start tracing deeper. Then I find what I’m looking for. It only processes classes that are marked with the Entity annotation (MappedSuperclass also but that’s not important right now). The javax.persistence Entity annotation. I had accidentally selected the Hibernate Entity extension annotation when I did an organize imports in Eclipse. Doh!

So I start looking through the documentation and there it is on page 29 of the annotations manual,

@javax.persistence.Entity is still mandatory, @org.hibernate.annotations.Entity is not a replacement.

I will have to file this one away and remember it. While it is documented I think it might be a good idea to rename the Hibernate Entity annotation because as it is, you have to name it absolutely since you must have the EJB3 Entity annotation regardless. And we all know that developers always read the manual before they start coding with a new library. At a minimum I would put a check in to spit out a warning message if a class has an instance of the Hibernate Entity annotation but not the EJB3 Entity annotation. Maybe I’ll submit a patch this weekend since this was bugging me so much.

If you come across this kind of behavior, just double check you imported the right Entity annotation in your code.

One thought on “An interesting Hibernate 3.2 annotation gotcha

  1. Simple trick: Never import org.hibernate.annotations, but write them fully qualified. Makes it easier to see what comes from Hibernate and what from JPA. Also helps searching for them, you’ll get all of them in your search result. Btw, new releases of HAN and HEM are out.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s