2013-10-22 195 views
2

我有像其他人一樣的問題(沒有解決的問題)! 當我使用fetch('userWorkItems')時,生成的sql查詢將同時具有LEFT OUTER JOIN和JOIN,通過TbUserWorkItem。 我只需要在TbUserWorkItem表中留下外連接! 我該如何解決這個問題?左外連接Ebean查詢

我的第一個型號:

@Entity 
@Table(name = "TbWorkItem", schema = "mySchema") 
public class TbWorkItem extends Model { 

    @Id 
    public Integer id; 

    public Integer code; 

    /* some other properties here */ 

    @JsonIgnore 
    @OneToMany//(mappedBy = "workItem") 
    public List<TbUserWorkItem> userWorkItems; 

    public static Finder<Byte, TbWorkItem> find = new Finder(Byte.class, TbWorkItem.class); 

    public static List<TbWorkItem> all(Integer systemId, Integer workingUserId) { 

/*  return find 
       .fetch("userWorkItems") 
       .where() 
       .eq("system.id", systemId).eq("workItemInformationType.code", 1) 
       .or(
         Expr.eq("publicWorkItemYesNo.code", 1), 
         Expr.and(Expr.eq("publicWorkItemYesNo.code", 2), Expr.eq("userWorkItems.workingUserId", workingUserId))) 
       .findList(); 
*/ 

     return find 
       .where() 
       .eq("system.id", systemId).eq("workItemInformationType.code", 1) 
       .or(
         Expr.eq("publicWorkItemYesNo.code", 1), 
         Expr.and(Expr.eq("publicWorkItemYesNo.code", 2), Expr.eq("userWorkItems.workingUserId", workingUserId))) 
       .findList(); 

    } 
} 

和第二型號:

@Entity 
@Table(name="TbUserWorkItem",schema="mySchema") 
public class TbUserWorkItem extends Model { 

    @Id 
    public Integer id; 

    /* some properties here */ 

    @ManyToOne 
    @JoinColumn(name="WorkItemId") 
    public TbWorkItem workItem; 


    public static Finder<Integer,TbUserWorkItem> find=new Finder(Integer.class,TbUserWorkItem.class); 

    /*some methods here*/ 

} 

回答

4

經過一番工作,谷歌上搜索我被迫使用查詢字符串期運用這樣的原始表達式:

 return Ebean.find(TbWorkItem.class) 
      .where().raw(
      "system.id=? and workItemInformationType.code =1 and (publicWorkItemYesNo.code=1 or userWorkItems.workingUserId = ?)",new Object[]{systemId,workingUserId}) 
      .findList(); 
} 

也許幫助別人! 但我歡迎任何使用ebean查詢來解決問題而不是原始字符串的答案。