Hibernate.Say Report と Projects テーブルに 1 対多の関係があります。1 つのレポートには多くのプロジェクトがあり、プロジェクトはアクティブまたは非アクティブの状態にすることができます。アクティブなプロジェクトを含むすべてのレポートを取得する必要があります。
Class Report{
@OneToMany(mappedby="reports")
List<Projects> projList;
/*get and set methods */
}
Class Project{
@ManyToOne
Report repors;
/*get and set methods */
/*This is a column which represenst ACTIVE or NOT*/
private status;
}
アクティブなプロジェクトですべてのレポートを取得しようとしていますが、基準は次のようになります。
List<Reports> reportsList = Criteria(Reports.class)
.createAlias("projList","projList",INNER_JOIN)
.add(Restrictions.eq("projList.status","ACTIVE")).list()
reportsList を反復処理して対応するプロジェクトを取得すると、アクティブか非アクティブかに関係なく、すべてのプロジェクトが返されます。
for(Report rep:reportsList){
rep.getProjList();
//This is returning me all the projects irresective of my criteria for
// ACTIVE projects. This is hitting another query to the table with the report id
//only but not with the status= ACTIVE.So this is returning me all the Projects.
}
しかし、INNER JOIN の代わりに LEFT OUTER JOIN を実行すると、正しい結果、つまり ACTIVE プロジェクトのみが返されます。なぜかわからないのですが?
どのように説明してもらえますか、それは正しいですか、すべてのシナリオで一貫していますか? または、アクティブなプロジェクトをロードして、それに対応するレポートを取得する必要があります。つまり、プロジェクトに関する基準 (子から親へ) があります。