I am struggling with NHibernate. I have a little ASP MVC website. I managed to setup NHibernate but when I want to retrieve data I get this error:
Additional information: could not initialize a collection: [Lore.Models.DatabaseModels.Statue.Keywords#1] [SQL: SELECT keywords0_.IdStatueKeyword as IdStatue1_1_, keywords0_.IdKeyword as IdKeyword1_, keyword1_.IdKeyword as IdKeyword4_0_, keyword1_.Name as Name4_0_, keyword1_.Description as Descript8_4_0_ FROM StatueKeyword keywords0_ left outer join Statue keyword1_ on keywords0_.IdKeyword=keyword1_.IdKeyword WHERE keywords0_.IdStatueKeyword=?]
Also I am not sure if I implemented the many-to-many relationship right. This is my table structure:
Statue
- IdStatue
- Name
Keyword
- IdKeyword
- Name
StatueKeyword
- IdStatueKeyword
- IdStatue
- IdKeyword
Statue Class:
public class Statue
{
public Statue()
{
Keywords = new List<Keyword>();
}
public int IdStatue { get; set; }
public string Name { get; set; }
public IList<Keyword> Keywords { get; set; }
}
Keyword Class
public class Keyword
{
public int IdKeyword { get; set; }
public string Name { get; set; }
}
Statue hbm file:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="Lore.Models.DatabaseModels" assembly="Lore">
<class name="Statue" table="Statue" lazy="false" >
<id name="IdStatue" column="IdStatue">
<generator class="identity" />
</id>
<property name="Name" column="Name" not-null="true" type="System.String" />
<bag name="Keywords" table="StatueKeyword" lazy="false">
<key column="IdStatueKeyword"/>
<many-to-many class="Keyword" column="IdKeyword"/>
</bag>
</class>
</hibernate-mapping>
Keyword hbm file
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="Lore.Models.DatabaseModels" assembly="Lore">
<class name="Keyword" table="Statue" lazy="false" >
<id name="IdKeyword" column="IdKeyword">
<generator class="identity" />
</id>
<property name="Name" column="Name" not-null="true" type="System.String" />
<property name="Description" column="Description" not-null="true" type="System.String" />
</class>
</hibernate-mapping>
I need to make this website for a master thesis so any help is much appreciated!