-->
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.  [ 4 posts ] 
Author Message
 Post subject: Problem with one-to-many relation
PostPosted: Fri Oct 01, 2010 6:59 am 
Newbie

Joined: Mon May 19, 2008 8:49 am
Posts: 5
Hi,

I implemented hibernate one-to-many relation in my application with by-directional relation.
I am using hibernate annotation to map the table with classes.

My class structure's are like :

Parent class

Code:
@Entity
@Table(name = "country")
public class Country implements Serializable {

   -------------
   
   private java.util.Collection<State> state = new java.util.ArrayList<State>();

   @OneToMany(fetch = FetchType.LAZY, mappedBy = "country")
   public java.util.Collection<State> getState() {
      return state;
   }


   public void setState(java.util.Collection<State> state) {
      this.state = state
   }

}


And relational Table class is :

Code:
@Entity
@Table(name = "state")
public class State implements Serializable {

   ----------

   private Country country;

   @ManyToOne(cascade = CascadeType.REFRESH, fetch = FetchType.LAZY)
     @JoinColumn(name = "country_id")
   public Country getCountry() {
      return country;
   }

   public void setCountry(Country country) {
      this.country = country;
   }
}



And my table's are related to each other.

I am trying to fetch the list of state by using country id by passing selected country object like :

Code:
public java.util.List findStateByCountryId(Country country) throws com.test.exception.GenericBusinessException {
   State newBean = new State();
      
   try {
      newBean.setCountryIdCountry(country);
      List list = hibernateTemplate.findByExample(newBean);
      return list;         
   } catch (DataAccessException e) {
      throw new GenericBusinessException(e);
   } finally {
   }
}



the problem with execution this is, it return me all the state present in the database.

The hibernate query executed is :

Hibernate: select this_.id as id11_0_, this_.country_id as country3_11_0_, this_.state_name as state2_11_0_ from state this_ where (1=1)

Please help me out, I am not be able to find out where i did a mistake, because earliar it was wroking

Thanks
Christabhi


Last edited by christabhi on Fri Oct 01, 2010 8:32 am, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: Problem with one-to-many relation
PostPosted: Fri Oct 01, 2010 7:33 am 
Regular
Regular

Joined: Thu Oct 19, 2006 12:07 pm
Posts: 75
This is the usual way of doing this:

Code:
// start hibernate session

Country c = load your country, using get() or load(), eg.  session.get(Country.class, countryId)

Collection<State> states = c.getState();

int i=0;
for (State state: states)
    System.out.println("state "+ (++i)  + ", name: " + state.getName());

// end  hibernate session


Note that the list of is actually loaded when you try to access them _while the session is still active_.
If you try to load it after the session is closed, you will get a LazyInitializationException.


Top
 Profile  
 
 Post subject: Re: Problem with one-to-many relation
PostPosted: Fri Oct 01, 2010 7:56 am 
Newbie

Joined: Mon May 19, 2008 8:49 am
Posts: 5
Hi Xerces8,

I am using hibernate template method. And after so many google search, I found that way (what is showed to you) to do the relation. And earlier it was working, and now i don't know what happen it stop working.

Please provide me the solution for hibernate template method.

below site showed the same thing
http://www.mkyong.com/hibernate/hibernate-one-to-many-relationship-example/


Thanks


Top
 Profile  
 
 Post subject: Re: Problem with one-to-many relation
PostPosted: Fri Oct 01, 2010 1:46 pm 
Regular
Regular

Joined: Thu Oct 19, 2006 12:07 pm
Posts: 75
What template? From Spring?

It is the same, just use hibernateTemplate.get(Country.class, countryId)


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