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.  [ 7 posts ] 
Author Message
 Post subject: Persisting inner classes with inheritance
PostPosted: Tue Dec 30, 2003 4:40 pm 
Regular
Regular

Joined: Wed Sep 03, 2003 9:56 pm
Posts: 58
I'm implementing the 'State' pattern from the 'Applied Java Patterns' book, and would like to be able to persist the current state using Hibernate. I've run into troubles because the State pattern dictates that the state classes (one per state) be inner classes of the context object. My context object is also persistant.

To simplify, given class A (the context), with inner state classes A.a, A.b and A.c, is it possible to map all four classes to two tables (A.a, A.b an A.c all inherit from A.z)

The inner classes (A.a, A.b and A.c) all extend base class A.z (also inner), and must map to the same table. Object 'A' maps to table TABLE_A and objects A.z, A.a, A.b and A.c all map to TABLE_B. I've been attempting 'table per class hierarchy' (unsuccessfully).

I'm able to get this working if I make the state classes separate/standalone, but not if I put them inside the context object ('A'). The problem with this approach is that the (3) state classes need to access the context object's instance data.

I know this is a rather complex scenario, but I don't seem to have trouble with the simple ones. ;)

-Mitch


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 30, 2003 4:57 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Hope the right tread this time ...

Just an idea, have you tried using the $ syntax in your mapping documents, like de.blabla.A$B where B is your inner class, and A your outer? This may work, but maybe just for static inner classes, I don't think for normal inner classes.

I don't really think Hibernate can persist normal inner classes, on retrieval, the link to the parent would have to be created somehow, and I don't think this is possible ... but maybe Hibernate surprises me once more ;) Does someone else know something about this?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 30, 2003 5:26 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
It is not supported - and cannot be done (AFAIK).

you can save *static* inner classes - but then you won't get that part saved.

How about remodelling it as classes with a has-a reference to the Context ? Should be just as good!

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 30, 2003 5:59 pm 
Regular
Regular

Joined: Wed Sep 03, 2003 9:56 pm
Posts: 58
The 'has-a' approach is what I'm currently working on. This should work fine, however there is one point on which I'm still confused.

The Chapter 16 of the Reference Doc states the following...

"Suppose we have an *interface* Payment..."

and proposes the following mapping

Code:
<class name="Payment" table="PAYMENT_ID">
    <id/>
    <discriminator column="PAYMENT_TYPE" type="string"/>
    <property name="amount column="AMOUNT"/>
    <subclass name="CreditCardPayment" discriminator-value="CREDIT"/>
    <subclass name="CashPayment" discriminator-value="CASH"/>
    <subclass name="ChequePayment" discriminator-value="CHEQUE"/>
</class>


Is this accurate? Can I specify an *Interface* as persistent? If so, what would my Configure.addClass() calls look like?

I just cannot get this to work.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 30, 2003 6:01 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
config.addClass(Payment.class)? Alternatively use config.addFile()


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 30, 2003 6:06 pm 
Regular
Regular

Joined: Wed Sep 03, 2003 9:56 pm
Posts: 58
Ridiculous type. Sorry. The Configure.addClass("Payment") works fine. Sorry.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 30, 2003 6:07 pm 
Regular
Regular

Joined: Wed Sep 03, 2003 9:56 pm
Posts: 58
"Ridiculous type" should be "Ridiculous typo"...sorry.


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