-->
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: Proxy and super class loading
PostPosted: Wed Jun 15, 2005 6:06 pm 
Senior
Senior

Joined: Tue Jan 11, 2005 5:03 pm
Posts: 137
Location: Montreal, Quebec
Hi,

I dont know how to look at this problem, but it is quite simple in fact. I am not expert in Hibernate Proxy, and the doc I have found dont give me a lot of information, also the tests I have made are not conclusive.

The problem is that most of my database tables have some identical fields that are never used by my application (CreationDate, CreationUser, CreationTime, LastUsedSoftware, LastUsedUser, etc).

Because most of the time it will never be read, I dont want to load all theses fields (in the Hibernate generated SQL) when I load my Java Pojo. Also, for some obvious reason, it would be nice if theses fields would not be present in my Pojo, but only in a super class.

But I still want to map this data if somebody want to use them someday. So I did a super class that have all theses unused fields and all my Pojo extends this superclass.

I guess here what I need is a "proxy" where the super class would be a "Cat" (having the CreationDate, CreationUser, CreationTime, LastUsedSoftware, LastUsedUser, etc) and the under class would be a "DomesticCat" having the field I want to read. The proxy class would be then the "DomesticCat" interface?

The Cat would have theses properties:
- CreationDate
- CreationUser
- CreationTime

and the DomesticCat would have theses properties:
- id
- name
- master

Theses 2 classes are mapped on the same database table name "CatTable" that have theses fields:
- id
- name
- master
- CreationDate
- CreationUser
- CreationTime


I would like to know if there is a way to load a DomesticCat loading only the DomesticCat properties (id, name and master). All the Cat properties should be access in a lazy way; only if somebody do a domesticCat.getCreationDate() per example.

-- So when I do:
Code:
DomesticCat myCat = session.load(DomesticCat.class, ID);

It sould load only the domestic cat properties by the proper SQL
and when I do
Code:
myCat.getCreationDate();

It should then do another SQL to load other properties of Cat...

How could I do this kind of thing by Hibernate?

Thanks.

Etienne.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 15, 2005 10:50 pm 
Senior
Senior

Joined: Thu May 12, 2005 11:40 pm
Posts: 125
Location: Canada
Hibernate 3 supports lazy loading of individual properties. It requires build time bytecode instrumentation and the devs seem bitter about having implemented it, but it's there for you. :P


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 16, 2005 3:34 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
alternatively http://www.hibernate.org/41.html

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 16, 2005 10:49 am 
Senior
Senior

Joined: Tue Jan 11, 2005 5:03 pm
Posts: 137
Location: Montreal, Quebec
Thanks for your answers.

One of the goal i wanted, that i didn't describe in my previous post is that i want to keep it simple and clean. To change the byte code could be an option, but the thing I liked of Hibernate is that it doesnt need any build time processing.

Following the "LightWeight class", if adding theses 5 or 7 new properties to all my classes means that I need to duplicate the xml mapping (and therefore the Spring mapping...) this might not be an option for me. I prefer to get rid of theses fields for all my classes... even if this has some effect on the vision of the whole project.

Because, following the "LightWeigh exemple", it would means to me that there is a need to create a set of 2 classes for each element of complexity plus a extended xml Schema...

Code:
public myClass;
public myClass_withPropertiesExtension;
public myClass2;
public myClass2_withPropertiesExtension;
public myClass3;
public myClass3_withPropertiesExtension;



If I understood the proxy practice correctly (probably not), the way I see it at first glance was that i could mapped a extended class on a table and tell Hibernate in the mapping that the "proxy" would be an Interface (there I can build an Interface for each Pojo, No problem here) containing only the needed fields for the first SQL.

Here the way I see it was that all my Pojo would extends a single class:

Code:
public myClass extends PropertiesExtension;
public myClass2 extends PropertiesExtension;
public myClass3 extends PropertiesExtension;
protected abstract PropertiesExtension;


MyClass, myClass2 and myClass3 extends an PropertiesExtension where the non-wanted element ares.
Code:
public class myClass extends PropertiesExtensionimplements java.io.Serializable {

   // Fields   
   String id
   String name;
   String description;
   // add here getters + setters
}

public class myClass2 extends PropertiesExtension implements java.io.Serializable {

   // Fields   
   String id
   String otherField;
   String otherField2;
   String descriptionENG;
   // add here getters + setters
}



For my ExtendedElements classes, where the no-wanted element are, I dont want to get this class along, so it is abstract.
Code:
public abstract class PropertiesExtension{

   private java.lang.String lastMaintUser;
   private java.lang.String lastMaintType;
   private java.lang.String lastMaintJob;
   private int lastMaintTime;
   private int lastMaintDate;
   // add here getters + setters



And this is the part of xml mapping I guess i could work (but not in my tests) :

Code:
<class name="com.myClass"
           table="myClassTable"
           proxy="com.myClass"> <-- I could put an identical Interface here of myClass, I dont mind!
etc, etc, ...
</class>

<class name="com.myClass2"
           table="myClassTable2"
           proxy="com.myClass2"> <-- I could put an identical Interface here of myClass2...




I hope this is not science fiction...

Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 02, 2005 9:36 am 
Senior
Senior

Joined: Tue Jan 11, 2005 5:03 pm
Posts: 137
Location: Montreal, Quebec
Hi,

the way I have resolve this problem was to use the Hibernate component for all the "changelog" data.

Instead of extending a class I put a ChangeLog property in the class. The main reason is that the class cannot be defined as a "is a" ChangeLog, because it only contain the data. I could add an Interface saying that it contains a changeLog if I need to work with the changeLog specifically.

The issue to lazy load the component is not very important, but the Object code seems cleaner now.

Etienne.


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.