Hibernate以一对多的关系检索错误的对象列表

时间:2013-07-23 07:38:51

标签: java hibernate one-to-many

我是hibernate的新手。我要表部门和老师。一个部门可以有很多教师,但一个教师只能隶属于一个部门。我有以下映射:

@Entity
@Table(name = "department")
public class Department {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Integer id;

    @Column(name = "name")
    private String name;

    @Column(name = "description")
    private String description;

    @OneToMany (cascade=CascadeType.ALL, fetch = FetchType.EAGER)
    @JoinColumn(name = "department_id")
    private List<Teacher> teachers = new ArrayList<Teacher>();
}

@Entity
@Table(name = "teacher")
public class Teacher {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Integer id;

    @Column(name = "fName")
    private String fName;

    @Column(name = "lName")
    private String sName;

    @Column(name = "degree")
    private String degree;

    @ManyToOne
    private Department department;
}

还有getter和setter,为简单起见,我省略了它们。 我正在使用MySql数据库。当我试图用新老师插入新部门时,一切正常。但是,我也希望能够获得一个完整的教师部门列表,分配给每个部门。目前在我的数据库中我有5个部门和13个 老师。但是当我执行以下代码时:

    DepartmentDaoImpl ddi = new DepartmentDaoImpl();
    List<Department> departments = ddi.getAllDepartment();

    for (Department k : departments){
        printDepartmentData(k);
        List<Teacher> teachers = k.getTeachers();
        for (Teacher t : teachers){
            printTeacherName(t);
        }
    }

我的getAllDepartments实现:

@Override
public ArrayList getAllDepartment() throws SQLException {
    Session session = null;
    ArrayList<Department> result = null;
    try{
        session = HibernateUtil.getSessionFactory().openSession();
        result = (ArrayList)session.createCriteria(Department.class).list();
    }catch(Exception ex){
        ex.printStackTrace();
    }finally{
        if(session != null && session.isOpen()){
            session.close();
        }
    }
    return result;
}

创建表的SQL:

CREATE TABLE teacher (
id INTEGER PRIMARY KEY auto_increment,
fName varchar(30),
lName VARCHAR(30),
degree VARCHAR(100),
department_id INTEGER
);
CREATE TABLE department(
id INTEGER PRIMARY KEY auto_increment,
name VARCHAR(100),
description VARCHAR(5000) 
);

我得到了13个部门的实例。我得到了第一个部门的5个实例(我有5个教师分配给它)每个人都分配了相同的五个教师,第二个部门的两个实例(我有两个教师分配给它)每个部门都有两个相同的教师分配,所以上。 有人可以弄清楚,如何解决它?我需要获得5个部门实例,用 相应的教师人数。

4 个答案:

答案 0 :(得分:3)

由于您使用fetch = FetchType.EAGER配置了一对多关系,因此hibernate会对两个表执行连接。您看到的结果是由此连接引起的。

确保equals()hashCode()中的DepartmentTeacher方法都正确无误。如果这不能解决问题,请尝试使用Set代替List

答案 1 :(得分:0)

进行以下更改并进行测试

   @OneToMany (cascade=CascadeType.ALL, fetch = FetchType.EAGER, mappedBy="department")
    private List<Teacher> teachers = new ArrayList<Teacher>();

答案 2 :(得分:0)

尝试改变这个:

@OneToMany (mappedBy="department", cascade=CascadeType.ALL, fetch = FetchType.EAGER)
private Set<Teacher> teachers = new HashSet<Teacher>();

并改变:

@ManyToOne
private Department department;

到:

@Collumn(name = "department")
private Integer department;

答案 3 :(得分:0)

只需在你的dao中尝试这个,它会有帮助

result =(ArrayList)session.createCriteria(Department.class)。 setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY).list();