Hibernate version: 1.2.0 GA
Database: Microsoft SQL Server 2000
Description:
I have two classes, File and Category simple as that. The File can have several categories and the category several files. Here's the deal. When I save a file to the database a want to assign it a category/categories as well. If I add a file manually to the database and assigning its categories I can perfectly get what categories a file has, and vice versa. Nothing wrong with that. My problem is that when I try to upload a file whit NHibernate and try to assign that file to a category it does not work, it only adds the file and that's it!
It will become more clear once the mapping is displayed.
Note! It's only small portions of the classes.
File class:
Code:
public class File
{
private string _fileName;
//etc...
private IList<Category> _fileCategorys = new List<Category>();
//and then the get, set for all the members.
}
File mapping:Code:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Acme" namespace="Acme.Core.Domain">
<class name="File" table="Files" lazy="true">
<id name="ID" column="pkFileGuid">
<generator class="guid.comb" />
</id>
<property name="Name" column="FileName" type="String(50)" unique="true" />
etc...
<bag name="Categories" table="FileCategories" inverse="true" lazy="true">
<key column="fkFileGuid" />
<many-to-many class="Category" column="fkCategoryID" />
</bag>
</class>
Category class:Code:
public class Category
{
private string _fileName;
//etc...
private IList<Category> _fileCategories = new List<Category>();
//and then the get, set for all the members.
}
Category mapping:Code:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Acme" namespace="Acme.Core.Domain">
<class name="Category" table="Categories" lazy="true">
<id name="ID" column="pkCategoryID" type="Int32">
<generator class="native" />
</id>
<property name="Name" column="CategoryName" type="String(50)" unique="true" />
<bag name="Files" table="FileCategories" inverse="true" lazy="true">
<key column="fkCategoryID" />
<many-to-many class="File" column="fkFileGuid" />
</bag>
</class>
"Database Diagrams"Files:Name: Type:
-------------------------------
pkFileGuid uniqueidentifier
FileName nvarchar(50)
FileCategories:Name: Type:
-------------------------------
fkFileGuid uniqueidentifier
fkCategoryID int
Categories:Name: Type:
-------------------------------
pkCategoryID int
CategoryName
ExampleCode:
File file = new File();
file.Name = "Test";
// Trying with the categories ID.
Category category = new Category(1);
// Even tried with the name of the category.
file.Categories.Add("Foo");
// Finally, save it.
FileDao.SaveOrUpdate(file);
I don't really know if this is the correct way but I thought that if NHibernate can get all the categories that a file has and vice versa it mind as well insert them automatically. And again, this does not generate any errors, but it does not insert the category assigned to the file in the database. I know that NHibernate can do this, I'm sure that there something with my mapping, or?
Oh yeah, one more thing... :) Anyone have a good idea on how to handle file duplicates, I do not want users to be able to upload the same file does this have to be handled manually by a stored procedure or such?