Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Hibernate 如何在JPA条件中执行联接获取而不进行未检查的强制转换?_Hibernate_Jpa_Jpa 2.0_Criteria_Criteria Api - Fatal编程技术网

Hibernate 如何在JPA条件中执行联接获取而不进行未检查的强制转换?

Hibernate 如何在JPA条件中执行联接获取而不进行未检查的强制转换?,hibernate,jpa,jpa-2.0,criteria,criteria-api,Hibernate,Jpa,Jpa 2.0,Criteria,Criteria Api,我需要使用静态元模型在JPA标准中执行连接获取,但是我不知道如何在不获得未检查异常警告的情况下执行该操作 假设我们有一个Thing实体,其中有一个延迟初始化的其他实体。我想用获取的其他对象检索内容,其中other.someField=“someValue”。我大致上是这样做的: public List getThings(){ CriteriaBuilder cb=em.getCriteriaBuilder(); CriteriaQuery cq=cb.createQuery(Thing.cla

我需要使用静态元模型在JPA标准中执行连接获取,但是我不知道如何在不获得未检查异常警告的情况下执行该操作

假设我们有一个Thing实体,其中有一个延迟初始化的其他实体。我想用获取的其他对象检索内容,其中other.someField=“someValue”。我大致上是这样做的:

public List getThings(){
CriteriaBuilder cb=em.getCriteriaBuilder();
CriteriaQuery cq=cb.createQuery(Thing.class);
Root=cq.from(Thing.class);
//这将发出未经检查的强制转换警告:
Join other=(Join)root.fetch(Thing.other);
cq.select(root).where(cb.equal(other.get(other_.someField),“someValue”);
返回em.createQuery(cq.getResultList();
}
但是,由于连接和获取接口没有任何共同点,因此我得到了一个“uncheckedcast”警告。我想去掉这个警告(不使用@SuppressWarnings)

我想我可以这样做:

cb.equal(root.get(This_u.THING).get(Other_u.SOMEFIELD),“someValue”)) 但我怀疑它是否更好,因为我在这里操作字符串(所以没有类型安全性)


实现上述目标的更干净的方法是什么?

Fetch方法不应用于创建JPA联接。在JPA中,连接用于创建where条件,而不是加载数据。这与本机SQL不同。供加入使用:

Join<Thing, Other> other = root.join(Thing_.other);
root.fetch(Thing_.other);