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.  [ 2 posts ] 
Author Message
 Post subject: Problem in Listing Records
PostPosted: Mon Oct 17, 2005 6:14 am 
Newbie

Joined: Mon Oct 17, 2005 4:48 am
Posts: 2
Hi

I have the following problem in Listing the records.
When I try to list the records the first record is always null.

Problem:
==========

I have two tables;


1) User

2) Card with a one-to-many relation
(a User can have many addresses).


I declared a list in User mapping.

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >

<hibernate-mapping package="com.smarteck.smartsecurity.users">
<class name="SmartUser" table="SmartUser">
<id
column="userId"
name="userId"
type="integer"
>
<generator class="sequence">
<param name="sequence">UserIdSeq</param>
</generator>
</id>
<list inverse="true" name="cardListByOwnerUser">
<key column="ownerUser" />
<index column="cardNo" />
<one-to-many class="com.smarteck.smartcrm.cards.Card" />
</list>
</class>

</hibernate-mapping>




following is the mapping for card Table:
=========================================


<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >

<hibernate-mapping package="com.smarteck.smartcrm.cards">
<class name="Card" table="Card">
<composite-id class="com.smarteck.smartcrm.cards.CardPK" name="cardPK">
<key-property
column="cardNo"
name="cardNo"
type="integer"
/>
<key-many-to-one
class="com.smarteck.smartsecurity.users.SmartUser"
column="ownerUser"
name="ownerUser"
/>
</composite-id>
<property
column="nameOnCard"
length="255"
name="nameOnCard"
not-null="false"
type="string"
/>
</class>
</hibernate-mapping>


The Code in Struts Action:
===========================


Session s = getSession();
Criteria c = s.createCriteria(SmartUser.class);
List userList = crit.list();


// Session committed and closed here
// List object put in HttpServletRequest





Following code is in the jsp
=================================

// Get the userList from request

for(int i=0; i<userList.size(); i++) {
SmartUser user = (SmartUser) userList.get(i);
List cardList = user.getCardListByOwnerUser(); // In this list, always first element is null
for (j=0; j<cardList.size(); j++) {
Card card = (Card) cardList.get(j);
System.out.println("Card="+card);
}
}


For example, if I have 5 Card records, when I call
user.getCardListByOwnerUser(), I am getting a List of size=5.


when I call user.getCardListByOwnerUser(), the first element in the list
is always null.


But the first element in the list is null and the remaining 4 elements are valid records.


Any suggestions??







Hibernate version:2.1.7

Mapping documents:

//The following is mapping for user:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >

<hibernate-mapping package="com.smarteck.smartsecurity.users">
<class name="SmartUser" table="SmartUser">
<id
column="userId"
name="userId"
type="integer"
>
<generator class="sequence">
<param name="sequence">UserIdSeq</param>
</generator>
</id>
<list inverse="true" name="cardListByOwnerUser">
<key column="ownerUser" />
<index column="cardNo" />
<one-to-many class="com.smarteck.smartcrm.cards.Card" />
</list>
</class>

</hibernate-mapping>



following is the mapping for card Table:
=========================================


<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >

<hibernate-mapping package="com.smarteck.smartcrm.cards">
<class name="Card" table="Card">
<composite-id class="com.smarteck.smartcrm.cards.CardPK" name="cardPK">
<key-property
column="cardNo"
name="cardNo"
type="integer"
/>
<key-many-to-one
class="com.smarteck.smartsecurity.users.SmartUser"
column="ownerUser"
name="ownerUser"
/>
</composite-id>
<property
column="nameOnCard"
length="255"
name="nameOnCard"
not-null="false"
type="string"
/>
</class>
</hibernate-mapping>







Code between sessionFactory.openSession() and session.close():


The Code in Struts Action:
===========================


Session s = getSession();
Criteria c = s.createCriteria(SmartUser.class);
List userList = crit.list();


// Session committed and closed here
// List object put in HttpServletRequest





Top
 Profile  
 
 Post subject: index
PostPosted: Mon Oct 17, 2005 2:08 pm 
Expert
Expert

Joined: Fri Jul 22, 2005 2:42 pm
Posts: 670
Location: Seattle, WA
The problem relies in the fact that you have used ID of the card as index.

In the essence index is the column that user code does not change or access directly, Hibernate takes care of the data in the column.
http://www.hibernate.org/hib_docs/v3/re ... ns-indexed
I would suggest creating and using a different column to be index of the card in your collection id you need to manipulate and preserve the order of cards in the collection, or simple map the list as set and specify sort column, which very well could be the card ID.
http://www.hibernate.org/hib_docs/v3/re ... ons-sorted

The second scenario will simply guarantee that cards are always in the same order.

_________________
--------------
Konstantin

SourceLabs - dependable OpenSource systems


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.