• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

@NamedQuery - Where it should be

 
Ranch Hand
Posts: 270
Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 893
Tomcat Server Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
author
Posts: 580
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Tomcat Server Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
What is that? Is that a mongol hoarde? Can we fend them off with this tiny ad?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic