• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hibernate Annotations -- NamedQueries

 
Eric Nielsen
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've been trying to establish some named queries in my application using Hibernate Annotations. I've been placing the named query annotation on my Repository classes, but they don't seem to be picked loaded.

From reading the documentation I'm beginning to get the impression that the @NamedQueries is only parsed if the class also has an @Entity annotation (rather than the @Transactional one that I have in this case).

Is this correct? The named queries I'm setting up make more sense to declare at the Repository than Entity level, but if I have to moved them somewhere else I can....
 
Jaikiran Pai
Marshal
Pie
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Based on this documentation, you have to use it with a @Entity or @MappedSuperclass:

Use the @NamedQuery annotation to create pre-defined queries associated with an @Entity or @MappedSuperclass
 
Eric Nielsen
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sigh. Yeah I got it working with the Entity last night. But I think this will be the final annoyance that pushes me to stop using annotations and move back to xml mapping files. The number of non-domain concepts leaking into the Entities is getting too high for my liking.
 
Jaikiran Pai
Marshal
Pie
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Eric Nielsen:
Sigh. Yeah I got it working with the Entity last night. But I think this will be the final annoyance that pushes me to stop using annotations and move back to xml mapping files. The number of non-domain concepts leaking into the Entities is getting too high for my liking.


I do understand what you are saying. The Hibernate documentation has a bit more details about where the named queries can be defined:

2.3.Mapping JPAQL/HQL queries. Mapping JPAQL/HQL queries

You can map EJBQL/HQL queries using annotations. @NamedQuery and @NamedQueries can be defined at the class level or in a JPA XML file. However their definitions are global to the session factory/entity manager factory scope.


Furthermore, that document in Section 2.4.10 talks about Hibernate specific annotations for named queries:

Another key advantage is the ability to set those annotations at a package level.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic