Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/367.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 休眠OneToMany/ManyToOne列表返回_Java_Spring_Hibernate_Orm - Fatal编程技术网

Java 休眠OneToMany/ManyToOne列表返回

Java 休眠OneToMany/ManyToOne列表返回,java,spring,hibernate,orm,Java,Spring,Hibernate,Orm,下面是我的表格,里面几乎没有记录 EMPLOYEE TABLE ______________________________________________________________________________________ | id | username | name | surname | mail |password|salary|deptId| type | |------|

下面是我的表格,里面几乎没有记录

                                    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 |
-----------------
下面是我的
员工
班级

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

我的
部门
课程如下:

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

EmployeeDao
实施:

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

}
问题是,当我只需要department表中的部门列表时,我使用
DepartmentDaoImpl
中的
listDepartments()

Department[15,Backend]  
Department[15,Backend]
Department[15,Backend]
Department[16,Frontend]
Department[17,UNKNOWN]

而不仅仅是这三个部门。因此,我假设返回属于特定部门的
员工的数量。我做错了什么

内部
EmployeeDao
我们必须实施

@Override
@SuppressWarnings("unchecked")
public List<Employee> listEmployees() {
    return this.session.createCriteria(Employee.class).
    setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY).list();
}
@覆盖
@抑制警告(“未选中”)
公共列表列出员工(){
返回this.session.createCriteria(Employee.class)。
setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY).list();
}

这真是奇怪的行为,您的代码不应该生成比表中实际列表大的列表。你能发布更多的代码吗,比如你在哪里调用
listDepartments()
?@Yserbius我添加了我调用它的地方。有一个ManagamentService类调用它。调用ManagamentService基本上是为了填充列表。like List deptList=managamentService.getDepartmentList();当我调试它时,在dao中,departmentList包含我在问题中给出的记录,我不知道为什么。我在想也许我在映射一个omany或多个one时做错了什么,因为我在hibernate中是个新手。请修复您的缩进。这很难理解,因为你是指部门道还是员工道?我很困惑,因为DepartmentDao中没有listEmployees()方法。如果是,我可以问一下原因吗?那是什么意思?对不起,是员工要改的。你的手术会成功的
@Override
@SuppressWarnings("unchecked")
public List<Employee> listEmployees() {
    return this.session.createCriteria(Employee.class).
    setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY).list();
}