views:

165

answers:

2

I have a fairly large (new) project in which we have annotated many domain classes with JPA mappings. Now it is time to implement many named queries -- some entities may have as many as 15-20 named queries. I am thinking that writing these named queries in annotations will clutter the source files and therefore am considering putting these in XML mapping files.

Is this possible?

Mort importantly, is this reasonable?

Are there better approaches?

How is this done?

+1  A: 

It's possible, but I don't think it's needed. I work on a lot of big projects with many named queries attached to some entities and I don't think that this clutters the source much - after all the queries are all before the class definition. The main advantage of using annotations is that you can see everything in the source. If you've extracted the queries in an xml config the presence of the named queries won't be immediately visible which I'd consider a drawback. I like to keep stuff pure - either xml only setup or only annotations setup. The only xml config I generally keep around on JPA projects in the persistence.xml.

Bozhidar Batsov
+1  A: 

Is this possible?

Yes it is, but the trend is more to centralize things, not the inverse.

More importantly, is this reasonable?

I am not annoyed by a having a block of annotations at the top of my entities class files. Actually, I like to have my queries where I think they belong: next to entities. I also like the compile time checks (on entity names, attributes) and the code completion I get when writing queries in the Java code (not sure my IDE would do that with xml mappings). In other words, I don't feel the need and don't want to externalize queries .

Are there better approaches?

I believe that using annotations is the best practice1.

How is this done?

The recommendation is to use XML mapping files only for native SQL statements that are specific to a particular database (of course, I omit the obvious case of legacy code that you can't annotate). In other words, use annotations but keep the code as free from vendor-specific stuff as possible.

1 The JPA 1.0 specification co-lead Mike Keith covered many of the trade-offs associated with an XML metadata strategy (XML strategy) versus an in-source metadata strategy (annotations strategy) in the OTN column "To Annotate or Not". Sadly, I couldn't find a non-dead link to his publication. Maybe you'll be more lucky and in that case, read it.

Pascal Thivent
Thanks to the way-back-machine, the internet never forgets anything. Check it out at:http://web.archive.org/web/20041027182132/http://www.oracle.com/technology/pub/articles/annotations_xml.htmlThe original article however is from 2004 and is less complimentary of annotations that I would have guessed. Nonetheless you and Bozhidar have convinced me that I might as well put up with the extra lines of text and stick the named queries in the Java code. It's not like somebody is ever going to need to tweak these at a customer-site or anything.
HDave