-->
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.  [ 4 posts ] 
Author Message
 Post subject: Mapping Interface Type produces strange GetType() call
PostPosted: Thu Aug 09, 2007 8:32 am 
Newbie

Joined: Wed Jul 11, 2007 11:22 am
Posts: 3
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version: Build 1.2.0.GA

I'm attempting to map the following:

Staff object: A staff object maintains a collection of roles

Code:
IList<StaffRole> staffRoles;


A StaffRole object maintains a link to the role type

Code:
IStaffRole role;


This all appears to work fine until I do something like this

Code:
foreach(StaffRole r in someStaffMember.Roles) {
    Console.WriteLine(r.Role.GetType());
}


Now I would have thought that it would print something like

SomeNameSpace.Project.ModuleLeaderRole;

but instead it prints

ProxyInterfaceSystemSystemObject_ProxyInHibernateProxy_System_Runtime_Serialization

My question is, how can I (if its possible) get the call to GetType to return the correct type of object.

The important bits of mapping files look like

Interface mapping file

Code:
<class name="IStaffRole" table="Role">
    <id name="RoleID" column="RoleID" type="Int32">
      <generator class="native" />
    </id>
    <discriminator column="RoleName" type="String"/>

    <subclass name="ModuleLeaderRole"   discriminator-value="ModuleLeaderRole">
      <property name="RoleName" column="RoleName" type="String"/>
      <property name="Description" column="Description" type="String" />
    </subclass>
     
  </class>


Staff Role Mapping file
Code:
<class  name="StaffRole" table="StaffInRoles">
    <id name="StaffInRoleID" column="StaffInRoleID" type="int">
      <generator class="native"></generator>
    </id>
    <many-to-one name="Staff" column="StaffID" not-null="true"/>
    <many-to-one name="Role" column="RoleID" not-null="true" class="IStaffRole"/>
  </class>


Collection of roles from Staff Mapping file
Code:
<bag name="StaffRoles" lazy="true" cascade="save-update" inverse="false" >
      <key column="StaffID"/>
      <one-to-many class="StaffRole"/>
    </bag>


Many thanks if anybody can shed some light on this.[/b]


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 09, 2007 11:51 am 
Newbie

Joined: Thu Jun 28, 2007 7:30 pm
Posts: 7
It works taht way by design. Read the section in the docs on proxies. And you can always use Type.IsAssignableFrom(Type t) if you need to check for type 'equality.'


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 13, 2007 11:42 am 
Newbie

Joined: Wed Jul 11, 2007 11:22 am
Posts: 3
Sorry but I'm still slightly confused.

I've done some tweaking and from what I can see this should work. I am of course wrong because it doesnt ;-)

I have the following.

Code:
abstract class Role {}

class ProgrammeRole : Role {}

class Staff {

    IList<Role> roles;

    public Role OfRole(String value) {

        /// within list try and find role;
    }
}


However if a client ever tries to call this by doing something like

Code:
Staff staff = DAO.GetStaffSomeHow();
ProgrammeRole foundRole = (ProgrammeRole )staff.OfRole("Programme");


I get a InvalidCastException.

So is it not possible to achieve this using NHibernate or am I being stupid :-)

Thanks

Jon


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 13, 2007 12:36 pm 
Newbie

Joined: Wed Jul 11, 2007 11:22 am
Posts: 3
I have worked out a solution by not using an association class to model the link between staff and roles.

Seems to work out okay.


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