-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Mystery of bidrectional/unidirectional associations
PostPosted: Tue Sep 02, 2003 9:42 pm 
Newbie

Joined: Mon Sep 01, 2003 5:34 pm
Posts: 4
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


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 03, 2003 2:41 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
It is there to define who is reponsible for the link, child or parent. It does not define uni- or bi-directionality.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 03, 2003 10:45 am 
Quote:
It is there to define who is reponsible for the link, child or parent. It does not define uni- or bi-directionality.


Ah but it does. Refer to Section 5.9 of the reference manual where it says:

Quote:
You may specify a bidirectional many-to-many association simply by mapping two many-to-many associations
to the same database table and declaring one end as inverse. Heres an example of a bidirectional many-to-many
association from a class back to itself:
...
Changes made only to the inverse end of the association are not persisted.
You may map a bidirectional one-to-many association by mapping a one-to-many association to the same table
column(s) as a many-to-one association and declaring the many-valued end inverse="true".


Also it says that changes made to the inverse end are not persisted. Again this is not my experience

cheers


Top
  
 
 Post subject: bidirectional relation, persisting both directions allowed?
PostPosted: Tue Oct 14, 2003 2:28 pm 
Newbie

Joined: Tue Oct 14, 2003 2:07 pm
Posts: 10
Location: Berkeley, CA
Although the documentation links the inverse flag with bi-directionality, is it correct to declare both sides of a bi-directional many-to-many relation as inverse=false, and persist from both sides?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.