在我的数据库中,每个用户都有一个部门和一个用户级别。这些在users表中用department_id和userlevel_id表示。相应的User java类还有一个department和userlevel字段。
我需要使用给定的用户名和密码从我的数据库中选择所有用户,并自动将他们的部门和用户级别对象映射到他们的用户对象。
我有我的Users,Departments和Userlevels类(由hibernate pojo映射自动生成),如下所示:
public class Users implements java.io.Serializable {
private Integer id;
private Departments departments;
private Userlevels userlevels;
private String username;
private String password;
private String salt;
private String email;
private String firstName;
private String lastName;
public Users() {
}
//Getters and setters
}
public class Departments implements java.io.Serializable {
private Integer id;
private String name;
public Departments() {
}
//Getters and Setters
}
}
public class Userlevels implements java.io.Serializable {
private Integer id;
private String name;
public Userlevels() {
}
//Getters and Setters
}
}
我有一个SQL查询:
SQLQuery q = session.createSQLQuery("SELECT u.*,d.*,ul.* FROM users u, departments d, userlevels ul WHERE u.department_id=d.id AND u.userlevel_id=ul.id AND u.username=? AND u.password=?");
q.setString(0, "usernameToGet");
q.setString(1, "passwordToGet");
q.addEntity("u", Users.class);
q.addJoin("dept", "u.departments");
q.addJoin("ulevels", u.userlevels");
List<Users> users = q.list();
现在我正在使用相应的部门对象获取正确的User对象,但userlevels对象返回null .... 任何帮助将不胜感激,谢谢!!