Inverse OneToOne and JoinTable problem
chrisxl Jan 21, 2011 12:39 PMI have come across a problem in JBoss 4.2.2 (hibernate 3.2.4) where I cannot insert into or select from an entity (CompanyCar) that is the inverse-side of a @OneToOne join to a second entity (Employee), when the second entity has a @ManyToOne join that uses a @JoinTable.
This is also a problem in JBoss 5.1, but it appears to have been “fixed” in JBoss 6.0 (hibernate 3.6). Unfortunately we are not yet in a position to consider upgrading the version of JBoss we use.
I am looking for a workaround that we can use for JBoss 4.2.2. Also I’d like to know if this looks like a genuine bug, or whether I have got something wrong with my entity model that for some reason the hibernate spec has been changed to allow in the later release.
My entity model:
public class CompanyCar implements Serializable { private Long id; private Employee employee; @Id @Column(name = "id", nullable = false) public Long getID() { return id; } public void setID(final Long id) { this.id = id; } @OneToOne(mappedBy="car",optional = true, fetch = FetchType.LAZY) public Employee getEmployee() { return employee; } public void setEmployee(final Employee employee) { this.employee = employee; } } public class EmployeeTeam implements Serializable { private String teamName; private Collection<Employee> members; @Id @Column(name = "teamname", length = 30, nullable = false) public String getTeamName() { return teamName; } public void setTeamName(final String teamName) { this.teamName = teamName; } @OneToMany(mappedBy = "team") public Collection<Employee> getMembers() { return members; } public void setMembers(final Collection<Employee> members) { this.members = members; } } public class Employee implements Serializable { private String employeeID; private CompanyCar car; private EmployeeTeam team; @Id @Column(name = "employeeid", length = 36, nullable = false) public String getEmployeeID() { return employeeID; } public void setEmployeeID(final String employeeID) { this.employeeID = employeeID; } @OneToOne(optional = true, fetch = FetchType.LAZY) @JoinColumn(name = "companycarid", referencedColumnName = "id") public CompanyCar getCar() { return car; } public void setCar(final CompanyCar car) { this.car = car; } @ManyToOne(fetch = FetchType.LAZY) @JoinTable( name = "employeeteammembers", joinColumns = { @JoinColumn(name = "employee_fk", referencedColumnName ="employeeid") }, inverseJoinColumns = @JoinColumn(name = "team_fk", referencedColumnName = "teamname") ) public EmployeeTeam getTeam() { return team; } public void setTeam(final EmployeeTeam team) { this.team = team; } }
On inserting a CompanyCar:
final CompanyCar car = new CompanyCar(); car.setID(1L); entityManager.persist(car);
not-null property references a null or transient value: uk.co.exel.test.CompanyCar.employee
On loading a CompanyCar:
Invalid column name 'companycarid'
The following SQL is generated by hibernate. It is attempting to join the CompanyCar directly to EmployeeTeam?
select companycar0_.id as id0_, companycar0_1_.employee_fk as employee0_3_ from companycars companycar0_ left outer join employeeteams companycar0_1_ on companycar0_.id=companycar0_1_.companycarid
Many thanks for any comments / suggestions.
Chris.