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:
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,
Independent Consultant — Author, EJB 3 in Action — Expert Group Member, Java EE 6 and EJB 3.1