Hello I am posting this question here b/c it is not a specific problem that I'm having with Hibernate but rather a mystery I don't quite get. The term "bidirectional" is used quite a bit in the manual, and it says that a bidrectional association is one that can be navigated from both ends. And the way to define a bidirectional association is to declare one end as "inverse = true". So far so good. However, even if I do not declare the association as "inverse = true" I am still able to navigate the persistent association from both directions. Here is a sample of what I mean:
Two classes Department and Employee.
only the Department mapping file makes any mention of the association:
Code:
<set name="employees">
<!-- this is the name of he collection key, ie the reference back from the association table -->
<key column="deptID"/>
<one-to-many class="testdb2.Employee"/>
</set>
Now the code:
Code:
Department department;
department = new Department();
department.setCity("Austin");
department.setState("TX");
department.setName("IBM Global Services");
session.save(department);
///////////////////create Employee objects in the database
session.delete("from testdb2.Employee"); //clear out the DB
Employee employee = new Employee();
employee.setFirstName("Fu");
employee.setLastName("Barr");
employee.setEmail("fubarr@nowhere.com");
employee.setDepartment(department);
session.save(employee);
list = session.find("from testdb2.Employee");
System.out.println("employee query results = " + list);
Employee's toString() method is:
Code:
public String toString()
{return "(" + employeeId + ' ' + firstName + ' ' + lastName + ' ' + email + ' ' + department.getName() + ')';}
Running the above example produces:
Code:
employee query results = [(1 Fu Barr fubarr@nowhere.com IBM Global Services)]
As you can see, calling setDepartment on Employee caused the foreign key reference from the Employee to be filled in even though it was never explicitly declared as being the inverse link. So it appears that the link is navigable in both directions. Is this correct and if so, is the "inverse" declation merely an efficiency "hack"? Should bidrectional associations be named something different?
thanks