I've searched but cannot seem to find the answer.
I have two tables:
select ts_id, tsjoin_id, workdate from TimeSheets
select e_id, lastname from Employees
I also have a join table:
TSJoin
tsjoin_id, employee_id
There is only one Employee to a TimeSheet. So with any given TimeSheet entity, I'd expect to be able to:
TimeSheet ts = tsService.getTimeSheet(123);
String lastName = ts.getEmployee().getLastName();
In SQL, to get the employee of a TimeSheet:
select e.lastname from TimeSheets t
join TSJoin x on (x.tsjoin_id = t.tsjoin_id)
join Employees e on (e.e_id = x.employee_id)
where t.ts_id = 123
In my Hibernate mapping, I have:
@OneToOne
@JoinTable(
name = "TSJoin",
joinColumns = {
@JoinColumn(name = "tsjoin_id", nullable = false)
},
inverseJoinColumns = {
@JoinColumn(name = "e_id", nullable = false)
}
)
However, the SQL it's generating is:
select * from TimeSheet t
left outer join TSJoin x on (t.ts_id = x.tsjoin_id)
Which returns null for the Employee.
It's taking the primary key of the TimeSheet and trying to match the primary key of the join table.
What am I doing wrong?
EDIT
I also want to state that I have only setup one direction at this point. Which is a TimeSheet -> Employee (OneToOne) and that Employee is not mapped to TimeSheet yet. Not sure if this makes a difference but I wanted to mention it.
EDIT 2 I also want to state that I believe the error might be because my join table does not contain a reference to the TimeSheet. And Hibernate is assuming that a join table is going to contain the primary keys of each entity involved (legacy database). I could probably create a mapping of TimeSheet -> JoinTable -> Employee and access it as: ts.getJoin().getEmployee() but that's pretty ugly.
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire