-->
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.  [ 12 posts ] 
Author Message
 Post subject: Class hierarchy problem
PostPosted: Mon Feb 21, 2005 9:01 am 
Newbie

Joined: Mon Feb 21, 2005 8:36 am
Posts: 3
Hi,

I have a class hierarchy that I want to use Hibernate to save. I have read the documentation and understand that there are 3 ways to do it.

The problem is my need is quite unique. Only the base class has persistent fields. The sub classes only contain fields that are transient. But the application will work with these subclasses and when it saves them only the fields defined in the parent class needs to be saved. I have got a mapping file for parent class.

But when I try to save them Hibernate complains that it doesn’t know how to persist the class. Looking at the source code I see that you use the Class type to retrieve the mapping document.

I don’t want to introduce an artificial discriminator column since I don’t have any sub class specific things saved on the DB. When I load the classes from DB they will be of type parent class.

The only option I can think of is abandon my hierarchy and go for a delegation model. (as recommended by the book “Hibernate in Action”) but want to leave that as a last option.

If anyone has any suggestions I would greatly appreciate it.

Thanks in advance
Roshan


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 21, 2005 3:55 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
how can you yourself see which subclass the class should be when being loaded again ?

changing the id and type of an entity is two things that is normally looking for trouble....(this is a general not hibernate specific statement)

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject: Re:
PostPosted: Tue Feb 22, 2005 5:28 am 
Newbie

Joined: Mon Feb 21, 2005 8:36 am
Posts: 3
max,

ok a bit of backgroud about what I am trying to do.

I get two types of requests. They share a lot of common fields (about 40) so I have created a superclass containing all those. The rest of the fields are only used for processing the request and not needed to be stored in the DB.

So the idea is when I get a transaction I work on it then save it in the DB for future reference. If I was to load it again I treat is as a superclass type. All the fields I save is in the super class and without the transient data from the original reqeust I can't create a meaningful subclass anyway.

My first design was to have just one persistent class and two objects representing the request (which were unrelated to persistent class). Then I ended up having to copy 40 fields from the request object to and create a persistent object in order to save it. So that's why I thought I will try inheritence to solve the problem.

I understand this is a slight deviation from the normal situation where sublcasses will have persistent data as well.

Thanks
Roshan


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 22, 2005 6:34 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
im not saying subclasses require additonal data as it is their behavior that is important. But if that is the case a discriminator is needed.

Your stuff sounds like something delegation would be way better for.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 22, 2005 11:57 am 
Regular
Regular

Joined: Wed Nov 17, 2004 11:49 am
Posts: 65
Location: Pittsburgh
We had to do something similar on our project. I'll tell you what we had to do to get it to work the way we wanted and the hibernate team can tell you what the pitfalls are. One I am aware of, it's not supported by hibernate before 2.1.7.

We mapped all classes to the table directlty. So, in your case, both of the subtypes and the parent class would simply map to the same table. We did not set up any object heirarchy from a hibernate stand-point. We then set polymorphism to explicit on the parent and only used the parent for subsequent reads.

Code:

@hibernate.class table="parent" polymorphism="explicit"
public class Parent {

...

@hibernate.class table="parent
public class Child extends Parent {



Top
 Profile  
 
 Post subject: subclass problems also.
PostPosted: Wed Feb 23, 2005 1:19 pm 
Newbie

Joined: Wed Feb 23, 2005 1:02 pm
Posts: 13
Location: Sacramento, CA
Hello,

Newby to hibernate here... but I have the same problem that was posted on this thread - I use the hbm2java.CodeGenerator to generate the base classes for persistence, then I create subclasses of them called *_bean, that represents the WEB presence, it also has generic DB IO methods on it to save itself, according to its parents class structure.

I am having the exact same problem where if I pass the child class to a call like:

tx = sessLocal.beginTransaction();
session.save(hbm_instance);
tx.commit();

where hbm_instance is an instance of the child class it complains that it is an unregistered class.

Obviously I don't want to change the BASE CLASS any since it is a generated class.

Here is the entire structure:

---------------------------------------BASE CLASS
public class Customer implements Serializable {

/** identifier field */
private Long ID;

/** nullable persistent field */
private String name;

... with all the get/setters correctly defined
}
---------------------------------------BEAN
public class Customer_bean extends Customer implements dbIO
{

//The dbIOAdapter implements all the
//database:save/update/delete/query methods for the Customer_bean.

dbIOAdapter dbu=new dbIOAdapter();
public String create(Session sess)
{
return dbu.create(sess);
}

}
---------------------------------------HIB XML:
<hibernate-mapping>
<class name="com.rhinosystemsinc.ccm.hib.Customer" table="CUSTOMER">
<meta attribute="class-description">Customer of CCM
</meta>

<id name="ID" type="long" column="ID">
<meta attribute="scope-set">public</meta>
<generator class="hilo">
<param name="table">CUSTOMER_SEQ</param>
<param name="column">next_value</param>
<param name="max_lo">1</param>
</generator>
</id>
<property name="name" type="string">
<meta attribute="use-in-tostring">true</meta>
<column name="NAME" not-null="true" unique="false" index="CUSTOMER_NAME"/>
</property>

... and on...
</class>
</hibernate-mapping>

Please help me to understand how I can work around this problem without having to change the BASE CLASS or the XML structure. I have also already tried not extending the base class, and using a base class instance in the subclass, as a delegate. That didn't work as the XML and the bean class are still at odds.

The *fix* would be to have hibernate recognize subclasses of persistent classes as valid persistent classes themselves. Short of that, what is the work-around for now?

-Joel


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 23, 2005 2:40 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
look into using meta attribute generated class which allows you to tell hbm2java to generate a base class that you then extend naming it the right name (in this case Customer)

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 23, 2005 3:26 pm 
Newbie

Joined: Wed Feb 23, 2005 1:02 pm
Posts: 13
Location: Sacramento, CA
can you give an example, that fits with the Customer example I provided?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 23, 2005 3:39 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
did you even try go looking for it in the docs/forums ?


<class name="Customer">
<meta attribute="generated-class">CustomerBase<meta/>
</class>

class Customer extends CustomerBase ....

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 23, 2005 4:40 pm 
Newbie

Joined: Wed Feb 23, 2005 1:02 pm
Posts: 13
Location: Sacramento, CA
So, actually I did try to look but could not find it... I was hoping you knew the answer off the top of your head--sorry if this was an inconvenience. Nonetheless, I appreciate it.
Thanks,
Joel


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 24, 2005 1:03 am 
Newbie

Joined: Wed Feb 23, 2005 1:02 pm
Posts: 13
Location: Sacramento, CA
worked like a charm...
Thanks again.
-Joel


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 24, 2005 5:33 am 
Newbie

Joined: Mon Feb 21, 2005 8:36 am
Posts: 3
Kerstetter,

Thank you very much for the suggestion. I tried it out and it works fine.

In my case I have made the subclass's polymorphism attribute to be "explicit". In practice I never load the subclass from the DB anyway.

I read the article http://www.hibernate.org/41.html about light weight classes and so far I can't see any issues with this solution. (at least in the context I have used it)

Roshan


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