Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NamedQuery: Best practices

When creating named queries in JPA, is there an accepted best practice for the names of these queries (eg. EntityName.allActive or findAllActiveFoos etc.) and also is it good to declare these named queries in the entity classes that they query or all together in a utility class?

like image 881
Clinton Bosch Avatar asked Jun 07 '12 15:06

Clinton Bosch


People also ask

How do you use NamedQuery?

We can use @NamedQuery annotation to specify a named query within an entity class and then declare that method in repository. Following is an example. We've added custom methods in Repository in JPA Custom Methods chapter. Now let's add another method using @NamedQuery and test it.

What are the benefits of NamedQuery?

1. Advantages. The named queries help in grouping the HQL statements in a single place and lately refer to them using pre-configured names whenever we need to use them. This grouping helps largely in code cleanup because these HQL statements are no longer scattered in the whole code.


2 Answers

No, there is no widely accepted best practice that covers any complex cases. Also in general there is not too much style guides available for JPA. What seems to be commonly accepted, and in general used in books as well, is to start query with name of the entity.

I would go for EntityName (to guarantee unique names in persistence unit) combined with operation and arguments.

  • Person.findByAge
  • Person.findByAgeAndFirstName
  • Person.removeByFirstName
  • Person.updateSalaryIfYearBornBefore

Just as a note, specification uses with instead of by in examples, and does not prefix query with name of the entity. But that is of course specification, not style guide.

I find it good to declare constants for these query names and then use these constants in both @NamedQuery.name and em.createNamedQuery.

Because @NamedQuery, @NamedNativeQuery, and @NamedQueries can only be applied to mapped superclass or entity, you cannot locate them to utility class.

like image 91
Mikko Maunu Avatar answered Sep 17 '22 20:09

Mikko Maunu


Although there doesn't seem to be a globally accepted best practice, the book "Pro JPA 2" by Mike Keith and Merrick Shincariol recommends exactly what Mikko said, e.g. if you have a query for finding all Employees then call it "Employee.findAll".

Ito where to declare these, again there is no real best practice from what I can see. They seem to tend to favour declaring them on the Entity itself rather than all in one big class (such as a base MappedSuperclass from which all your entities extend) since that would very quickly become monolithic and could be a bit hard to maintain. Another option is to declare them in a separate XML file, not that I would recommend that. Personally I like the approach where they are declared on the Entity that they are related to. I also agree with Miko's suggestion to use constants for the name, you could just define all of these constants in a separate class.

like image 21
brent777 Avatar answered Sep 19 '22 20:09

brent777