问题描述
在,他们提供了关于Criteria / Metamodel API使用的儿子示例,但据我了解Java,似乎无法工作:
In Sun Online resources, they provide son example about the usage of the Criteria/Metamodel API, but as far as I understand Java, it seems to be impossible to work:
CriteriaQuery<Pet> cq = cb.createQuery(Pet.class);
Metamodel m = em.getMetamodel();
EntityType<Pet> Pet_ = m.entity(Pet.class);
EntityType<Owner> Owner_ = m.entity(Owner.class);
Root<Pet> pet = cq.from(Pet.class);
Join<Owner, Address> address = cq.join(**Pet_.owners**).join(**Owner_.addresses**);
宠物_
是实例类 不定义名为所有者
或地址的任何属性
。
他们定义了名为 Pet _
和所有者_ 的类code>用于元模型,但是在这里导入它们会与变量名称产生冲突......我是对的吗?
They do define classes named Pet_
and Owner_
for the metamodel, but they importation here would create a conflict with the variable names ... Am I right?
__
(问题也与此相关)
推荐答案
这个例子不正确,作者正在混合规范静态元模型类(生成)与通过Metamodel API获得的类。它们应该使用弱类型API或stronlgy类型生成的类,而不是两者。在他们的情况下, Pet _
(这是一个令人难以置信的错误命名选择并且具有误导性)确实没有任何所有者
属性。
这应该报告,本教程的这一部分是误导和错误。
This example is incorrect, the authors are mixing canonical static metamodel classes (generated) with classes obtained via the Metamodel API. They are supposed to use either the weakly typed API or the stronlgy typed generated classes, not both together. In their case, the Pet_
(which is an incredible bad naming choice and is misleading) indeed doesn't have any owners
attribute.This should be reported, this part of the tutorial is misleading and wrong.
- Dynamic, typesafe queries in JPA 2.0
这篇关于JPA / Metamodel:Sun Docs中的奇怪(不一致?)示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!