以下是我的表格,其中几乎没有记录。
EMPLOYEE TABLE
______________________________________________________________________________________
| id | username | name | surname | mail |password|salary|deptId| type |
|------|------------|--------|-----------|-------------|--------|------|------|------|
| 1 | jdoe | John | Doe | jdoe@doe.com| 123 | 31 | 15 |annon |
| 2 | wise | Wise | Sunshine | ws@ws@com | 345 | 62 | 15 |visib |
--------------------------------------------------------------------------------------
DEPARTMENT TABLE
_________________
| id | name |
|------|--------|
| 15 |Backend |
| 16 |Fontend |
| 17 |Unknown |
-----------------
我下面的Employee
类。
@Entity
@Table(name = "employee")
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@Column
private String username;
@Column
private String name;
@Column
private String surname;
@Column
private String mail;
@Column
private String password;
@Column
private String salary;
@Column
private String type;
@ManyToOne(cascade = { CascadeType.DETACH, CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH })
@JoinColumn(name = "deptId")
private Department deptId;
//setters getters
//constructors
}
我的Department
类在下面:
@Entity
@Table(name = "department")
public class Department {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@Column
private String name;
@OneToMany(fetch = FetchType.EAGER, cascade = { CascadeType.DETACH, CascadeType.MERGE, CascadeType.PERSIST,
CascadeType.REFRESH }, mappedBy = "deptId")
private List<Employee> employes;
public void add(final Employee employee) {
if (this.employes == null) {
this.employes = new ArrayList<>();
}
this.employes.add(employee);
employee.setDeptId(this);
}
//setter getters
//constructors
}
DepartmentDao
完成:
public class DepartmentDaoImpl implements DepartmentDao {
Session session;
@Override
public void addDepartment(final Department dept) {
this.session.persist(dept);
}
@SuppressWarnings("deprecation")
@Override
public List<Department> listDepartments() {
return this.session.createCriteria(Department.class).list();
}
@Override
public Department getDepartmentById(final int id) {
return this.session.get(Department.class, id);
}
@Override
public void deleteDepartment(final int id) {
this.session.delete(id);
}
public Session getSession() {
return this.session;
}
public void setSession(final Session session) {
this.session = session;
}
}
EmployeeDao
实现:
public class EmployeeDaoImpl implements EmployeeDao {
Session session;
@Override
public void addEmployee(final Employee employee) {
this.session.save(employee);
}
@Override
@SuppressWarnings("unchecked")
public List<Employee> listEmployees() {
return this.session.createCriteria(Employee.class).list();
}
@Override
public Employee getEmployeeById(final int id) {
return this.session.get(Employee.class, id);
}
@Override
public void removeEmployee(final int id) {
this.session.delete(this.session.get(Employee.class, id));
}
public void updateEmployee(final Employee employee) {
this.session.update(employee);
}
public Session getSession() {
return this.session;
}
public void setSession(final Session session) {
this.session = session;
}
}
我确实从listDepartments
拨打了ManagementServiceClass
,如下所示:
public List<Department> getDepartmentList() {
List<Department> departmentList;
try {
this.session = Service.FACTORY.openSession();
this.departmentDao.setSession(this.session);
this.session.beginTransaction();
departmentList = this.departmentDao.listDepartments();
this.session.getTransaction().commit();
} finally {
this.session.close();
}
return departmentList;
}
问题是,当我只想从部门表中获得部门列表时,我在listDepartments()
中使用了DepartmentDaoImpl
,但是返回
Department[15,Backend]
Department[15,Backend]
Department[15,Backend]
Department[16,Frontend]
Department[17,UNKNOWN]
而不仅仅是这三个部门。因此,我假设返回了属于特定部门的Employee
数量。我在这里做错了什么?
答案 0 :(得分:0)
在EmployeeDao
内部,我们必须实现
@Override
@SuppressWarnings("unchecked")
public List<Employee> listEmployees() {
return this.session.createCriteria(Employee.class).
setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY).list();
}