-->
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.  [ 2 posts ] 
Author Message
 Post subject: about cascade insert,help me!
PostPosted: Fri May 20, 2005 8:25 pm 
Newbie

Joined: Fri May 20, 2005 8:23 pm
Posts: 3
table:
*************************************************
create table [dbo].[child] (
[cid] [varchar] (36) [pid] [varchar] (36) null ,
[childname] [varchar] (50) null
)
create table [dbo].[parent] (
[pid] [varchar] (36) not null ,
[parentname] [varchar] (50) null
) on [primary]
go

alter table [dbo].[child] add
constraint [pk_child] primary key clustered
(
[cid]
) on [primary]
go

hbm.xml
*************************************************
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="Business.Data.Parent,Business.Data" table="Parent">
<id name="Pid" column="PID" type="String">
<generator class="uuid.hex" />
</id>
<property column="ParentName" type="String" name="ParentName" length="50" />
<bag name="Children" inverse="true" lazy="true" cascade="all">
<key column="PID" />
<one-to-many class="Business.Data.Child, Business.Data" />
</bag>
</class>
</hibernate-mapping>


<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="Business.Data.Child,Business.Data" table="Child"
dynamic-insert="true"
dynamic-update="true">

<id name="Cid" column="CID" type="String">
<generator class="uuid.hex"/>
</id>

<many-to-one name="Pid" column="PID" class="Business.Data.Parent,Business.Data" />
<property column="ChildName" type="String" name="ChildName" length="50" />

</class>
</hibernate-mapping>


test code
*************************************************
[Test]
public void testPC()
{
Configuration cfg = new Configuration();
cfg.AddAssembly("Business.Data");

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

// *********** Create Object *********************
Parent p = new Parent();
p.ParentName = "Parent";


Child c = new Child();
c.ChildName = "Child";

c.Pid = p;
p.Children.Add(c);

session.Save(p);

// ***********************************************
transaction.Commit();
session.Close();
}

log.txt
*************************************************
I want to get two insert statements:
insert parent...
insert child...

but, I get the follow statements:
insert parent...
update child...

why? buy maybe?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 17, 2005 3:24 pm 
Expert
Expert

Joined: Fri May 13, 2005 11:13 am
Posts: 292
Location: Rochester, NY
I found this old one while searching for something else. Hopefully you figured out the problem already: that the cascade requires that you set an "unsaved-value" on your child class, otherwise the underlying call to SaveOrUpdate() can't figure out whether to save or update.


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