Hello, first of all i beg your perdon if my english is not that well Then, i've been looking around about this problem and i've found lots of information but i've tried lots and lots of things yet and not solving this problem.
i will copy all my code:
The example page:
aspx: <%@ Page Title="" Language="C#" MasterPageFile="~/LayoutNew.Master" AutoEventWireup="true" CodeBehind="Prueba.aspx.cs" Inherits="OfficeMart.Prueba" %> <%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajaxToolkit" %> <%@ Register Assembly="DevExpress.Web.ASPxGridView.v10.1, Version=10.1.4.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" Namespace="DevExpress.Web.ASPxGridView" TagPrefix="dx" %> <%@ Register assembly="DevExpress.Web.ASPxEditors.v10.1, Version=10.1.4.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" namespace="DevExpress.Web.ASPxEditors" tagprefix="dx" %>
<asp:Content ID="Content1" ContentPlaceHolderID="cphContent" runat="server">
</asp:Content>
The cs
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using DomainLayer.Objects; using System.Text; using System.Configuration; using NHibernate; using System.Net.Mail; using OfficeMart.DataAccesLayer;
namespace OfficeMart { public partial class Prueba : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { ISession Sesion; ITransaction Transaction = null; Sesion = SessionManager.OpenSession(); Transaction = Sesion.BeginTransaction(); BO_User user = (BO_User)Sesion.Load(typeof(BO_User), Request["User"]); user.Categorys.Clear(); try { Sesion.SaveOrUpdate(user); Transaction.Commit(); } catch (Exception er) { Transaction.Rollback(); Response.Redirect("ErrorPage.aspx?Error=" + er.Message); } finally { Sesion.Close(); Sesion.Dispose(); } } } }
the cs for hibernate:
using System; using System.Collections.Generic; using System.Linq; using System.Text;
namespace DomainLayer.Objects { public class BO_User { public BO_User() { }
public BO_User(String UserName, String Password, String Name, String LastName, String Email, BO_Rol Rol, Boolean Enable, List<BO_CategoryUser> Categorys) { this.UserName = UserName; this.Password = Password; this.Name = Name; this.LastName = LastName; this.Email = Email; this.Rol = Rol; this.Enable = Enable; this.Categorys = Categorys; }
public String UserName { get; set; } public String Password { get; set; } public String Name { get; set; } public String LastName { get; set; } public String Email { get; set; } public BO_Rol Rol { get; set; } public Boolean Enable { get; set; } public BO_Area Area { get; set; } public BO_Company Company {get; set;} public IList<BO_CategoryUser> Categorys { set; get; }
public override string ToString() { return this.LastName + ", " + this.Name + " - " + this.Rol.Description; } } }
the xml of hibernate:
<?xml version="1.0" encoding="utf-8" ?> <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="DomainLayer" namespace="DomainLayer.Objects" default-lazy="false"> <class name="BO_User" table="USERS" lazy="false">
<id name="UserName" column="USERNAME" type="string" > <generator class="assigned" /> </id> <property name="Password" column="PASSWORD" type="string" not-null="true"/> <property name="Name" column="NAME" type="string" not-null="true"/> <property name="LastName" column="LASTNAME" type="string" not-null="true"/> <property name="Email" column="EMAIL" type="string" not-null="true"/> <many-to-one name="Rol" column="IDROL" class="BO_Rol" /> <property name="Enable" column="ENABLE" type="boolean" not-null="true"/> <many-to-one name="Company" column="IDCOMPANY" class="BO_Company" /> <many-to-one name="Area" column="IDAREA" class="BO_Area" /> <bag name="Categorys" cascade="all-delete-orphan"> <key column="USERNAME"/> <one-to-many class="BO_CategoryUser"/> </bag> </class> </hibernate-mapping>
The error message:
deleted object would be re-saved by cascade (remove deleted object from associations): 100, of class: DomainLayer.Objects.BO_CategoryUser
what i need to do is simply delete the relationship bettwen The user (BO_User) and The BO_CategoryUser.
I'm using this middle-table (CategoryUser) to connect Users and categories.
This is the code of the CategoryUser table:
using System; using System.Collections.Generic; using System.Linq; using System.Text;
namespace DomainLayer.Objects { public class BO_CategoryUser { public BO_CategoryUser() { } public BO_CategoryUser(Int32 IdCategoryUser, BO_Category Category, BO_User User) { this.IdCategoryUser = IdCategoryUser; this.Category = Category; this.User = User; } public Int32 IdCategoryUser { get; set; } public BO_Category Category { get; set; } public BO_User User { get; set; } } }
And the xml
<?xml version="1.0" encoding="utf-8" ?> <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="DomainLayer" namespace="DomainLayer.Objects" default-lazy="false"> <class name="BO_CategoryUser" table="CATEGORY_USER" lazy="false">
<id name="IdCategoryUser" column ="IDCATEGORYUSER" type="int" unsaved-value="0"> <generator class="identity"/> </id> <many-to-one name="Category" column="IDCATEGORY" class="BO_Category" /> <many-to-one name="User" column="USERNAME" class="BO_User" /> </class> </hibernate-mapping>
thanks in advance
|