Hibernate没有填充连接表

时间:2015-11-05 14:53:38

标签: java hibernate jpa

我有一个Hibernate的问题。我有经典的 @ManyToMany assosciation。当我将数据插入表格时,连接表为空

@ManyToMany
@JoinTable(name = "employee_interview", catalog = "db69824xmatko", schema = "",
        joinColumns = @JoinColumn(name = "employee_id", nullable = false),
        inverseJoinColumns = @JoinColumn(name = "interview_id", nullable = false))
public Set<InterviewEntity> getInterviews() {
    return interviews;
}

@ManyToMany(mappedBy = "interviews")
    public Set<EmployeeEntity> getEmployees() {
        return this.employees;
    }

我运行的代码在这里:

InterviewEntity interviewEntity = prepareInterview();
        EmployeeEntity employeeEntity = prepareEmployeeEntity();
        employeeDao.create(employeeEntity);
        employeeEntity.getInterviews().add(interviewEntity);

        interviewEntity.getEmployees().add(employeeEntity);
        interviewDao.create(interviewEntity);
        interviewDao.flush();
        employeeDao.flush();

我试图找到任何解决方案,但没有成功。我试着冲洗()。我没有同花顺的SQL:

Hibernate: 
insert 
into
    employee
    (created_date, created_user, first_name, last_name, login, password) 
values
    (?, ?, ?, ?, ?, ?)
Hibernate: 
    insert 
    into
        interview
        (candidate_id, comment, created_date, created_user, interview_date, job, protocol, round) 
    values
        (?, ?, ?, ?, ?, ?, ?, ?)

当我在下面使用flush SQL时正在运行:

Hibernate: 
    insert 
    into
        employee_interview
        (employee_id, interview_id) 
    values
        (?, ?)

当这个SQL脚本运行时,我不明白为什么我有空表employee_interview。我该如何解决这个问题?如何使用正确的数据填充连接表?谢谢你的每一个提示。 :)

当我创建新员工时,一切正常,但是当我使用旧对象时,我有例外,更新后的面试ID不存在。

- UPDATE-- 在...     InterviewEntity interviewEntity = prepareInterview();

        EmployeeEntity employeeEntity = employeeDao.findById(11);
        employeeEntity.getInterviews().add(interviewEntity);//<<------------

        CandidateEntity candidateEntity = prepareCandidateEntity();

        interviewEntity.setCandidate(candidateEntity);
        interviewEntity.getEmployees().add(employeeEntity);//<<------------
        candidateDao.create(candidateEntity);
        interviewDao.create(interviewEntity);
        employeeDao.updateObject(employeeEntity);

并且此代码抛出此异常

org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [com.tuke.entity.InterviewEntity#104]

1 个答案:

答案 0 :(得分:0)

尝试在InterviewEntity中设置关系。类似的东西:

interviewEntity.getEmployees().add(employeeEntity)