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