This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Object Relational Mapping and the fly likes Indexed Collections Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Indexed Collections" Watch "Indexed Collections" New topic
Author

Indexed Collections

Ruchika Kapoor
Greenhorn

Joined: Oct 03, 2004
Posts: 22
I'm using map to retrieve object graph from database. The problem is while retrieving I'm getting the save value even though I'm getting different keys on each iteration.

My Schema is

TB_EVENTS
---------------------------------------------------------
IDNUMBER
NAMEVARCHAR
START_DATEDATE
DURATIONNUMBER


TB_SPEAKERS_LIST
---------------------------------------------------------
EVENT_IDNUMBER
NAMEVARCHAR
EVENT_INDEXVARCHAR


Event.hbm.xml
---------------------------------------------------------
<class name="Event" table="TB_EVENTS">
<id name="eventid" column="ID" unsaved-value="0">
<generator class="increment"/>
</id>
<property name="name">
<column name="NAME" />
</property>
<property name="startDate">
<column name="START_DATE" />
</property>
<property name="duration">
<column name="DURATION" />
</property>

<map name="speakers" cascade="all" fetch="join">
<key column="EVENT_ID"/>
<map-key column="EVENT_INDEX" type="string"/>
<one-to-many class="Speakers"/>
</map>
</class>
</hibernate-mapping>


Speakers
---------------------------------------------------------
<class name="Speakers" table="TB_SPEAKERS_List">
<id name="eventid" column="EVENT_ID" unsaved-value="0">
<generator class="increment"/>
</id>
<property name="name">
<column name="NAME" />
</property>
</class>
</hibernate-mapping>


Class File
---------------------------------------------------------

Event event=(Event)session.load(Event.class, new Integer("1"));
System.out.println("Event Name: "+event.getName());

Map map=event.getSpeakers();
Iterator itr=map.entrySet().iterator();
System.out.println("*************************");
Speakers speaker=null;
while(itr.hasNext()){
Map.Entry mapEntry=(Map.Entry)itr.next();
System.out.println("Event Index: "+mapEntry.getKey());
speaker=(Speakers)mapEntry.getValue();
System.out.println("Name: "+speaker.getName());
System.out.println("*************************");
}
txn.commit();



=========================

The query generated is

select event0_.ID as ID1_, event0_.NAME as NAME0_1_, event0_.START_DATE as START3_0_1_, event0_.DURATION as DURATION0_1_, speakers1_.EVENT_ID as EVENT1_3_, speakers1_.EVENT_INDEX as EVENT3_3_, speakers1_.EVENT_ID as EVENT1_0_, speakers1_.NAME as NAME1_0_ from TB_EVENTS event0_, TB_SPEAKERS_List speakers1_ where event0_.ID=speakers1_.EVENT_ID(+) and event0_.ID=?

Which is giving correct results when I executed it manually. Please help me figure out this problem.

Thanks,
Ruchika
Ruchika Kapoor
Greenhorn

Joined: Oct 03, 2004
Posts: 22
Anybody there. Please send some response.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

Can you explain your problem in a little more detail? At this point, I am not understanding what the problem is.

Also, there is a CODE button below the Add Reply button that will add CODE tags where you can paste your mappings and code, and it will retain all the indentation, so it is readable. Right now it has no indentation and is very difficult to read.

Thanks

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Ruchika Kapoor
Greenhorn

Joined: Oct 03, 2004
Posts: 22
Mark,

I've written a piece of code which is using Map. Well, when I try to save objects, this code works fine but now when I try to retrieve records, it returns me Event object. When I retrieve Speaker and cast it to Map, it returns keys but it returns the same value twice as value even for different keys. The output I'm getting:
--------------------------------------------------------------



The code is:



Thanks,
Ruchika
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

Off the top of my head I am not seeing anything.

Anyone else see something I am missing (probably a real simple thing, those are the ones that I always overlook). If not, we can try some things

1. Print out the Map just after save() but before your call to load()
2. commit and close your transaction and session right after save, then open a new Session/transactiona and do a load(), to see what happens


You are sure the save is creating three records all with different speaker names in the speaker table?

Mark
Ruchika Kapoor
Greenhorn

Joined: Oct 03, 2004
Posts: 22
Mark,

I tried printing out the value, and you know it displays the values correctly. But then I created a all together a new session and loaded the object, but this time I'm getting the same results.

Am not really able to figure out the real problem. Values are being saved correctly, stored correctly and even keys are retrieved correctly. Then where am I going wrong ?

Thanks,
Ruchika
 
 
subject: Indexed Collections