Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Indexed Collections

 
Ruchika Kapoor
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anybody there. Please send some response.
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ruchika Kapoor
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic