2

我和其他人有同样的问题(未解决的问题)!当我使用 fetch('userWorkItems') 时,生成的 sql 查询将同时具有 LEFT OUTER JOIN 和 JOIN over 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*/

}
4

1 回答 1

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 查询而不是原始字符串来解决问题的答案。

于 2013-10-23T07:54:14.717 回答