2013年10月29日星期二

Id department table and employee table foreign key department_id how employee information in the query at the same time check out the staff department ?

员工实体类

@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;
}


}


I dao layer HQL or Mysql query how to write , the returned list object or list an array of objects , or other ? then page output how to write ? This effect can be obtained : ( I can not find out the query department , be null)

------ Solution ------- -------------------------------------
select A. *, B.departmentName from tb_user A, tb_department B where B.id = A.department_id
use hibernate , then you can create a view of the data , and then converted into a POJO class to view . . . .
------ Solution ---------------------------------------- ----
you have associated with the direct use of the entire entity is not able to take hql taken out Mody,
from User where .....
removed, not directly on the page user.department.DepartmentName can get to it
------ Solution ------------------- -------------------------

it directly sql, left join relational query
---- - For reference only ---------------------------------------
view did not learn too . . .
------ For reference only -------------------------------------- -
very grateful ! ! Solved in user.getDepartment_id added after getDepartmentName have to. .

------ For reference only ---------------------------------- -----

very grateful ! ! After you remind solve the output page user.getDepartment_id added after getDepartmentName have to. .

没有评论:

发表评论