4

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")
aldebaran-ms
  • 666
  • 6
  • 18

1 Answers1

9

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);
  • 1
    Nice solution although it seems more a workaround. I'll accept your answer because I don't think there's a better way to do that for now. Thank you :) – aldebaran-ms Jul 17 '15 at 16:05
  • I believe this is the only way. I've used this work-around also. It seems to me this is a glaring hole in Java/Javadoc that forces us to adopt such a work-around. :( – luis.espinal Jul 01 '20 at 17:30