| Author |
inverse attribute - too confusing !!!
|
Sandeep Vaid
Ranch Hand
Joined: Feb 27, 2006
Posts: 390
|
|
If we have a bidirectional association - we need to make one end of the association as inverse. Consider example of Item - Bid bidirectional association where From Item to Bid it's one-to-many association From Bid to Item it's many-to-one association In Hibernate_Reference : It's mentioned as : All bi-directional associations need one side as inverse. In a one-to-many association it has to be the many-side. According to this, the inverse attribute should be @ Bid side. This means we need to specify inverse attribute in Bid.hbm.xml file. BUT (NOTE THE CONTRADICTION) In Hibernate In Action Book, it's written in Item.hbm.xml as : The following are few more related questions : 1. When we say many side of association (Bid) do we mean/refer to Bid.hbm.xml ? 2. Does this mean that, if i specify the inverse attribute in Item.hbm.xml (i.e Item Side [One Side of association]), it means ignore the changes made @ item say. Hence if we only call item.getBids().add(bid), no changes will be made persistent. Instead we are telling Hibernate that it should propagate changes made at the Bid end of the association to the database. i.e when we call bid1.setItem(item) ,it should become persistent ?
|
 |
Jaikiran Pai
Marshal
Joined: Jul 20, 2005
Posts: 8145
|
|
|
This has a very good explanation on inverse attribute of Hibernate.
|
[My Blog] [JavaRanch Journal]
|
 |
Sandeep Vaid
Ranch Hand
Joined: Feb 27, 2006
Posts: 390
|
|
Jaikiran, the link provided by you is really a nice one. but sill i have few doubts : In case of Item -> Bid Bidirectional Association Item.hbm.xml Item.java Can you please justify the output in the following cases : Case 1: inverse="true" bid.setItem(this); RESULT : No Error. No INERST statement Case 2: inverse="false" bid.setItem(this); RESULT : No Error. No INERST statement Case 3: inverse="true" this.bids.add(bid); RESULT : Exception in thread "main" org.hibernate.PropertyValueException: not-null property references a null or transient value: Bid.item Case 4: inverse="false" this.bids.add(bid); RESULT : Exception in thread "main" org.hibernate.PropertyValueException: not-null property references a null or transient value: Bid.item  [ August 27, 2007: Message edited by: Sandeep Vaid ]
|
 |
Jaikiran Pai
Marshal
Joined: Jul 20, 2005
Posts: 8145
|
|
Sandeep, could you please post the entire exception stacktrace that you are seeing. Also, please post the hbm mapping for your Bid which contains the many-to-one declaration. The other information that will help is the code where you are saving/updating the Item or Bid object. P.S: I am travelling tonight and wont be able to access JavaRanch. Once you post these details, either someone else will be able to explain you more or you will probably see my reply in a couple of days. So please be patient
|
 |
 |
|
|
subject: inverse attribute - too confusing !!!
|
|
|