我使用qbe(通过示例查询)在我的数据库中查找一些对象。如何让我的查询也考虑到孩子的目标。
例如,我有这个对象:
public class Derogation
{
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "derogation_seq_gen")
@SequenceGenerator(name = "derogation_seq_gen", sequenceName = "calamar.derogation_id_seq",initialValue = 1, allocationSize = 1)
private int id;
@ManyToOne
private Application application;
@OneToMany(mappedBy="derogation", cascade = CascadeType.ALL, orphanRemoval=true, fetch = FetchType.EAGER)
private Set<DerogationAutre> listDerogationAutre;
[...]
}
我希望能够做到这一点:
Derogation derogation = new Derogation();
derogation.setApplication(new Application());
derogation.getApplication().setNom("applicationName");
Criteria criteria = session.createCriteria(Derogation.class);
criteria.add(Example.create(derogation));
ArrayList<Derogation> listDerogation = (ArrayList<Derogation>) criteria.list();
并且必须得到一个列表,该列表将考虑applicationName而不仅仅是我的对象Derogation的参数。
为了能够为我的清单做同样的事情,这可能吗?
我正在考虑合并两个标准查询,一个关于Derogation,另一个关于Application,但似乎不可能..
答案 0 :(得分:0)
我找到了正确的方法!
以下是代码:
Derogation derogation = new Derogation();
derogation.setApplication(new Application());
derogation.getApplication().setNom("applicationName");
Criteria criteria = session.createCriteria(Derogation.class);
criteria.add(Example.create(derogation));
//For the ManyToOne child
criteria.createCriteria("application").add(Example.create(derogation.getApplication()));
//For the OneToMany child
for(DerogationLinux derogationLinux : derogation.getListDerogationLinux())
criteria.createCriteria("listDerogationLinux").add(Example.create(derogationLinux));
希望有一天能帮助别人;)