1

I am having problem and the time spend beeing stuck tells me, to ask stackOverflow community. So: I have basic versioning system for files represented as abstract class FileSystemEntry (FSE) which has 2 childs - File and Folder. There is also FileSystemEntryVersion (FSEV). FSE has many FSEV.

public abstract class FileSystemEntry
{
    public FileSystemEntry(){}

    public virtual long Id { get; protected set; }

    public virtual IList<FileSystemEntryVersion> Versions { get; set; }
}

and here is the FSEV:

public class FileSystemEntryVersion {
    public FileSystemEntryVersion(){}

    public virtual long Id { get; protected set; }

    public virtual String Name { get; set; }

    //pointer to physical file/folder
    public virtual FileSystemEntry FileSystemEntry { get; set; }

    //pointer to parent file/folder
    public virtual FileSystemEntry ParentFolder { get; set; }
}

Here is the hbm.xml file for FSE (the most important part)

<class name="FileSystemEntry" table="[FileSystemEntry]" abstract="true">
<id name="Id" type="Int64">
  <generator class="identity" />
</id>

<discriminator column="Type"
               not-null="true"
               type="String"/>
<subclass name="File"
          discriminator-value="File"/>

<subclass name="Folder"
          discriminator-value="Folder"/>
</class>

Every FSEV (has parent folder). I want to make a query, which gets only folders contained in parent folder. My current query is:

 IList<FileSystemEntryVersion> versions =
                session.CreateCriteria<FileSystemEntryVersion>()
                       .Add(Restrictions.Eq("ParentFolder.Id", parentFolderId))
                       .Add(Restrictions.Eq("FileSystemEntry.class", typeof(Folder)))))
                       .List<FileSystemEntryVersion>();

            return versions;

But the property "class" in FileSystemEntry.class is said it couldn't be resolved. Why?

Milanec
  • 377
  • 1
  • 3
  • 7
  • Have you tried this: http://stackoverflow.com/questions/17283070/nhibernate-query-by-discriminator-on-association – Najera Dec 17 '13 at 18:44
  • @Najera Yes, I did. But got same exception, until I tried to make alias this way: CreateAliast("FileSystemEntry", "FileSystemEntry")... but I wasn´t sure at all why does it work suddenly :) Radim Kohler set it clear, but I swear, I tried it and it didnt work yesterday:D Now it does.. – Milanec Dec 18 '13 at 12:52

1 Answers1

1

You are almost there:

var versions = session
     .CreateCriteria<FileSystemEntryVersion>()
     .Add(Restrictions.Eq("ParentFolder.Id", parentFolderId))

     // we do need a reference here, JOIN in fact, 
     // to include the Many-to-One table          
     .CreateAlias("FileSystemEntry ", "Entry")

     // now the SELECT will contain even the FileSystemEntry table
     // and can start evaluate
     .Add(Restrictions.Eq("Entry.class", typeof(Folder)))))
     .List<FileSystemEntryVersion>();
Radim Köhler
  • 122,561
  • 47
  • 239
  • 335
  • Thank you very much! That's it :) But as I said to Najera, I thought I tried it and it didn't work. Now it does. Greetings from Prague to Prague :) – Milanec Dec 18 '13 at 12:58
  • Cool if that helped ;) Good luck with NHibernate. It is amazing tool ;) – Radim Köhler Dec 18 '13 at 13:03