-->
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: Querying two classes
PostPosted: Wed Mar 12, 2008 12:59 pm 
Newbie

Joined: Mon Feb 04, 2008 7:24 pm
Posts: 6
Hi.

I am testing NHibernate as an option for the architecture of my systems. I created a project with two classes, made my mappings and I can query each one without problems. But I can't figure out how do I do a simple join between these two classes, though I have tried different ways as stated on the docs.

These are my two (simplified) classes:

Code:
'My main class, Persons
Public Class Persons

Private intId As Integer
Private strPerson As String

Public Overridable Property PersonId() As Integer
        Get
            Return intId
        End Get
        Set(ByVal value As Integer)
            intId = value
        End Set
End Property

Public Overridable Property PersonName() As String
        Get
            Return strPerson
        End Get
        Set(ByVal value As String)
            strPerson = value
        End Set
End Property

End Class


'My associated class, Members
Public Class Members

Private intId As Integer
Private intPersonId As Integer
Private strStatus As String

Public Overridable Property MemberId() As Integer
        Get
            Return intId
        End Get
        Set(ByVal value As Integer)
            intId = value
        End Set
End Property

Public Overridable Property PersonId() As Integer
        Get
            Return intPersonId
        End Get
        Set(ByVal value As Integer)
            intPersonId = value
        End Set
End Property

Public Overridable Property MemberStatus() As String
        Get
            Return strStatus
        End Get
        Set(ByVal value As String)
            strStatus = value
        End Set
End Property

End Class

There are a one-to-one relationship between these two classes. On the database side, the members table has a FK to the Persons table on the PersonId column. I need a method that lists the {Persons}.PersonId and the associated {Members}.Status. Do I need to create a third class that contains this method? Where should I put the mapping for this association?

TIA,
rferj


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 12, 2008 1:06 pm 
Regular
Regular

Joined: Wed Jan 25, 2006 1:11 am
Posts: 118
Location: Copenhagen, Denmark
You can map the Member onto the Person class based on the relationsship so you can Access:

Person.Member.MemberStatus

you can use this in your queries as well.

Take a look at 5.1.11 in the nHibernate reference manual


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 12, 2008 5:53 pm 
Newbie

Joined: Mon Feb 04, 2008 7:24 pm
Posts: 6
Hi jta !

I read the topic, but I should have misunderstood something. On my Persons class I defined a new property called Users, that I want to list the join between Persons and Members:

Code:
Public Overridable Property Users() As IList
    Get
        Return lstUsers
    End Get
    Set(ByVal value As IList)
        lstUsers = value
    End Set
End Property


And I changed the mapping file of my Persons class as below:


<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="TestHB" assembly="TestHB">
<class name="Persons" table="tblPersons" lazy="true">

<id name="PersonId" column="PersonId" type="Int32">
<generator class="native">
<param name="sequence">Persons_seq</param>
</generator>
</id>

<property name="PersonName" type="String" length="150" not-null="true" />

<one-to-one name="Users" class="Members" constrained="true" property-ref="PersonId" />
</class>


When I run the application, I receive errors saying that my property is invalid. Can you see where is the error? I tried some changes on the one-to-one element but could not solve this.

TIA,
rferj


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 13, 2008 3:05 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
An one-to-one association expects a single value not a list. If you really have a one-to-one relation you have to use

Code:
Public Overridable Property Users() As Members
    Get
        Return users
    End Get
    Set(ByVal value As Members)
        users = value
    End Set
End Property


or you have to map a collection

Code:
<bag name="Users" table="tblMembers">
    <key column="PersonId" />
    <one-to-many class="Members" not-found="ignore" />
</bag>

_________________
--Wolfgang


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.