Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
166 views
in Technique[技术] by (71.8m points)

java - 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_

question from:https://stackoverflow.com/questions/9379536/what-does-an-underscore-concatenated-to-a-class-name-mean

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...