• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

inverse attribute - too confusing !!!

 
Ranch Hand
Posts: 392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This has a very good explanation on inverse attribute of Hibernate.
 
Sandeep Vaid
Ranch Hand
Posts: 392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
No matter how many women are assigned to the project, a pregnancy takes nine months. Much longer than this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic