hibernate默認的,以及網絡上的主流支持left join的表關系是one-to-many的,可以使用left join fetch(需要配置lazy="true" fetch="select"),也可以使用Criteria或者CriteriaQuery(link1 link2)來進行查詢。
對于many-to-one,首先我們先建兩個model:
@Entity
public class ClassOne {
public String id;
public boolean isDeleted; }
@Entity
public class ClassTwo {
public String id; @ManyToOne
public ClassOne classOne; // 父表
public boolean isDeleted; }
目前有兩個需求:
(1)select a.id,b.id from ClassTwo as b left join b.classOne as a;【正確,獲取到了所有ClassOne表的數據項】
(2)select a.id,count(b.id) from ClassTwo as b left join b.classOne as a where a.isDeleted=false and b.isDeleted=false group by a.id;【count結果中把0的濾去了,沒達到我的需求】
對于第二種,目前我還沒找到具體的解決方法,仍需研究。