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