Hello everyone!
I am building a small application and I have encountered a problem that I cannot overcome.
Lets see some SQL:
Code:
CREATE TABLE [dbo].[Categories](
[Id] [bigint] IDENTITY(1,1) NOT NULL,
[Name] [varchar](50) COLLATE Polish_CI_AS NOT NULL,
[Description] [varchar](255) COLLATE Polish_CI_AS NULL,
CONSTRAINT [PK_Categories] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
CREATE TABLE [dbo].[CategoriesTree](
[CategoryId] [bigint] NOT NULL,
[ParentCategoryId] [bigint] NOT NULL,
CONSTRAINT [PK_CategoriesTree] PRIMARY KEY CLUSTERED
(
[CategoryId] ASC,
[ParentCategoryId] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
ALTER TABLE [dbo].[CategoriesTree] WITH CHECK ADD CONSTRAINT [FK_CategoriesTree_Categories] FOREIGN KEY([CategoryId])
REFERENCES [dbo].[Categories] ([Id])
GO
ALTER TABLE [dbo].[CategoriesTree] WITH CHECK ADD CONSTRAINT [FK_CategoriesTree_Categories1] FOREIGN KEY([ParentCategoryId])
REFERENCES [dbo].[Categories] ([Id])
CREATE TABLE [dbo].[Products](
[Id] [bigint] IDENTITY(1,1) NOT NULL,
[ProducerId] [bigint] NOT NULL,
[CategoryId] [bigint] NOT NULL,
[Name] [varchar](50) COLLATE Polish_CI_AS NOT NULL,
[Description] [text] COLLATE Polish_CI_AS NULL,
[Price] [decimal](9, 2) NOT NULL,
[Amount] [int] NOT NULL,
[FromDate] [smalldatetime] NOT NULL,
CONSTRAINT [PK_Products] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
ALTER TABLE [dbo].[Products] WITH CHECK ADD CONSTRAINT [FK_Products_Categories] FOREIGN KEY([CategoryId])
REFERENCES [dbo].[Categories] ([Id])
GO
ALTER TABLE [dbo].[Products] WITH CHECK ADD CONSTRAINT [FK_Products_Producers] FOREIGN KEY([ProducerId])
REFERENCES [dbo].[Producers] ([Id])
So, we have a categories tree (every category can have one parent and every category can have several children). Also every product has its category and a category has a list of products.
I created the following mapping:
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0" assembly="NCommercePersistence" namespace="NCommerce.Persistence">
<class name="Category" table="CategoriesV">
<id name="Id" column="Id" type="Int64">
<generator class="identity"/>
</id>
<property name="Name" column="Name" type="String" length="50"/>
<property name="Description" column="Description" type="String"/>
<set name="Products" table="Products">
<key column="CategoryId"/>
<one-to-many class="Product"/>
</set>
<set name="ChildrenCategories" table="CategoriesTree">
<key column="ParentCategoryId"/>
<many-to-many column="CategoryId" class="Category"/>
</set>
<many-to-one name="ParentCategory" column="ParentCategoryId" class="Category"/>
</class>
</hibernate-mapping>
CategoriesV is a view that returns a list of Id, Name, Description and ParentCategoryId. With that mapping file I am able to get data from the database without problems, but unfortunately I am unable to insert/update/delete because of the view (there is a JOIN inside). I tried to create an INSTEAD OF INSERT trigger for that view, but there is something wrong with my mapping and I am still unable to insert new records. I am sure that I did something wrong. And the worst thing is that I do not know what to change...
I can redesign the database schema, I can change classes and mapping file. I just need to get it working. Could any of you, please, help me with that? I spent long hours trying to correct it, but my experience with NHibernate is not enough to do it.
Well, I am not sure, but something tells me that this kind of hierarchical relation is unable to be mapped, when both ends of association are related to one class - Category. Could anyone, please, confirm or deny that? If it is true...what is the best way to solve this problem?
I would really apprieciate any help - all hints are welcome.
Hibernate version: 1.02
Name and version of the database you are using: MS SQL 2005 Developer
Thanks in advance.
Regards,
snake_net