-->
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.  [ 3 posts ] 
Author Message
 Post subject: StaleStateException (expecting INSERT, got UPDATE)
PostPosted: Sat Mar 31, 2007 7:12 pm 
Newbie

Joined: Sat Mar 31, 2007 6:58 pm
Posts: 2
All,

New user here, I know I'm missing something simple and obvious here, but can't figure it out. TIA for help.

I am working with a very simple example, basically right out of the docs. I have a parent class (Group) and child class (Article) in a one-to-many relationship. I create a Group, and add an Article to it, commit the transaction, but get a StaleStateException when the generated SQL UPDATEs the Article table instead of INSERTing. What am I doing wrong?

NHibernate 1.2.0.3001


Code:
internal class Group
{
   private int id;
   public virtual int Id {
      get {
         return id;
      }
      set {
         id = value;
      }
   }

   private string name;
   public virtual string Name {
      get {
         return name;
      }
      set {
         name = value;
      }
   }

   private ISet articles = new HashedSet();
   public virtual ISet Articles {
       get {
           return articles;
       }
       private set {
           articles = value;
       }
   }
}

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Newsreader" namespace="Newsreader.Nntp">
   <class name="Group" table="Newsgroup">
      <id name="Id" column="GroupId" type="Int32">
         <generator class="assigned" />
      </id>
      <property name="Name" column="Name" type="String" length="256"/>
      <set name="Articles" lazy="true" >
         <key column="GroupId" />
         <one-to-many class="Article" />
      </set>
   </class>
</hibernate-mapping>

class Article
{
   private int groupId;
   public virtual int GroupId {
      get {
         return groupId;
      }
      set {
         groupId = value;
      }
   }
   private int articleId;
   public virtual int ArticleId {
      get {
         return articleId;
      }
      set {
         articleId = value;
      }
   }

   private string subject;
   public virtual string Subject {
      get {
         return subject;
      }
      set {
         subject = value;
      }
   }
}

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
   <class name="Newsreader.Nntp.Article, Newsreader" table="Article">
      <id name="ArticleId" column="ArticleId" type="Int32">
         <generator class="assigned" />
      </id>
      <property name="Subject" column="Subject" type="String" length="256"/>
   </class>
</hibernate-mapping>

void Main() {
   Configuration cfg = new Configuration();
   cfg.AddAssembly("Newsreader");

   ISessionFactory factory = cfg.BuildSessionFactory();
   ISession session = factory.OpenSession();
   ITransaction transaction = session.BeginTransaction();

   Group g = new Group();
   g.Id = 1;
   g.Name = "group";

   Article a1 = new Article();
   a1.ArticleId = 1;
   a1.Subject = "foo";
   g.Articles.Add(a1);

   Article a2 = new Article();
   a2.ArticleId = 2;
   a2.Subject = "bar";
   g.Articles.Add(a2);

   session.Save(g);
   transaction.Commit();
   session.Close();
}


Generated SQL:
Code:
INSERT INTO Newsgroup (Name, GroupId) VALUES (@p0, @p1)',N'@p0 nvarchar(5),@p1 int',@p0=N'group',@p1=1

UPDATE Article SET GroupId = @p0 WHERE ArticleId = @p1',N'@p0 int,@p1 int',@p0=1,@p1=1


Top
 Profile  
 
 Post subject:
PostPosted: Sat Mar 31, 2007 8:05 pm 
Newbie

Joined: Sat Mar 31, 2007 6:58 pm
Posts: 2
As usual, as soon as I ask for help, I'm able to solve my own problem.

It seems that NHib was not detecting my Articles as new. Changing to a generated key solved the issue.


Top
 Profile  
 
 Post subject: need unsaved-value attribute
PostPosted: Wed Jul 04, 2007 9:20 am 
Newbie

Joined: Wed Jul 04, 2007 9:12 am
Posts: 1
I think you should use unsaved-value attribute for the class so hibernate can now when to insert or update

<class name="Common.Tables.Area, Common" table="Areas">
<id name="Id" unsaved-value="0" type="Int32" column="AreaID">
<generator class="identity" />
</id>
<property ..>
</class>

_________________
Nacho
Developer


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