I just read another article regarding one-to-one:

http://fabiomaulo.blogspot.com/2010/03/conform-mapping-one-to-one.html

I did not tried to code yet but maybe deletes do work for one-to-one.

--
Ramon

On Wed, May 2, 2012 at 5:37 PM, Ramon Smits <[email protected]> wrote:

> Host -> Sequence (Host.Sequence) [one-to-one]   : host.sequence points to
>> one sequence.
>> Sequence -> Host (Host.Sequences) [one-to-one] : sequence.host points to
>> one host.
>>
>
> I understand, problem is that only one of the tables has a foreignkey
> relation to the other. This is th many-to-one side where the other side is
> mapped as one-to-one.
>
> Examples for such a mapping are:
>
> http://ayende.com/blog/3960/nhibernate-mapping-one-to-one
>
> http://nhforge.org/wikis/howtonh/lazy-loaded-one-to-one-with-nhibernate.aspx
>
>
>
>> So that when I set host.sequence = null the sequence is deleted. Note,
>> host and sequence are held in separate tables.
>>
>
> Always make sure that you set both sides of the relation to null. Don't
> use objects as tables. You do this by making one of the classes responsible
> for managing the relation.
>
> In your example I would somehow expect methods like the following pseudo
> code:
>
> Host::SequenceLink(Sequence sequence)
> {
>     Sequence = sequence;
>     sequence.Host = this;
> }
>
> Host::Sequence SequenceUnlink()
> {
>     var s = Sequence;
>     s.Host = null;
>     Sequence = null;
>     return s;
> }
>
> This would also simplity management of the remaining Sequence entity:
>
>
> var host = session.Get<Host>(id);
> session.Delete(host.SequenceUnlink());
>
>
> AFAIK, orphened childs only work for collections but I must say that I
> understand your problem and wondering why it does not work especially when
> you consides Host/Sequence to be an aggregate with Host as the root object.
>
> What you could do if use the set approach internally and use something
> like:
> protected ICollection Sequences { get;set } // Managed by NHibernate
> mapping
>
> public Sequence Sequence
> {
> set
> {
> if(Sequences.Length>0)
>  {
> var s = Sequences[0];
> s.Host = null;
>  Sequences.Clear();
> }
>  if(value!=null)
> {
> value.Host = this;
>  Sequences.Add(value);
> }
> }
>  get
> {
> if(Sequences.Length==0) return null;
>  return Sequences[0];
> }
> }
>
>
> But that would be a very nasty hack which I think should be avoided.
>
> --
> Ramon
>



-- 
Ramon

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

Reply via email to