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.  [ 1 post ] 
Author Message
 Post subject: any mapping doesn delete orphaned row
PostPosted: Fri May 15, 2009 5:56 am 
Newbie

Joined: Thu Feb 05, 2009 11:46 am
Posts: 4
Hi everyone, I have a few classes that look like this,

Code:
public class Token
{
    public int Id
    {
        get;
        set;
    }

    public ITokenInstance Instance
    {
        get;
        set;
    }
}

public interface ITokenInstance
{
    int Id
    {
        get;
        set;
    }

    Token Token
    {
        get;
        set;
    }
}


and mapping files

Code:
<class name="Token" >
   <id name="Id" >
      <generator class="hilo" />
   </id>

   <any name="Instance" meta-type="class" id-type="Int32" cascade="all-delete-orphan">
      <column  name="instance_type" />       
      <column name="instance_id" />
   </any>
</class>

<class name="TokenInstanceOne" >
   <id name="Id" >
      <generator class="hilo" />
   </id>

   <many-to-one name="Token" class="Token" column="token_id"/>
</class>


The problem is that if a new instance is assigned to the token the old instance is not deleted from the database, below is an example of the code i need to write

Code:
Token token = Session.Get<Token>(4);
var instance = Session.Get<TokenInstanceOne>(1);
Assert.AreSame(token.Instance, instance);

var newInstance = new TokenInstanceOne();
token.Instance = newInstance;
newInstance.Token = token;
instance.Token = null;
Session.Flush();


NHibernate produces the following SQL;

1. INSERT INTO TokenInstanceOne (token_id, Id) VALUES (@p0, @p1); @p0 = '4', @p1 = '32768'
2. UPDATE Token SET instance_type = @p0, instance_id = @p1 WHERE Id = @p2; @p0 = 'ClassLibrary1.TokenInstanceOne', @p1 = '32768', @p2 = '4'
3. UPDATE TokenInstanceOne SET token_id = @p0 WHERE Id = @p1; @p0 = '', @p1 = '1'

What i need to happen is that in number 3 instead of it updating to '', it should delete the row. If i put not-null="true" on the many-to-one mapping i get an exception:
PropertyValueException : not-null property references a null or transient value

I have also asked this question here, but have had no luck. Does anyone know how i can map this correctly?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.