-->
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: Unable to move domain object between sessions
PostPosted: Thu May 04, 2006 4:18 am 
Newbie

Joined: Thu May 04, 2006 3:58 am
Posts: 4
Hi,

I am trying to copy data between two identical databases, and keep getting a
could not delete collection: [Domain.User.Roles#<null>]
This appears to be problem with the identity columns, as if I remove them from the target database and change my id generator class attribute to read assigned everthing works. Please could someone tell me what I am doing wrong.

thanks

Jim


I have two databases with the following tables

CREATE TABLE [dbo].[users] (
[u_id] [int] IDENTITY (1, 1) NOT NULL ,
[username] [varchar] (50) NOT NULL
) ON [PRIMARY]


CREATE TABLE [dbo].[userroles] (
[u_id] [int] NOT NULL ,
[r_id] [int] NOT NULL
) ON [PRIMARY]
GO

CREATE TABLE [dbo].[roles] (
[r_id] [int] IDENTITY (1, 1) NOT NULL ,
[rolename] [varchar] (50) NOT NULL
) ON [PRIMARY]
GO

Mapping documents:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="Domain.User, Domain" table="users">

<id name="Id" column="u_id" type="Int32" unsaved-value="-1">
<generator class="identity" />
</id>

<property name="UserName" column="username" type="String" length="50" />

<bag name="Roles" table="userroles" cascade="all" lazy="false">

<key column="u_id" />
<many-to-many class="Domain.Role, Domain" column="r_id" outer-join="true" />
</bag>

</class>
</hibernate-mapping>

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="Domain.Role, Domain" table="roles" >

<id name="Id" column="r_id" type="Int32" unsaved-value="-1">
<generator class="identity" />
</id>

<property name="Name" column="rolename" type="String" length="50" />
</class>
</hibernate-mapping>

Here are my domain objects

public class Role
{
private int _id;
private string _name;

/// <summary>
///
/// </summary>
public Role()
{
this._id = -1;
this._name = null;
}

/// <summary>
///
/// </summary>
public virtual int Id
{
get { return this._id; }
set { this._id = value; }
}

/// <summary>
///
/// </summary>
public virtual string Name
{
get { return this._name; }
set { this._name = value; }
}
}

public class User
{
private int _id;
private string _userName;

private IList _roles;

/// <summary>
///
/// </summary>
public User()
{
this._id = -1;
this._roles = new ArrayList();
}

#region properties
/// <summary>
/// Property Id (int)
/// </summary>
public virtual int Id
{
get { return this._id; }
set { this._id = value; }
}

/// <summary>
/// Property Name (string)
/// </summary>
public virtual string UserName
{
get { return this._userName; }
set { this._userName = value; }
}

/// <summary>
///
/// </summary>
public virtual IList Roles
{
get { return this._roles; }
set { this._roles = value; }
}

#endregion

}

Code between sessionFactory.openSession() and session.close():

NHibernate.ISessionFactory sourceSessionFact = BuildFactory("SourceDB");
NHibernate.ISessionFactory destSessionFact = BuildFactory("TargetDB");

NHibernate.ISession sourceSession = sourceSessionFact.OpenSession();
NHibernate.ISession destSession = destSessionFact.OpenSession();

NHibernate.ITransaction tx = sourceSession.BeginTransaction();
Domain.User user = (Domain.User)sourceSession.Get(typeof(Domain.User), 1);
tx.Commit();
sourceSession.Close();

tx = destSession.BeginTransaction();
destSession.Replicate(user, NHibernate.ReplicationMode.Overwrite);
tx.Commit();
destSession.Close();
sourceSessionFact.Close();
destSessionFact.Close();

Full stack trace of any exception that occurs:

" at NHibernate.Collection.AbstractCollectionPersister.Remove(Object id, ISessionImplementor session)\r\n at NHibernate.Impl.ScheduledCollectionRemove.Execute()\r\n at NHibernate.Impl.SessionImpl.Execute(IExecutable executable)\r\n at NHibernate.Impl.SessionImpl.ExecuteAll(IList list)\r\n at NHibernate.Impl.SessionImpl.Execute()\r\n at NHibernate.Impl.SessionImpl.Flush()\r\n at NHibernate.Transaction.AdoTransaction.Commit()\r\n at WindowsApplication1.Form1.button1_Click(Object sender, EventArgs e) in I:\\testarea\\WindowsApplication1\\Form1.cs:line 33\r\n at System.Windows.Forms.Control.OnClick(EventArgs e)\r\n at System.Windows.Forms.Button.OnClick(EventArgs e)\r\n at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)\r\n at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)\r\n at System.Windows.Forms.Control.WndProc(Message& m)\r\n at System.Windows.Forms.ButtonBase.WndProc(Message& m)\r\n at System.Windows.Forms.Button.WndProc(Message& m)\r\n at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)\r\n at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)\r\n at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)\r\n at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)\r\n at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)\r\n at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)\r\n at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)\r\n at System.Windows.Forms.Application.Run(Form mainForm)\r\n at WindowsApplication1.Program.Main() in I:\\testarea\\WindowsApplication1\\Program.cs:line 19\r\n at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)\r\n at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)\r\n at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()\r\n at System.Threading.ThreadHelper.ThreadStart_Context(Object state)\r\n at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)\r\n at System.Threading.ThreadHelper.ThreadStart()"


Hibernate version:
1.0.2.0

Name and version of the database you are using:
SQL Server 2000 sp4


Top
 Profile  
 
 Post subject: SaveOrUpdateCopy seems to work
PostPosted: Thu May 04, 2006 5:38 am 
Newbie

Joined: Thu May 04, 2006 3:58 am
Posts: 4
I think I have finally cracked it. SaveOrUpdateCopy appears to work.
Thanks


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.