Hi All Users,
Am stuck in a situation in which I am updating a PArent Child Relationship. When I am updating and adding more children to the Parent's Child Collection it is giving me an exception , which I am not able to resolve .
Note: I am not using lazy=true , as I am passing through different tiers , so lazy =false in all possible situations Here is my PArent HBM and Entity:
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-lazy="false">
<class name="TP.CoE.Products.Entity.SystemAdministration.ScreenEntity, TP.CoE.Products.Entity.SystemAdministration" table="SA_SCREEN" lazy="false" >
<id name="ScreenID" column="SCREEN_ID" type="System.Decimal" unsaved-value="-1">
<generator class="sequence">
<param name="sequence">SA_SCREEN_SEQ</param>
</generator>
</id>
<property name="ScreenName" column="SCREEN_NAME" not-null="false" type="System.String" />
<property name="ScreenDesc" column="SCREEN_DESC" not-null="false" type="System.String" />
<property name="Active" column="ACTIVE" not-null="false" type="System.String" />
<property name="PageName" column="PAGE_NAME" not-null="false" type="System.String" />
<property name="ParentID" column="PARENT_ID" not-null="false" type="System.Decimal" />
<property name="ShowLink" column="SHOWLINK" not-null="false" type="System.Decimal" />
<property name="ScreenOrd" column="SCREENS_ORD" not-null="false" type="System.Decimal" />
<property name="CreatedBy" column="CREATED_BY" not-null="false" type="System.Decimal" />
<property name="UpdatedBy" column="UPDATED_BY" not-null="false" type="System.Decimal" />
<property name="CreatedDate" column="CREATED_DATE" not-null="false" type="System.DateTime" />
<property name="UpdatedDate" column="UPDATED_DATE" not-null="false" type="System.DateTime" />
<!--<property name="TotalRecord" formula="count(*) over()" type="Int32" not-null="false"/>-->
<bag name="ScreenFields" table="SA_SCREEN_FIELD" cascade="all" inverse="true" generic="true" >
<key column="SCREEN_ID" />
<one-to-many class="TP.CoE.Products.Entity.SystemAdministration.ScreenFieldEntity,TP.CoE.Products.Entity.SystemAdministration" />
</bag>
</class>
</hibernate-mapping>
Code:
using System;
using System.ComponentModel;
using System.Collections.Generic;
using Iesi.Collections;
using Iesi.Collections.Generic;
using TP.CoE.Products.Entity.SystemAdministration;
using System.Runtime.Serialization;
namespace TP.CoE.Products.Entity.SystemAdministration
{
/// <summary>
/// An object representation of the Screen table
/// </summary>
[DataContract]
public class ScreenEntity : AuditBaseEntity//TP.CoE.Products.Entity.SystemAdministration.Entity<ScreenEntity>
{
[DataMember]
public virtual Decimal ScreenID { get; set; }
[DataMember]
public virtual String ScreenName { get; set; }
[DataMember]
public virtual String ScreenDesc { get; set; }
[DataMember]
public virtual String Active { get; set; }
[DataMember]
public virtual String PageName { get; set; }
[DataMember]
public virtual Decimal? ParentID { get; set; }
[DataMember]
public virtual Decimal? ShowLink { get; set; }
[DataMember]
public virtual Decimal? ScreenOrd { get; set; }
[DataMember]
public virtual IList<UserRoleEntity> Roles { get; set; }
[DataMember]
public virtual IList<ScreenFieldEntity> ScreenFields { get; set; }
[DataMember]
public virtual int? TotalRecord { get; set; }
}
}
The Child HBM and Entity:
Code:
<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping assembly="TP.CoE.Products.Entity.SystemAdministration" namespace="TP.CoE.Products.Entity.SystemAdministration" xmlns="urn:nhibernate-mapping-2.2" default-lazy="false">
<class name="TP.CoE.Products.Entity.SystemAdministration.ScreenFieldEntity, TP.CoE.Products.Entity.SystemAdministration" table="SA_SCREEN_FIELD" lazy="false" >
<id name="FieldId" column="FIELD_ID" type="System.Decimal" unsaved-value="-1">
<generator class="sequence">
<param name="sequence">SA_SCREEN_FIELD_SEQ</param>
</generator>
</id>
<property name="ActualName" column="ACTUAL_NAME" not-null="false" type="System.String"/>
<property name="UserFriendlyName" column="USER_FRIENDLY_NAME" not-null="false" type="System.String" />
<property name="TypeID" column="IS_ACTION_FIELD" not-null="false" type="System.Decimal" />
<property name="IsDeleted" column="IS_DELETED" not-null="false" type="System.Decimal" />
<property name="CreatedBy" column="CREATED_BY" not-null="false" type="System.Decimal"/>
<property name="CreatedDate" column="CREATED_DATE" not-null="false" type="System.DateTime"/>
<property name="UpdatedBy" column="UPDATED_BY" not-null="false" type="System.Decimal"/>
<property name="UpdatedDate" column="UPDATED_DATE" not-null="false" type="System.DateTime"/>
<!--<property name="TotalRecord" formula="count(*) over()" type="Int32" not-null="false"/>-->
<!--<property name="ScreenID" column="SCREEN_ID" not-null="false" type="System.Decimal"/>-->
<many-to-one name="Screen" column="SCREEN_ID" class="TP.CoE.Products.Entity.SystemAdministration.ScreenEntity,TP.CoE.Products.Entity.SystemAdministration" >
</many-to-one>
</class>
</hibernate-mapping>
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
namespace TP.CoE.Products.Entity.SystemAdministration
{
[DataContract]
public class ScreenFieldEntity : AuditBaseEntity
{
[DataMember]
public decimal? ScreenFieldID { get; set; }
[DataMember]
public virtual decimal? FieldId { get; set; }
//[DataMember]
//public virtual decimal? ScreenID { get; set; }
[DataMember]
public virtual string ActualName { get; set; }
[DataMember]
public virtual string UserFriendlyName { get; set; }
[DataMember]
public virtual decimal? TypeID { get; set; }
[DataMember]
public virtual string TypeDescription { get; set; }
[DataMember]
public virtual decimal? IsDeleted { get; set; }
[DataMember]
public virtual ScreenEntity Screen { get; set; }
[DataMember]
public virtual int? TotalRecord { get; set; }
//[DataMember]
//public virtual Decimal? UpdatedBy { get; set; }
//[DataMember]
//public virtual DateTime? CreatedDate { get; set; }
//[DataMember]
//public virtual DateTime? UpdatedDate { get; set; }
public ScreenFieldEntity()
{
this.ScreenFieldID = Convert.ToDecimal(Guid.NewGuid().GetHashCode());
}
}
}
The Code in the Data layer that is calling update to the main parent with all its childs.
Code:
public void SaveScreenDetail(ScreenEntity screenEntity)
{
using (ISession session = SessionManager.GetSession())
{
using (ITransaction tx = session.BeginTransaction())
{
try
{
screenEntity.ScreenFields= screenEntity.ScreenFields.ToList<ScreenFieldEntity>();
session.SaveOrUpdateCopy(screenEntity);
tx.Commit();
}
catch (NHibernate.HibernateException)
{
tx.Rollback();
throw;
}
}
}
}
I have one question as well , Is it possible to define a property in the HBM file and in the entity that is not present in the database ? I have defined such a property, called TotalRecord , is it due to this property ?Selection id going fine although.
I have debugged it thoroughly ,this is the exception that I get:
could not update: [TP.CoE.Products.Entity.SystemAdministration.ScreenEntity#3][SQL: UPDATE SA_SCREEN SET SCREEN_NAME = ?, SCREEN_DESC = ?, ACTIVE = ?, PAGE_NAME = ?, PARENT_ID = ?, SHOWLINK = ?, SCREENS_ORD = ?, CREATED_BY = ?, UPDATED_BY = ?, CREATED_DATE = ?, UPDATED_DATE = ? WHERE SCREEN_ID = ?]
Its making issues when updating the parent Entity ...Help Required , please...