• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

@NamedQuery - Where it should be

 
Karnati Sudhakar
Ranch Hand
Posts: 270
Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If i want to use Named Queries i have to use @NamedQuery but where to keep them.I mean in which entity class it should be mentioned?
 
Remko Strating
Ranch Hand
Posts: 893
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A good question which I was asking myself also. A simple Query which deals with one Entity is simple. Just put them in the class where you define your entity. This keeps things simple.

I'm now struggling with queries with deals with more entities. In which entity I shall put them. Now I try to use some naming convention where I know where the named query is.

An alternative is defining the named query within the orm.xml. I believe this would be my final place, because it makes controlling and adjusting easier without adjusting your source code.

But I started just with named queries so maybe someone else knows better what is practical the best solution.
 
Ralph Jaus
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Remko Strating wrote:
An alternative is defining the named query within the orm.xml. I believe this would be my final place, because it makes controlling and adjusting easier without adjusting your source code.

I agree that a mapping file is a good place for such kinds of named queries: There the named query can be defined on top-level, independently of any entity class:
 
Reza Rahman
author
Ranch Hand
Posts: 580
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unless you really hate XML, the DD is the best place to put named queries. Otherwise, it is best to put them with the most relevant entity and prefix the name with the entity name such as User.findByName, etc.

Hope it helps,
Reza
 
Remko Strating
Ranch Hand
Posts: 893
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the feedback. I will put the named queries into the orm.xml. The suggested naming convention sounds good for me.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic