-->
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.  [ 4 posts ] 
Author Message
 Post subject: Parent-child relation and not-null foreign key
PostPosted: Fri Aug 03, 2007 8:41 am 
Newbie

Joined: Thu Aug 02, 2007 8:40 am
Posts: 3
Hibernate version: 1.2.0
.Net version: 2
Studio Version: 2005 SP1

A question is about parent-child relation. I created simple example in which my problem can be repeated.

Parent table
Table1
(
id bigint identity, -- parent id
constraint PK_TABLE1 primary key (id)
)

Child table
Table2
(
id bigint identity, -- child id
pid bigint not null, -- parent id
constraint PK_TABLE2 primary key (id)
)

Relation
alter table Table2
add constraint FK_TABLE2_REFERENCE_TABLE1 foreign key (pid)
references dbo.Table1 (id)
on update cascade

Parent class
namespace winapp
{
class Root
{
private Int64 _id;
public virtual Int64 id
{
get { return _id; }
set { _id = value; }
}

private IList<Leaf> _leafList = new List<Leaf>();
public virtual IList<Leaf> LeafList
{
get { return _leafList; }
set { _leafList = value; }
}

public Root()
{
_leafList.Add(new Leaf());
}
}
}

Child class
namespace winapp
{
class Leaf
{
private Int64 _id;
public virtual Int64 id
{
get { return _id; }
set { _id = value; }
}

private Int64 _pid;
public virtual Int64 pid
{
get { return _pid; }
set { _pid = value; }
}
}
}

Parent class mapping
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="winapp" assembly="winapp">
<class name="Root" table="Table1">

<id name="id">
<column name="id" sql-type="bigint" not-null="true"/>
<generator class="native" />
</id>

<bag name="LeafList" inverse="true" cascade="all">
<key column="pid"/>
<one-to-many class="Leaf"/>
</bag>

</class>
</hibernate-mapping>

Child class mapping
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="winapp" assembly="winapp">
<class name="Leaf" table="Table2">

<id name="id">
<column name="id" sql-type="bigint" not-null="true"/>
<generator class="native" />
</id>

<many-to-one name="pid" class="Root" column="pid"/>

</class>
</hibernate-mapping>

Both mapping files are embedded resources of the project.

Main code

Root r = new Root();

ISession session = null;
ITransaction tx = null;
try
{
session = NHibernateHelper.GetCurrentSession();
tx = session.BeginTransaction();

session.Save(r);

if (tx != null)
tx.Commit();

MessageBox.Show("Done");
}
catch (Exception ex)
{
if (tx != null)
tx.Rollback();

MessageBox.Show("Error:\n" + ex.Message);
}
NHibernateHelper.CloseSession();

So, the question is - when I started the program and it executes session.Save(r), I suppose it stores 1 record in parent table and 1 record in child table and Table2.pid will be equal to Table1.id.
But I only receive "Unknown entity class: System.Int64" exception message...

If I omit cascade="all" in parent class mapping, the record in the parent table appears, but no record appears in child table.

I tried to use searching, but I don't find any message about such an exception.

What I am doing wrong?

Thanks for your time wasted on me,
Dennis.


Top
 Profile  
 
 Post subject: Re: Parent-child relation and not-null foreign key
PostPosted: Fri Aug 03, 2007 9:12 am 
Newbie

Joined: Thu Aug 02, 2007 8:40 am
Posts: 3
And all what log4net said was:

DEBUG NHibernate.SQL - INSERT INTO Table1 DEFAULT VALUES; select SCOPE_IDENTITY()

No other messages were added in log file.

I took all the settings for log4net from src\NHibernate.Test\app.config as it was said in NHibernate's documentation. Maybe it is possible to switch on some exta log data?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 03, 2007 12:43 pm 
Hibernate Team
Hibernate Team

Joined: Tue Jun 13, 2006 11:29 pm
Posts: 315
Location: Calgary, Alberta, Canada
You Leaf class should look like this:
Code:
class Leaf
{
  private Int64 _id;
  public virtual Int64 id
  {
    get { return _id; }
    set { _id = value; }
  }

  private Root _pid;
  public virtual Root pid
  {
    get { return _pid; }
    set { _pid = value; }
  }
}

Note the type of pid.

_________________
Karl Chu


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 06, 2007 1:22 am 
Newbie

Joined: Thu Aug 02, 2007 8:40 am
Posts: 3
Karl, thanks a lot!
Dennis.


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