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: Can hibernate create 2 instances of same record?
PostPosted: Mon Jul 17, 2006 4:37 pm 
Newbie

Joined: Tue Jun 13, 2006 9:03 am
Posts: 18
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


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 17, 2006 8:05 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
You will have to read about object/relation identity. There is information on the wiki (and other places around the net). As you have discovered its not a simple as it might first appear to be.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 18, 2006 5:14 pm 
Newbie

Joined: Tue Jun 13, 2006 9:03 am
Posts: 18
Thanks for the reply.

I was going to compare bottle object ids. If the references are different but the ids matched, I would make the references equal. However, one of my bottle references has it's id set appropriately but the other returns null. Now is it possible for hibernate to create an object instance without setting the object id?

-nefi


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 19, 2006 8:29 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
If the object has been stored then the Id (Primary key) value(s) will be the same.

Quote:
Now is it possible for hibernate to create an object instance without setting the object id?


Its not possible. You maybe using an API method that copies the domain object (such as merge) or you have a slight code issue or using some level of application caching problem. No idea.


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.