Siddharth
Siddharth

Reputation: 2254

What is purpose of metamodel class in JPA2.0?

I was recently reading about the Criteria API in JPA2.0 specification, where they introduced the concept of having metamodel class (see here). I understand how to use it, but what I do not understand is why was there a need to create this new concept just for Criteria API.

Upvotes: 0

Views: 429

Answers (2)

marekmuratow
marekmuratow

Reputation: 404

Metamodel helps to write a typesafe code

CriteriaQuery<Double> c = cb.createQuery(Double.class);
Root<Account> a = c.from(Account.class);
c.select(cb.avg(a.get(Account_.balance)));

Here the compiler can check for errors by checking a type of balance property (for example there should be some error if a balance is a String)

Upvotes: 0

Prasad Kharkar
Prasad Kharkar

Reputation: 13556

Without metamodel, we access name property of Employee entity as below,

Root<Employee> employee = query.from(Employee.class);
employee.get("name");

using metamodel, you can shorten it to

Employee_.name

where Employee_ is the metamodel of Employee entity I think having no need to create roots for entities is the advantage of using metamodel api.

Upvotes: 2

Related Questions