Domanda

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!

È stato utile?

Soluzione

The issue should/could be in the <key> mapping:

<bag name="Keywords" table="StatueKeyword" lazy="false">
  <!-- <key> is representing column where current Statue ID should be searched 
   while the below one seems to be the ID column of the pairing table
   so instead of this
  <key column="IdStatueKeyword"/>
   use this: -->
  <key column="IdStatue"/>
  <many-to-many class="Keyword" column="IdKeyword"/>
</bag>

Also check these:

Small cite:

The foreign key from the collection table to the table of the owning class is declared using a <key> element.

Another tip, if you do have an ID column of the pairing table, you should try to use augmented feature:

Another cite from doc about idbag:

Note that the update performance of an <idbag> is much better than a regular <bag>! NHibernate can locate individual rows efficiently and update or delete them individually, just like a list, map or set.

Finally, I (personally) would descourage you from using many-to-many. My view is, that it is better to avoid it. See: 24. Best Practices (a cite:)

Don't use exotic association mappings.

Good usecases for a real many-to-many associations are rare. Most of the time you need additional information stored in the "link table". In this case, it is much better to use two one-to-many associations to an intermediate link class. In fact, we think that most associations are one-to-many and many-to-one, you should be careful when using any other association style and ask yourself if it is really neccessary.

Maybe later also check these:

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top