Hi
We are using NHibernate 1.21.4 and we are getting the following exception when trying to save an object which has a ManyToOne relationship.
object references an unsaved transient instance - save the transient instance before flushing
We get a list of DTO objects from a Web Service and try to Insert/Update them. I have similar thing working for other objects, don't know why I am getting that error in this case.
That's the method which throws the Exception:
Code:
[WebMethod(EnableSession = true)]
public List<int> SetFleetAdmins(System.Collections.Generic.List<FleetAdminDTO> collFleetAdmins)
{
_objLogger.Debug("Setting Fleet Admins");
SuperAdminDTO objSuperAdminDTO = Session["SuperAdmin"] as SuperAdminDTO;
if (objSuperAdminDTO == null)
throw new Exception("Not logged on");
ISession objSession = Session["NHibernateSession"] as ISession;
ITransaction objTransaction = objSession.BeginTransaction();
List<int> collIds = new List<int>();
try
{
// CHECK: How to make sure that only the right items are updated ?
foreach (FleetAdminDTO f in collFleetAdmins)
{
_objLogger.Debug("Setting Fleet Admin...");
FleetAdmin objFleetAdmin = objSession.Get<FleetAdmin>(f.Id);
if (objFleetAdmin == null)
{
_objLogger.Debug("object not found");
objFleetAdmin = new FleetAdmin();
}
else
{
_objLogger.Debug("object found:" + f.Id);
}
objFleetAdmin.Fill(f);
objSession.SaveOrUpdate(objFleetAdmin);
_objLogger.Debug("After saving:" + objFleetAdmin.Id);
collIds.Add(objFleetAdmin.Id);
}
_objLogger.Debug("Committing...");
objTransaction.Commit();
_objLogger.Debug("Done...");
}
catch (Exception ex)
{
objTransaction.Rollback();
throw ex;
}
_objLogger.Debug(collFleetAdmins.Count + " Fleets Admin set");
return collIds;
}
The internal LOG is that:
Code:
2007-12-12 15:38:20,187 [3548 ] DEBUG ((null) ) ((null)) Setting Fleet Admins 3
2007-12-12 15:38:20,203 [3548 ] DEBUG ((null) ) ((null)) Setting Fleet Admin...
2007-12-12 15:38:20,203 [3548 ] DEBUG ((null) ) ((null)) object not found
2007-12-12 15:38:20,218 [3548 ] DEBUG ((null) ) ((null)) After saving:-1
2007-12-12 15:38:20,218 [3548 ] DEBUG ((null) ) ((null)) Setting Fleet Admin...
2007-12-12 15:38:20,218 [3548 ] DEBUG ((null) ) ((null)) object found:4
2007-12-12 15:38:20,234 [3548 ] DEBUG ((null) ) ((null)) After saving:4
2007-12-12 15:38:20,234 [3548 ] DEBUG ((null) ) ((null)) Setting Fleet Admin...
2007-12-12 15:38:20,234 [3548 ] DEBUG ((null) ) ((null)) object found:5
2007-12-12 15:38:20,234 [3548 ] DEBUG ((null) ) ((null)) After saving:5
2007-12-12 15:38:20,234 [3548 ] DEBUG ((null) ) ((null)) Committing...
The objects involved (quite simple) are:
Code:
[NHibernate.Mapping.Attributes.Class(Table = "fleetAdminUsers")]
public class FleetAdmin
{
private int _iId;
private string _strLogon;
private string _strPassword;
private bool _bDeleted;
private Fleet _objFleet = new Fleet();
[NHibernate.Mapping.Attributes.Id(Name = "Id")]
[NHibernate.Mapping.Attributes.Generator(1, Class = "native")]
public virtual int Id
{
get
{
return _iId;
}
set
{
_iId = value;
}
}
[NHibernate.Mapping.Attributes.Property(Column = "logon")]
public virtual string Logon
{
get
{
return _strLogon;
}
set
{
_strLogon = value;
}
}
[NHibernate.Mapping.Attributes.Property(Column = "password")]
public virtual string Password
{
get
{
return _strPassword;
}
set
{
_strPassword = value;
}
}
[NHibernate.Mapping.Attributes.Property(Column = "deleted")]
public virtual bool Deleted
{
get
{
return _bDeleted;
}
set
{
_bDeleted = value;
}
}
[NHibernate.Mapping.Attributes.ManyToOne(Column = "fleetId")]
public virtual Fleet Fleet
{
get
{
return _objFleet;
}
set
{
_objFleet = value;
}
}
public virtual void Fill(FleetAdminDTO objFleetAdminDTO)
{
this.Id = objFleetAdminDTO.Id;
this.Logon = objFleetAdminDTO.Logon;
this.Password = objFleetAdminDTO.Password;
this.Deleted = objFleetAdminDTO.Deleted;
this.Fleet.Fill(objFleetAdminDTO.Fleet);
}
Code:
[NHibernate.Mapping.Attributes.Class(Table = "fleets")]
public class Fleet
{
private int _iId;
private string _strName;
private bool _bDeleted;
[NHibernate.Mapping.Attributes.Id(Name = "Id")]
[NHibernate.Mapping.Attributes.Generator(1, Class = "native")]
public virtual int Id
{
get
{
return _iId;
}
set
{
_iId = value;
}
}
[NHibernate.Mapping.Attributes.Property(Column = "name")]
public virtual string Name
{
get
{
return _strName;
}
set
{
_strName = value;
}
}
[NHibernate.Mapping.Attributes.Property(Column = "deleted")]
public virtual bool Deleted
{
get
{
return _bDeleted;
}
set
{
_bDeleted = value;
}
}
public virtual void Fill(FleetDTO objFleetDTO)
{
this.Id = objFleetDTO.Id;
this.Name = objFleetDTO.Name;
this.Deleted = objFleetDTO.Deleted;
}
}
Please, can anyone help?
Thanks