Hibernate version: 1.2.0
Database: SQL Server 2005
I'm attempting to build out a business layer for a legacy application with a database running on SQL Server 2005. Unfortunately I cannot change any of the broken Database design concepts mentioned below. I just want to know if what I'm trying to do is possible?
I have a M:M relationship between 2 tables, and an associated table between them, defined like so.
Group
ID (GUID) PK
GroupID (char)
User
Login (char) PK
Name (char)
GroupMembers
ID (GUID) PK
GroupRef (GUID) FK
Login (char) FK
I'm trying to build a mapper file to map this into the following object representation in C#
Group
ID
GroupID
UserMemberships
User
Login
Name
GroupMemberships
The Mapper files look like this:
Group Mapper File:
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="Appframe.Business.System.Group, Appframe.Business" table="stbv_System_Groups">
<id name="ID" column="ID" type="System.Guid" unsaved-value="00000000-0000-0000-0000-000000000000">
<generator class="guid" />
</id>
<property name="GroupID" column="GroupID" not-null="true" />
<bag name="UserMemberships" table="stbv_System_GroupsMembers" cascade="none" lazy="true">
<key column="GroupRef" />
<many-to-many column="Login" class="Appframe.Business.System.User, Appframe.Business" />
</bag>
</class>
</hibernate-mapping>
User Mapper File:Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="Appframe.Business.System.User, Appframe.Business" table="stbv_System_Users">
<id name="Login" column="Login" type="string" unsaved-value="">
<generator class="assigned" />
</id>
<property name="Name" column="Name" not-null="true" />
<bag name="GroupMemberships" table="stbv_System_GroupsMembers" cascade="none" lazy="true">
<key column="Login" />
<many-to-many column="GroupRef" class="Appframe.Business.System.Group, Appframe.Business" />
</bag>
</class>
</hibernate-mapping>
The declaration of the UserMemberships property is like so:
Code:
private IList<User> userMemberships = new List<User>();
public virtual IList<User> UserMemberships
{
get { return new List<User>(userMemberships).AsReadOnly(); }
protected set { userMemberships = value; }
}
I'm following the example by Billy McCafferty posted on the Code Project as the bases for my business layer. So session handling is tied to the HTTP context etc...
I'm getting an error when I'm trying to bind my generic list of Groups to a datagrid like so:
Code:
// Get a reference to the Data Access Object(DAO) factory
ISystemDao systemDao = new SystemDao();
// Using the DAO factory, get an instance to a new DAO for the Group object
IGroupsDao groupsDao = systemDao.GetGroupsDao();
// Use the Group DAO to call the database and retreive a list of Groups
grdGroups.DataSource = groupsDao.GetAll();
grdGroups.DataBind();
The above code produces the following error:
"Cannot insert duplicate key row in object 'dbo.stbl_System_GroupsMembers' with unique index 'IX_stbl_System_GroupsMembers'.
The statement has been terminated."
Is is possible the PrimaryKey column (ID) in the GroupMembers table is causing the issue?
Can anyone help me? Have I done something wrong, or do I need to structure my objects like they are in the database, with an association object between them?
Thanks in advance!