Hello,
I am currently strongly thinking about using Hibernate for my current project.
I am creating a test application to figure out how to use it and I am running into a problem. The documentation doesn't state how to express requests for getting informations from bindings (only updating them), which leads me to think it should be automatic. I also have looked at the sample applications and I can't really find what I did wrongly as I feel like I have done the same.
Summary : I am able to map objects to database but not their relations, I do not understand what I am doing wrong
What basically happens is that, when receiving one my people object (details are below), which has a many-to-one relation, I am unable to access it's related member (country). This leads to the following exception (when I try to access the country member of the object) :
Code:
Exception in thread "main" org.hibernate.LazyInitializationException: could not initialize proxy - no Session
at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:57)
at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:111)
at org.hibernate.proxy.pojo.cglib.CGLIBLazyInitializer.invoke(CGLIBLazyInitializer.java:150)
at test_hibernate.Country$$EnhancerByCGLIB$$374c3394.getName(<generated>)
at test_hibernate.Main.runtest(Main.java:46)
at test_hibernate.Main.main(Main.java:30)
So, my "people" class is correctly populated but It's impossible to access it's country attribute.
I can easily retreive properly the countries separately, which leads me to think that my country mapping file is correctly written.
My problem comes probably from
- The many-to-one definition of mapping
- The way I express my query
I have the following two tables populated with some data :
Code:
CREATE TABLE country(id INTEGER PRIMARY KEY AUTOINCREMENT, name varchar(50));
CREATE TABLE people(id INTEGER PRIMARY KEY AUTOINCREMENT, name varchar(50), country integer);
I am trying to get those into my objects.
My mapping files are as follow (I cut off the header to keep it short)
Code:
<hibernate-mapping>
<class name="test_hibernate.People" table="people">
<id name="id" column="id">
<generator class="native"/>
</id>
<property name="name"/>
<many-to-one name="country"
column="country"
class="test_hibernate.Country"
foreign-key="id"
/>
</class>
</hibernate-mapping>
Code:
<hibernate-mapping>
<class name="test_hibernate.Country" table="country">
<id name="id" type="long" column="id">
<generator class="native"/>
</id>
<property name="name"/>
</class>
</hibernate-mapping>
The way I am accessing the data is :
Code:
public void runtest() {
List peoples = this.listPeople();
for (int i = 0; i < peoples.size(); i++) {
People thePeople = (People) peoples.get(i);
System.out.println("people: " + thePeople.getName() + " " (Country) thePeople.getCountry(););
}
}
private List listPeople() {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
List result = session.createQuery("from People").list();
session.getTransaction().commit();
return result;
}
Here are my classes, but I believe the problem doesn't come from here
Code:
public class People {
private long id;
private String name;
private Country country;
public People() {
}
public void setCountry(Country country)
{
this.country=country;
}
public Country getCountry()
{
return this.country;
}
public void setId(long id)
{
this.id=id;
}
public long getId()
{
return this.id;
}
public String getName()
{
return this.name;
}
public void setName(String name)
{
this.name=name;
}
}
Code:
public class Country {
private long id;
private String name;
public Country() {
}
public void setId(long id){
this.id=id;
}
public long getId()
{
return this.id;
}
public void setName(String name)
{
this.name=name;
}
public String getName()
{
return this.name;
}
}