onetomany只返回特定行

nukf8bse  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(303)

我有一个属性为的类

@Entity
public class Energy() {
   @Id
   private long id;

   private Date date;

   @OneToMany(fetch = FetchType.LAZY)
   private List<Value> values;
}

价值观和阶级观

private String obis;
private long value;

如果obis包含特定值,那么返回列表值元素的最快方法是什么?
谢谢!

uujelgoq

uujelgoq1#

这取决于您选择的实现,例如使用标准api:

CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<Energy> cq = cb.createQuery(Energy.class);

Root<Energy> root = cq.from(Energy.class);
Join<Energy,Value> join = root.join(Energy_.values);
//If you dont use metamodel change by root.join("values")

cq.select(root);
cq.where(cb.equal(join.get(Value_.value),VALUE));
//If you dont use metamodel change by join.get("value")

List<Energy> result = entityManager.createQuery(cq).getResultList();

相关问题