在Ebean查询中左外连接

时间:2013-10-22 11:32:59

标签: playframework-2.1 ebean

我和其他人一样有问题(没有解决问题)! 当我使用fetch('userWorkItems')时,生成的sql查询将同时具有LEFT OUTER JOIN和JOIN而不是TbUserWorkItem。 我需要只在TbUserWorkItem表上留下外连接! 我怎么解决这个问题?

我的第一个模特:

@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*/

}

1 个答案:

答案 0 :(得分: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查询解决问题而不是原始字符串的任何答案。