-->
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.  [ 2 posts ] 
Author Message
 Post subject: Why does Hibernate instaciate so many objects ?
PostPosted: Wed Jan 19, 2005 6:30 am 
Beginner
Beginner

Joined: Wed Jan 19, 2005 6:07 am
Posts: 20
Hibernate version:2.1.17


Name and version of the database you are using: mySql

I have created two classes where the one is an extension of the other.

I have mapped both classes to the same database-file.

The first class is a 'lightweight'-class with fewer properties than the child.

Code:
public class PortationLight extends Observable {
// just a few properties
}

public class Portation extends PortationLight{
//a lot of extra properties
}

<hibernate-mapping>

<class name="....PortationLight" table="polka_portation">
<!-- the same few properties -->
</class>
<class name="....Portation" table="polka_portation">
<!-- the same few properties plus the extra properties -->
</class>

public class PortationDao extends HibernateDaoSupport { //using Spring

    public List findActiveLightPortations() {
        return getHibernateTemplate().find("from PortationLight p where p.active = ?", new String[]{"T"});
    }


}



I am logging the instanciating of both classes in my log, and this shows me :

When calling the 'findActiveLightPortations'-method my framework does ;

Creates a full set of the heavy-weight objects, then creates a full set of the light-weight objects, then returns a set of the lightweight-objects , and THEN creates another full set of the heavy-weight objects.

A total of 3 times the objects I need.

Sometimes the creation of the last full set of heavy-weight objects occurs before the method returns it list, and sometimes even before the creation of the light-weight objects. But always 3 complete sets.

The only reason for ever coding the lightweight-object, was to save memory during instanciation. The creation of the heavy-objects was something i wanted to do on a single basis whenever I needed it.

Is this a Hibernate issue, or a spring-issue (spring 1.1.3) ?

Frode Halvorsen


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 19, 2005 1:40 pm 
Regular
Regular

Joined: Tue Jun 22, 2004 8:01 pm
Posts: 106
Location: PowderTown, Utah, USA
What you're using is called the "Flyweight Patter" and it does work in Hibernate. (I use it on objects with BLOBs such as images when I don't want the whole thing loaded into memory.)

I didn't see your mapping code in your post, but I can guess that you haven't set polymorphism="explicit" in your class mapping for your flyweight class.

Take a look at the reference manual and read up on the polymorphism="implicit|explicit" (it's scattered through the section, keep reading.)


Also, take a look at the "Lightweight Class" pattern in the Hibernate Patterns section of the site. It details what you're trying to do:
http://www.hibernate.org/41.html


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