akib
akib

Reputation: 115

What is the use of JPA MetaModel?

enter image description here

in this example whatever the use of this criteria.where(builder.equal(root.get(Person_.name), "John Doe"))

the same can be achieved with this criteria.where(builder.equal(root.get(Person.name), "John Doe"))

Why JPA MetaModel is needed at all then?

Upvotes: 1

Views: 360

Answers (1)

Chris
Chris

Reputation: 21145

Short answer, it isn't needed.

What it provides, in addition to being automatically generated based on your entity mappings by your provider, is typing.

Calling root.get(Person_.name) returns Path, with Y set to the name property's type. When you call root.get("propertyName"), the path expression you get back does not have any type associated to it. This means you may have to figure it out and set it yourself for some queries or run into issues where type doesn't match, requiring hurdles where you are forced to call (pulled from the root javadoc)

Path<Set<String>> nicknames = root.get("nicknames");
criteria.where(builder.isMember("John", nicknames));

or

criteria.where(builder.isMember("John", root.<Set<String>>get("nicknames")));

With just:

criteria.where(builder.isMember("John", root.get(Person_.nicknames)));

it is clear to everyone what is happening and on compile you know the property exists and that its type matches.

Upvotes: 2

Related Questions