-->
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.  [ 5 posts ] 
Author Message
 Post subject: many-to-one mapping beginner problem
PostPosted: Wed Feb 27, 2008 8:22 am 
Newbie

Joined: Fri Feb 22, 2008 11:52 am
Posts: 6
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;
    }
}


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 27, 2008 10:26 am 
Newbie

Joined: Fri Jan 16, 2004 9:59 am
Posts: 18
Location: La Plata-Argentina / Luxembourg
Viince:
The problem is in this line
People thePeople = (People) peoples.get(i);

You are trying to accces the objects outside the session (currently you open a session, query the objects, and then close the session).
So the loaded objects are outside the session.
To solve this you have to (fast) solutions:
1- do all the operations inside the session (printing, looping, etc), not very good solution for some applications
2- modify the mappings an put this tag to all classes lazy=false to avoid the use of proxies


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 27, 2008 10:32 am 
Beginner
Beginner

Joined: Tue Nov 27, 2007 9:44 am
Posts: 46
You stumbled upon a feature called Lazy Initialization.

Your problem occurs when you call thePeople.getCountry(). In that moment, Hibernate recognises that it hasn't yet loaded the corresponding Country entity and tries to fetch it. Unfortunately the session is already closed.

So you have two options: Either run everything within an open session or enable eager fetching on the People - Country relationship.

Regards,
Frank


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 27, 2008 10:34 am 
Beginner
Beginner

Joined: Tue Nov 27, 2007 9:44 am
Posts: 46
Ups, too late :-(


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 27, 2008 11:14 am 
Newbie

Joined: Fri Feb 22, 2008 11:52 am
Posts: 6
Thanks a lot, it's working all of a sudden ;)


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