Say I have a class called Bottle, a class called Pack and a class called Case. A Pack contains an instance of Bottle where a Case contains a collection of Bottle(s):
Code:
public abstract class Inventory {
// some methods for all inventories
}
public class Bottle extends Inventory {
}
public class Pack extends Inventory {
Bottle bottle;
// accessors for bottle
}
public class Case extends Inventory {
List bottles;
// accessors for bottles
}
I have a Bottle table, and Pack table, a Case table and a Case_Bottle table:
Code:
Bottle table:
BOTTLE_ID | BOTTLE_NAME
Pack table:
PACK_ID | BOTTLE_ID
Case table:
CASE_ID | ...
Case_Bottle table:
CASE_ID | BOTTLE_ID
The hibernate mapping for Pack is:
Code:
<subclass
name="Pack"
extends="Inventory"
discriminator-value="Pack">
<join table="Pack">
<key column="PACK_ID"/>
<many-to-one
name="bottle"
class="Bottle"
column="BOTTLE_ID"
cascade="save-update"
unique="true"/>
....
The hibernate mapping for Case is:
Code:
<subclass
name="Case"
extends="Inventory"
discriminator-value="Case">
<join table="Case">
<key column="CASE_ID"/>
<list name="bottleList" table="CASE_BOTTLE" lazy="false" cascade="save-update">
<key column="CASE_ID"/>
<index column="INDEX"/>
<many-to-many
column="BOTTLE_ID"
class="Bottle"/>
</list>
....
Now say I save Bottle, Pack and Case to the database. Below is a sample instance:
Code:
Bottle table:
BOTTLE_ID | BOTTLE_NAME
1 | Glass_A
Pack table:
PACK_ID | BOTTLE_ID
2 | 1
Case table:
CASE_ID | ...
3
Case_Bottle table:
CASE_ID | BOTTLE_ID
3 | 1
Now when I retrieve it, I would expect Pack's reference to a Bottle object == the one in the list of bottles contained in the Case object, i.e. Pack.getBottle() == (Bottle)Case.getBottleList().get(0).
However, I actually get two separate instances of bottle but with the same bottle name, e.g. System.out.print(Pack.getBottle()) = Bottle@12345 while System.out.print((Bottle)Case.getBottleList().get(0)) = Bottle@67890.
How do I make the bottles the same (one) object in both Pack and Case? Thanks.
-nefi