Why even have the folders.hbm.xml or folder.hbm.xml aware of each other...
if you create the file_folders table to have either a composite key or its own key..
e.g. psuedo code...
FilesFoldersJoin
int id
int fileId
int folderId
and in the database create a constraint so that both fileId and folderId must be a foreign key to the primary keys on those tables so you don't get an invalid record in the files/folders join table..
now your mapping is :
Files
Code:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="WindowsApplication1.Files, WindowsApplication1" table="`files`">
<id name="Id" column="fil_id">
<generator class="sequence">
<param name="sequence">files_fil_id_seq</param>
</generator>
</id>
<property name="Name" column="fil_name" />
<property name="Description" column="fil_description" />
</class>
</hibernate-mapping>
Folders:
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="WindowsApplication1.Folders, WindowsApplication1" table="`folders`">
<id name="Id" column="fol_id">
<generator class="sequence">
<param name="sequence">folders_fol_id_seq</param>
</generator>
</id>
<property name="Name" column="fol_name" />
<property name="Description" column="fol_description" />
</hibernate-mapping>
Code:
<?xml version="1.0" encoding="utf-8" ?>
New FileFolderJoin Object...
Code:
class FileFolderJoin
{
int id;;
Folder folder;
File file;
}
Files/Folder mapping for your new Files/Folder Object...
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="WindowsApplication1.FilesFoldersJoin, WindowsApplication1" table="`folders`">
<id name="Id" column="fol_id">
<generator class="sequence">
<param name="sequence">folders_fol_id_seq</param>
</generator>
</id>
<many-to-one name="File" class="WindowsApplication1.File" column="fileId"/>
<many-to-one name="Folder" class="WindowsApplication1.Folder" column="FolderId"/>
</hibernate-mapping>
Code:
<?xml version="1.0" encoding="utf-8" ?>
now you can do a query
select FileFolder as ff where ff.File.Id =1234;
So you'll get all fileFolders Object that contains both the file and the folder objects
you an also do
select FileFolder as ff where ff.Folder.Id = 12345
Hibernate in action is a good book, its written for the java version but it does give a lot of ideas on how to structure your objects. Many to many is not good and I find using join tables and objects gives me better performance and simpler mapping files.