• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Mapping

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!!

can someone please give me solution to this problem........

I have two tables; 1) User, 2) Address with a one-to-many relation
(a User can have many addresses). I declared a list in User mapping.
But when I call user.getAddressList(), the first element in the list
is always null.

For example, if I have 5 Address records, when I call
user.getAddressList(), I am getting a List of size=5. But the first
element in the list is null and the remaining 4 elements are valid
records.

thx & rgds,
TJim.
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you show us your mapping files, and the code you use to get a user and their addresses?
 
TJim
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Paul Sturrock:
Can you show us your mapping files, and the code you use to get a user and their addresses?



Hi

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

My Hibernate version:2.1.7

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??


thx & rgds,
Jim
 
Paul Sturrock
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why have you given your child table a composite key which includes the parent id? Not a criticism as such, just curious.
 
ranger
Posts: 17346
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"TJim"-
Welcome to the JavaRanch! Please adjust your displayed name to meet the

JavaRanch Naming Policy.

You can change it

here.

Thanks! and welcome to the JavaRanch!

Mark
 
That feels good. Thanks. Here's a tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic