[nhusers] nhiberante one-to-many mapping

247 views
Skip to first unread message

Yauhen

unread,
Apr 27, 2010, 10:03:58 AM4/27/10
to nhusers
Hello All,

I have problems with cascading of children.

I want to map one to many objects Person
and PersonAddress

public class Person
{
public virtual int Id {get; set;}
public virtual string FirstName {get; set;}
public virtual ICollection<PersonAddress> PersonAddress { get; set; }
}

public class PersonAddress
{
public virtual int Id {get; set;}
public virtual int PersonId {get; set;}
...
}

I don't want to have person object property in address. It creates
cyclic references and don't necessary for my application.


mapping file is like following:

<class name="Person" table="Persons" >
<id name="Id" type="Int32" column="PersonId">
<generator class="identity"/>
</id>
<set name="PersonAddress" table="PersonAddress" lazy="true"
fetch="join" outer-join="true" cascade="all-delete-orphan">
<key column="PersonId"></key>
<one-to-many class="PersonAddress"/>
</set>

</class>

<class name="PersonAddress" table="PersonAddress" >
<id name="Id" type="Int32" column="Id">
<generator class="identity"/>
</id>
<property name="PersonId" column="PersonId" type="Int32"/>
<property name="PhoneWork" column="PhoneWork" type="String"/>

</class>

when trying to insert Person with person address I am receiving
exception. Because it tries to insert PersonAddress with invalid id
(default -1, 0, etc).

in samples that I have found it is specified back reference from child
to parent
<many-to-one. In my case I have't parent object and don't want it.
Integer field could not be mapped.
So is there way to do cascade insert without parent object?

--
You received this message because you are subscribed to the Google Groups "nhusers" group.
To post to this group, send email to nhu...@googlegroups.com.
To unsubscribe from this group, send email to nhusers+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/nhusers?hl=en.

Fabio Maulo

unread,
Apr 27, 2010, 10:11:47 AM4/27/10
to nhu...@googlegroups.com
On Tue, Apr 27, 2010 at 11:03 AM, Yauhen <yauh...@gmail.com> wrote:

   public class PersonAddress
   {
         public virtual int Id {get; set;}
         public virtual int PersonId {get; set;}
       ...
  }

I don't want to have person object property in address. It creates
cyclic references and don't necessary for my application.


And what is that property "PersonId" ? do you want or you don't want ?
 
--
Fabio Maulo

Diego Mijelshon

unread,
Apr 27, 2010, 10:12:36 AM4/27/10
to nhu...@googlegroups.com
Yauhen,

Your are basically trying really hard to do things in a wrong way.
Having a proper bidirectional relationship is the best way to maintain cascading collections.
"Cyclic references" are not a problem, unless you are trying to automatically serialize your domain entities (wrong).
If you absolutely don't want the Person reference and are willing to accept two SQL statements (insert and update), plus having PersonAddress.PersonId nullable in the DB, you can remove the property from PersonAddress completely.

   Diego

Yauhen

unread,
Apr 27, 2010, 10:17:29 AM4/27/10
to nhusers
1) PersonAddress.PersonId not nullable in the DB. Could not be
changed. Backward compatibility
2) most of the queries are slq-queries not hql. so i don't want to
select all person info when selecting just person address. I really
don't want it.
sql-queries are used due of a lot of secondary table references that
are used for filtering of persons but for which I really don't need
separate objects because they are not displayed anywhere. And didn't
go somewhere outside of filtering.

So as I undertsand without separate object property to parent I could
not do this?
> > nhusers+u...@googlegroups.com<nhusers%2Bunsu...@googlegroups.com­>
> > .
> > For more options, visit this group at
> >http://groups.google.com/group/nhusers?hl=en.
>
> --
> You received this message because you are subscribed to the Google Groups "nhusers" group.
> To post to this group, send email to nhu...@googlegroups.com.
> To unsubscribe from this group, send email to nhusers+u...@googlegroups.com.
> For more options, visit this group athttp://groups.google.com/group/nhusers?hl=en.

Yauhen

unread,
Apr 27, 2010, 10:19:59 AM4/27/10
to nhusers
practically I don't it this property. Only if insert/update could not
written without setting it I will need.
If could - no problem I could remove. But as I have described in next
post I am heavily vased on sql-queries and don't want to do any things
to maintain parent object.

On Apr 27, 5:11 pm, Fabio Maulo <fabioma...@gmail.com> wrote:

Diego Mijelshon

unread,
Apr 27, 2010, 10:23:09 AM4/27/10
to nhu...@googlegroups.com
This is documented in 6.4. One-To-Many Associations (last paragraph).
Also, you DON'T need to select Person fields when querying PersonAddress. Where did you get that idea?
<many-to-one/> is lazy by default.

   Diego

Yauhen

unread,
Apr 27, 2010, 10:28:28 AM4/27/10
to nhusers
so no way without bidirection?
And this is the only correct and POSSIBLE way?

On Apr 27, 5:23 pm, Diego Mijelshon <di...@mijelshon.com.ar> wrote:
> This is documented in 6.4. One-To-Many
> Associations<http://nhforge.org/doc/nh/en/index.html#collections-onetomany>
> > <nhusers%2Bunsu...@googlegroups.com<nhusers%252Bunsubscribe@googlegroup­s.com>

Diego Mijelshon

unread,
Apr 27, 2010, 10:33:39 AM4/27/10
to nhu...@googlegroups.com
Yes. And I still don't see why that would be a problem.

   Diego

Yauhen

unread,
Apr 27, 2010, 10:36:58 AM4/27/10
to nhusers
Serialization to javascript
> > > > <nhusers%2Bunsu...@googlegroups.com<nhusers%252Bunsubscribe@googlegroup­s.com>
> > <nhusers%252Bunsubscribe@googlegroup­s.com>
> > > > ­>
> > > > > > .
> > > > > > For more options, visit this group at
> > > > > >http://groups.google.com/group/nhusers?hl=en.
>
> > > > > --
> > > > > You received this message because you are subscribed to the Google
> > Groups
> > > > "nhusers" group.
> > > > > To post to this group, send email to nhu...@googlegroups.com.
> > > > > To unsubscribe from this group, send email to
> > > > nhusers+u...@googlegroups.com<nhusers%2Bunsu...@googlegroups.com­>
> > <nhusers%2Bunsu...@googlegroups.com<nhusers%252Bunsubscribe@googlegroup­s.com>
> > ­>
> > > > .

Diego Mijelshon

unread,
Apr 27, 2010, 10:41:55 AM4/27/10
to nhu...@googlegroups.com
Use a DTO or a serialization library that you can customize (i.e. tell it to serialize PersonAddress.Person.Id instead of going deep into Person)

   Diego

Fabio Maulo

unread,
Apr 27, 2010, 10:47:15 AM4/27/10
to nhu...@googlegroups.com
That property is the bidiretional...
if you use the class Person (many-to-one) instead the field PersonId you have exactly the same persistent representation.
Reply all
Reply to author
Forward
0 new messages