如何在HQL中连接多个表

时间:2017-05-18 05:23:02

标签: java sql spring hibernate hql

我有这个实体类:

public class Work implements java.io.Serializable {

private Integer workId;
private User userByCreateBy;
private User userByUpdateBy;
private Long studyId;
private String name;
private byte panelType;
private byte status;
private Date createDate;
private Date updateDate;
private Set<Task> tasks = new HashSet<Task>(0);

public Work() {
}

}

private Integer taskId;
private User userByCompleteBy;
private User userByExecutedBy;
private Work work;
private byte type;
private Date startDate;
private Date endDate;
private byte status;
private Date updateDate;
private Forecast forecast;
private Set<ForecastOutput> forecastOutputs = new HashSet<ForecastOutput>(0);
private MonitorLottery monitorLottery;
private QprMonitorLotteryArea qprMonitorLotteryArea;
private Set<QprMonitorLotteryOutput> qprMonitorLotteryOutputs = new HashSet<QprMonitorLotteryOutput>(0);
private Set<ForecastCellafter740> forecastCellafter740s = new HashSet<ForecastCellafter740>(0);
private Set<QprMonitorLotteryResult> qprMonitorLotteryResults = new HashSet<QprMonitorLotteryResult>(0);
private Set<AnswerDataOutput> answerDataOutputs = new HashSet<AnswerDataOutput>(0);
private QprMonitorLottery qprMonitorLottery;
private AnswerDataCheck answerDataCheck;

public Task() {
}

}

public class User实现java.io.Serializable {

private Integer userId;
private String userName;
private String password;
private String name;
private String email;
private byte authority;
private Date lastLogin;
private Date passwordChangeDate;
private byte status;
private Integer createBy;
private Integer updateBy;
private Date createDate;
private Date updateDate;
private Set<MasterFile> masterFiles = new HashSet<MasterFile>(0);
private Set<Task> tasksForCompleteBy = new HashSet<Task>(0);
private Set<Work> worksForCreateBy = new HashSet<Work>(0);
private Set<Work> worksForUpdateBy = new HashSet<Work>(0);
private Set<Task> tasksForExecutedBy = new HashSet<Task>(0);

public User() {
}

}

我想使用hql加入这个表并获取username,workname,task.type,task.status等的详细信息。我已经完成了这段代码

 StringBuilder dataQuery = new StringBuilder("select new com.web.main.view.WorkView("
        + "w.workId, "
        + "w.studyId, "
        + "w.name, "
        + "w.panelType, "
        + "t.type, "
        + "t.status, "
        + "u.user_name,"
        + "w.createDate, "
        + "w.updateDate, "
        + "t.startDate, "           
        + "t.endDate ) "
        + "from Work w  left join w.tasks t  left join w.user u where u.user_Id = w.create_by and w.status!=:status");    

但是给出了错误

HTTP状态500 - 请求处理失败;嵌套异常是java.lang.IllegalArgumentException:org.hibernate.QueryException:无法解析属性:user of,

我做错了什么会是什么问题

2 个答案:

答案 0 :(得分:1)

user实体上没有名为Work的字段。因此,您应该将w.user替换为w.userByCreateByw.userByUpdateBy(或重命名实体字段)。

答案 1 :(得分:0)

在工作实体中,我没有看到用户字段 你有

private User userByCreateBy;
private User userByUpdateBy;

private User userByCompleteBy;
private User userByExecutedBy;

问题出在这一行

left join w.user u where u.user_Id = w.create_by

您需要让工作中的用户列表与任务

保持联接