员工实体类
@Entity
@Table(name="tb_user")
public class User {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="id")
private int id;
@Column(name="userName")
private String userName;
@Column(name="password")
private String password;
@ManyToOne(fetch=FetchType.EAGER,cascade={CascadeType.PERSIST})
@JoinColumn(name="department_id")
//Department类型
private Department department_id;
@Column(name="sex")
private String sex;
@Column(name="birthday")
private Date birthday;
@Column(name="phoneNo")
private int phoneNo;
@Column(name="email")
private String email;
@Column(name="address")
private String address;
@Column(name="userDesc")
private String userDesc;
@Column(name="departmentName")
private String departmentName;
public String getDepartmentName() {
return departmentName;
}
public void setDepartmentName(String departmentName) {
this.departmentName = departmentName;
}
//Department类型
public Department getDepartment_id() {
return department_id;
}
public void setDepartment_id(Department departmentId) {
department_id = departmentId;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public int getPhoneNo() {
return phoneNo;
}
public void setPhoneNo(int phoneNo) {
this.phoneNo = phoneNo;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getUserDesc() {
return userDesc;
}
public void setUserDesc(String userDesc) {
this.userDesc = userDesc;
}
}
部门实体类
@Entity
@Table(name="tb_department")
public class Department {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="id")
private int id;
@Column(name="departmentName")
private String departmentName;
@Column(name="departmentDesc")
private String departmentDesc;
public String getDepartmentDesc() {
return departmentDesc;
}
public void setDepartmentDesc(String departmentDesc) {
this.departmentDesc = departmentDesc;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getDepartmentName() {
return departmentName;
}
public void setDepartmentName(String departmentName) {
this.departmentName = departmentName;
}
}
我dao层HQL或者Mysql查询语句要怎么写,返回的list对象还是list对象数组,还是其他的? 然后页面输出怎么写?能得到这种效果:(我查询的所属部门查不到,为null)
外键 entity 对象 hql mysql
------解决方案--------------------
select A.*,B.departmentName from tb_user A,tb_department B where B.id=A.department_id
要用hibernate的话 可以在数据创建视图,再以视图转换成POJO类 。。。。
------解决方案--------------------
你有关联了直接用hql取不是整个实体都能取出来么,
from User where .....取出后,在页面上直接user.department.DepartmentName不就可以获取到了么