-->
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.  [ 7 posts ] 
Author Message
 Post subject: the retrieved collection is null
PostPosted: Wed Feb 18, 2009 1:28 am 
Newbie

Joined: Sun Feb 08, 2009 4:33 am
Posts: 8
Hi every body ,
I m new to hibernate and i have a problem in loading a collection from the database

I have user class
Code:
package helper;


import java.util.HashSet;
import java.util.Set;

public class User {

   private Long id;
   private String first_name;
   private String second_name;
   private String user_name;
   private String password;
   private Set addresses;
   private Set phone;
   private String email;
   private boolean admin;
   private Department department;
   
   public User(String first_name,String second_name,String user_name,String password
         ,String email){
      this.first_name=first_name;
      this.second_name=second_name;
      this.user_name=user_name;
      this.password=password;
      this.email=email;
      addresses=new HashSet();
      phone=new HashSet();
   }
   
   public User(){
      first_name="first";
      second_name="second";
      user_name="user name";
      password="password";
      email="email@yahoo.com";
      addresses=new HashSet();
      phone=new HashSet();
   }
// getters and setters


and Address class:
Code:
package helper;

public class Address {
   
   private Long id;
   private User user;
   private String country;
   private String city;
   private String street;
   private String building;
   private int appartment;
   
   public Address(){
      country="default";
      city="default";
      street="default";
      building="default";
      appartment=0;
   }
   
   public Address(String country,String city,String street,
         String building,String appartment){
      this.country=country;
      this.city=city;
      this.street=street;
      this.building=building;
      if(appartment !=null && appartment.length()>0)
         this.appartment=Integer.parseInt(appartment);
      else
         this.appartment=0;
   }
//getters and setters



The user class mapping is:

Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
   "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="helper">
  <class name="User" table="user_table"
  dynamic-update="true">
    <id name="id"
       column="user_id"
        >
        <generator class="native"/>
     </id>
     
     <property name="first_name"
        column="user_first_name"
        type="string"/>
        
     <property name="second_name"
        column="user_second_name"
        type="string"/>
        
     <property name="user_name"
        column="user_name"
        type="string"/>
        
     <property name="password"
        column="user_password"
        type="string"/>
        
     <set name="addresses"
        outer-join="true"
        inverse="true"
        cascade="all-delete-orphan">
        <key column="address_id" />
        <one-to-many class="Address" />
    </set>
        
     <set name="phone"
        inverse="true"
        cascade="all-delete-orphan">
        <key column="user_phone" />
        <one-to-many class="Phone"/>
    </set>
     <property name="email"
        column="user_email"
        type="string"/>
     
     <property name="admin"
        column="user_admin"
        type="boolean"/>
        
      <many-to-one name="department"
       column="user_department"/>
        
        
  </class>
</hibernate-mapping>


the address class mapping is:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
   "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="helper">
  <class name="Address" table="address_table" dynamic-update="true">
 
     <id name="id"
        column="address_id">
     <generator class="native"/>
     </id>
     
     <many-to-one
        name="user"
      column="user_id"
      class="helper.User"
      not-null="true"/>
      
   <property name="country"
        column="address_country"
        type="string"/>
        
     <property name="city"
        column="address_city"
        type="string"/>
        
     <property name="street"
        column="address_street"
        type="string"/>
        
     <property name="building"
        column="address_building"
        type="string"/>
        
     <property name="appartment"
        column="address_appartment"
        type="int"/>
  </class>
</hibernate-mapping>


I have no problem saving the user and its associated collection of addresses but i have a problem retrieving the collection

when i run this code
Code:
         Vector usersVector=null;
         
         SessionFactory sessionFactory=new  Configuration().configure().buildSessionFactory();
         Session newSession=sessionFactory.openSession();
         Transaction transaction=newSession.beginTransaction();
         
         java.util.List results= newSession.createCriteria(User.class).setFetchMode("addresses", FetchMode.EAGER).addOrder(Order.asc("first_name")).list();
         
         Iterator iter=results.iterator();
         if(iter.hasNext())
            usersVector=new Vector();
         while(iter.hasNext()){
            User user=(User)iter.next();
            usersVector.add(user);
         }
         
         
         
         for(int i=0;i<usersVector.size();i++){
            User user=(User) usersVector.elementAt(i);
            Iterator it=user.getAddresses().iterator();
            Address ad=null;
            if(it.hasNext())
               ad=(Address)it.next(); 
               System.out.println("____user____"+user.getFirst_name());
              System.out.println("_____ad_____"+ad);
              if(ad !=null)
              System.out.println("____country__"+ad.getCountry());
         }
         
         transaction.commit();
         newSession.close();
         



i do not get any errors but i get

____user____Sarah
_____ad_____null
____user____Tonny
_____ad_____null


could any body please tell me what am i doing wrong??

Thanks in advance


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 18, 2009 1:52 am 
Regular
Regular

Joined: Thu Sep 06, 2007 2:22 am
Posts: 108
Location: Noida,India
Problem is in your mapping. Please make following changes.
In User class mapping -
Code:
<set name="addresses"
        outer-join="true"
        inverse="true"
        cascade="all-delete-orphan">
        <key column="address_id" /> //Here is problem
        <one-to-many class="Address" />
    </set>

Correct One is

<set name="addresses"
        outer-join="true"
        inverse="true"
        cascade="all-delete-orphan">
        <key column="user_id" />
        <one-to-many class="Address" />
    </set>


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 18, 2009 7:20 am 
Newbie

Joined: Sun Feb 08, 2009 4:33 am
Posts: 8
hi parmendratyagi ,

Thanks for your help, it solved my problem.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 18, 2009 7:51 am 
Newbie

Joined: Sun Feb 08, 2009 4:33 am
Posts: 8
hi ,

i have in my database 2 user records , the first user has one address in the address table and the second user has 2 addresses in the address table.

but the results list size is 3(i thought it should be only 2 as there are only 2 users),the user with 2 addresses is appears twice in the results list.

could any body tell me how to solve this??


Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 18, 2009 8:10 am 
Regular
Regular

Joined: Thu Sep 06, 2007 2:22 am
Posts: 108
Location: Noida,India
Please post the code which is using to retrieve USER with Query(HQL or SQL).


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 18, 2009 8:20 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
On your critera, try calling (before you call list() ):

Code:
Criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY );


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 18, 2009 8:26 am 
Newbie

Joined: Sun Feb 08, 2009 4:33 am
Posts: 8
hi nordborg ,

Thanks that solved the problem


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.