JPA-2.0 简单选择位置问题
我遇到了有关 JPA-2.0 关系查询的问题。如何选择具有至少一个Event
且type = B
的任何Dataset
?
@Entity
class Dataset {
@OneToMany(fetch = FetchType.LAZY, mappedBy = "dataset")
public List<Event> events;
}
@Entity
class Event {
@ManyToOne
@JoinColumn
public Dataset dataset;
public Type type;
}
enum Type {
A, B, C
}
我的出发点是
CriteriaBuilder _builder = em.getCriteriaBuilder();
CriteriaQuery<Dataset> _criteria = _builder.createQuery(Dataset.class);
// select from
Root<Dataset> _root = _criteria.from(Dataset.class);
_criteria.select(_root);
// apply some filter as where-clause (visitor)
getFilter().apply(
_root, _criteria, _builder, em.getMetamodel()
);
// how to add a clause as defined before?
...
对此有任何想法。我尝试创建子查询和联接,但不知何故我做错了,并且总是得到所有数据集作为结果。
I am stuck with a problem concerning JPA-2.0 queries with relationships. How would it be possible to select any Dataset
with at least one Event
with type = B
?
@Entity
class Dataset {
@OneToMany(fetch = FetchType.LAZY, mappedBy = "dataset")
public List<Event> events;
}
@Entity
class Event {
@ManyToOne
@JoinColumn
public Dataset dataset;
public Type type;
}
enum Type {
A, B, C
}
My starting point is
CriteriaBuilder _builder = em.getCriteriaBuilder();
CriteriaQuery<Dataset> _criteria = _builder.createQuery(Dataset.class);
// select from
Root<Dataset> _root = _criteria.from(Dataset.class);
_criteria.select(_root);
// apply some filter as where-clause (visitor)
getFilter().apply(
_root, _criteria, _builder, em.getMetamodel()
);
// how to add a clause as defined before?
...
Any ideas on this. I tried to create a subqueries as well as a join, but I somehow did it wrong and always got all datasets as result.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试
编辑:正如 Pascal 指出的那样,您似乎正在使用 Criteria API。对这个不太熟悉,不过我会尝试一下。
唷,那真是辛苦了。我现在要回到 Linq。
Try
EDIT: As Pascal pointed out it looks like you are using the Criteria API. Not as familiar with this, but I'll have a stab.
Phew that was hard work. I'm going back to Linq now.