Hi,
Can you send me your code where this exception is given? I have executed the sample code pasted below. It worked fine.
1. There is typo in the Person.hbm.xml. Change the column name to 'name' field as:
<property
name="name"
column="name"
>
2. Insert two records into Person table.
insert into person(id, name) values(1, 'husband')
insert into person(id, name, spouse) values(2, 'wife', 1)
3. Execute the PersonDAO.
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
public class PersonDAO {
public static void main(String args[]){
Session _session = HibernateUtil.currentSession();
//System.out.println("_session: "+_session);
Person _person = (Person)_session.createCriteria(Person.class)
.add(Restrictions.eq("id", new Long(1)))
.uniqueResult();
Person _spouse = _person.getSpouse();
System.out.println("_person: "+_person);
System.out.println("_spouse: "+_spouse);
_person = (Person)_session.createCriteria(Person.class)
.add(Restrictions.eq("id", new Long(2)))
.uniqueResult();
_spouse = _person.getSpouse();
System.out.println("_person: "+_person);
System.out.println("_spouse: "+_spouse);
}
}
4. Result should be:
Hibernate: select this_.id as id0_0_, this_.name as name0_0_, this_.spouse as spouse0_0_ from Person this_ where this_.id=?
_person: com.shyam.hibernate.Person@1e228bc[spouse=<null>,name=husband,id=1]
_spouse: null
Hibernate: select this_.id as id0_0_, this_.name as name0_0_, this_.spouse as spouse0_0_ from Person this_ where this_.id=?
_person: com.shyam.hibernate.Person@19da4fc[spouse=com.shyam.hibernate.Person@1e228bc[spouse=<null>,name=husband,id=1],name=wife,id=2]
_spouse: com.shyam.hibernate.Person@1e228bc[spouse=<null>,name=husband,id=1]
Thanks
|