-->
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.  [ 6 posts ] 
Author Message
 Post subject: inheritance in Components
PostPosted: Thu Jan 05, 2006 10:44 am 
Newbie

Joined: Thu Jan 05, 2006 9:20 am
Posts: 5
How can I use an Interface for a Component?
I cant see a discriminator between the Component tags?
Is there an alternative way to do this


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 05, 2006 1:59 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
No Inheritance for Components. Use a full entity.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 05, 2006 3:21 pm 
Newbie

Joined: Thu Jan 05, 2006 9:20 am
Posts: 5
The problem is how I map the component to a part of the same table. Maybe I'l explain the situation.

I have an 'Instruction' entity that has a 'state' field.
The 'Instruction is mapped to a table called 'INSTRUCTION' that has a field called 'state'.

The thig is I want to implement the state pattern in my logic, so I want the state field to be of a Istate interface type and I want Hibernate to load the correct concrete class based on a text field called state in the INSTRUCTION table.

Did I make my self clear ?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 06, 2006 8:04 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Yes, pretty clear. I don't think thats possible, though.


Top
 Profile  
 
 Post subject: Implementing State Pattern via inheritance in components
PostPosted: Mon May 29, 2006 7:04 pm 
Newbie

Joined: Mon May 29, 2006 5:39 pm
Posts: 6
I have the same problem. I would like to use the State Pattern, in which each state is represented by a concrete subclass. My legacy database has a state column that holds integers representing the state. It would be very easy and elegant to implement a full State Pattern in Java and map it to the database like this:

Code:
<class name="Account" table="ACCOUNT">
    <component name="state" class="AccountState" discriminator-value="null">
        <discriminator column="STATE" type="integer"/>
        <subclass name="AccountStateActive" discriminator-value="0" />
        <subclass name="AccountStateInactive" discriminator-value="1" />
        <subclass name="AccountStateCancelled" discriminator-value="2" />
    </component>

    ...


Unfortunately, Hibernate does not (yet) support inheritance in the component tag. Hence I have 2 questions:
    1) Can this feature be added to a future feature list? I think the prevalence of this type of State Pattern alone would warrent it, and it seems consistent with the overall design and use of the framework.

    2) Does anyone have any recommendations about how to implement this without this feature? I have thought of several possibilities, all of them bad:


I thought of doing a one-to-one mapping of my AccountState class to the Account table, but I assume this will fail during inserts because, since they both point to the same table, the new AccountId will already be present when we try to insert the AccountState number (though I haven't tried this).

Another idea is to push the inheritance logic up into the domain model. That is, the stateId could be a simple integer mapped to the STATE column (which Hibernate can easily handle), and my setter for Account.stateId will handle the logic of finding and instantiating the correct subtype of AccountState. That's letting a lot of ugly persistence logic bleed into the domain model, but it should work.

A final possibility is to try to bastardize the UserType interface to treat the state as an immutable value type. That is, I could make an AccountStateUserType that is saved in the ACCOUNT table, and it uses the subtype to get the value and create the correct subclass when re-instantiating. Unfortunately, I don't see how this can work as the UserType method returnedClass() doesn't have any way to access the state number, which is needed to determine which subclass should be instantiated.

Am I missing something?

Thanks!
James


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 07, 2006 6:26 am 
Newbie

Joined: Thu Jul 07, 2005 4:21 am
Posts: 1
I also have the same problem. Essentially I want to do the same thing as James. I have a legacy table of account holders which, as well as basic account details, also holds payment information. This can be either credit card or direct debit (bank account) details. So I want my Account domain class to have a 'paymentDetail' component property, which will either be a CardPaymentDetail or DirectDebitPaymentDetail, depending on which data is present.

I think I'm going to have to map the fields as simple properties and do the appropriate PaymentDetail object creation within my domain class. It would be much cleaner if <component> supported nested <subclass>.

Al.


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