Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does an underscore concatenated to a class name mean?

I was reading the "Dynamic, typesafe queries in JPA 2.0" article and stumbled upon this example:

EntityManager em = ... CriteriaBuilder qb = em.getCriteriaBuilder(); CriteriaQuery<Person> c = qb.createQuery(Person.class); Root<Person> p = c.from(Person.class); Predicate condition = qb.gt(p.get(Person_.age), 20); //                                     ^^ --- this one c.where(condition); TypedQuery<Person> q = em.createQuery(c);  List<Person> result = q.getResultList(); 

I was wondering, what exactly does the underscore here mean?

Since an underscore it is a valid part of a classname I don't understand why this can be used in JPA. I checked this with an existing entity in my code and of course my class couldn't be resolved as ClassName_

like image 751
stacker Avatar asked Feb 21 '12 14:02

stacker


People also ask

Can class name have underscore in Java?

Except for variables, all instance, class, and class constants are in mixed case with a lowercase first letter. Internal words start with capital letters. Variable names should not start with underscore _ or dollar sign $ characters, even though both are allowed.

Can class name start with _?

Technically it's possible to use the underscore in a class name, but there's no practical reason to do so. If the underscore were used in a class name, it would act as a separator between words.

Can a class start with an underscore?

Generally it a convention to use "_" in variables name to indicate that they're instance variable or for some other special purpose [but defiantly it depends on programmer's taste]. But there are few classes listed in Java API [J2SE], whose names are starting with underscore ("_").


2 Answers

That is the metamodel for the persistance. It is how you can do type safe JPA queries in Java. It allows queries to staticly check your queries because classBar_ describes your JPA Bar. In HQL, you can easily mistype a query and not know it until it is run.

So technically, the _ does not mean anything, but it is the convention used by JPA to name a metamodel class of a JPA persistent model class. Model_ is the metamodel of Model, and it provides the names of the queryable fields and their types.

like image 68
Sled Avatar answered Sep 22 '22 14:09

Sled


I found this way to declare the metamodel in this article.

/**  * A  meta model class used to create type safe queries from person  * information.  * @author Petri Kainulainen  */ @StaticMetamodel(Person.class) public class Person_ {     public static volatile SingularAttribute<Person, String> lastName; } 
like image 38
stacker Avatar answered Sep 21 '22 14:09

stacker