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: Get superclass instance
PostPosted: Fri Jul 28, 2006 2:58 pm 
Newbie

Joined: Thu Jun 15, 2006 3:40 pm
Posts: 4
Hi,

I have two entities: Person and AdminPerson. A person is any person registered in the database. An AdminPerson is a person that has a login and password.

I have defined this inheritance as table per subclass. When I try to load an instance of a superclass (Person) I get an instance of the subclass (AdminPerson) if it exists.

I would like to get an instance of the Person even if the Person is an AdminPerson. I tried to define polymorphism="explicit" but it doesn't have any effect.

What's the best way to accomplish this?


Hibernate version:
1.2.0.Alpha2

Mapping documents:

something like this:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0" schema="dbo">
<class name="Domain.T_Person" lazy="false" table="PERSON" polymorphism="explicit">
<id name="ID" type="Decimal" column="USERID">
<generator class="identity" />
</id>
<property name="FirstName" column="FirstName" type="String" />
<property name="LastName" column="LastName" type="String" />
<joined-subclass name="Domain.T_AdminPerson" table="ADMIN_PERSON">
<key column="USERID"/>
<property name="Login" column="UserName" type="String" />
<property name="Password" column="Password" type="String" />
</joined-subclass>
</class>
</hibernate-mapping>


Code between sessionFactory.openSession() and session.close():

T_Person person = session.Get<T_Person>(1);

Name and version of the database you are using:

MS Sql Server 2000


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jul 30, 2006 6:06 am 
Senior
Senior

Joined: Wed Jun 15, 2005 4:17 am
Posts: 156
For some reason you are against the current OOP wisdom...
In your case you should break the hierarchy and add the extra data for AdminUser as a one-to-one relationship to the person.

HTH,
Radu


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jul 30, 2006 10:36 am 
Newbie

Joined: Thu Jun 15, 2006 3:40 pm
Posts: 4
Thank you for your reply. But the real problem is that I have another entity that has a reference to a Person:

[Serializable]
class T_Request()
{
.
.
public DateTime RequestDate
public T_Person RequestedBy

.
.

}

I want to serialize the T_Request and I'm getting the message "Domain.T_AdminPerson was not expected. Use the XmlInclude or SoapInclude attribute to specify types that are not known statically". Of course, this occurs only if the Person is an AdminPerson.

The XmlInclude attribute is valid only on class, struct, method, interface. This is the reason why I would like to "force" the superclass. Do you have any suggest?


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jul 30, 2006 12:46 pm 
Senior
Senior

Joined: Wed Jun 15, 2005 4:17 am
Posts: 156
Well, this is not C++ with objects on the stack which can be "sliced"...
Some alternatives:
1. study the documentation, it should be able to serialize a polymorphic collection, maybe requires more work on your part.

2. "brute force" solution (I consider it ugly): build a conversion operator or a constructor able to convert an AdminPerson back to Person. Iterate the resulting collection and replace AdminPerson instances with newly created copies of Person.

3. My previous post was already explaining you how to remove the inheritance hierarchy and add Admin part as a separate object in an one-to-one relationship, so your collection will be only of Person objects.

I tend to preffer 1., try to search for info on how to serialize a polymorphic collection.

HTH,
Radu


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jul 30, 2006 12:48 pm 
Senior
Senior

Joined: Wed Jun 15, 2005 4:17 am
Posts: 156
-- duplicate posting, please disregard --


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.