Search code examples
javajpanamed-query

How to comment (javadoc) NamedQueries


Assume there are some named queries in an entity, how should those named queries be commented? Is there a way to map them into the created javadoc?

  @Entity
    @NamedQueries({
        @NamedQuery(name="Country.findAll",
                    query="SELECT c FROM Country c"),
        @NamedQuery(name="Country.findByName",
                    query="SELECT c FROM Country c WHERE c.name = :name"),
    }) 
    public class Country {
      ...
    }

At the moment I put comments (non javadoc) in the line before but I don't like it very much.

// find all countries
@NamedQuery(name="Country.findAll", query="SELECT c FROM Country c")

Solution

  • I use to define the Query's name as a constant inside of the entity class. That constant can of course be commented:

    @Entity
    @NamedQueries({
    @NamedQuery(name=Country.QUERY_FIND_BY_NAME,
                query="SELECT c FROM Country c WHERE c.name = :name"),
    }) 
    public class Country {
    
        /**
         * Description of the Query. Bla bla.
         */
        public static final String QUERY_FIND_BY_NAME = "Country.findByName";
    
       ...
    }
    

    As a bonus, you can use this constant instead of a String when creating a named query:

    em.createNamedQuery(Country.QUERY_FIND_BY_NAME, Country.class);