简体   繁体   中英

Nhibernate mapping of Table without Entity

I have a Table And Entity called Software


Software

  • Id
  • Serial
  • Name
  • LinkedSoftware (This field is in the Entity)

Then I have a Table LinkedSoftware with no Entity


LinkedSofware

  • BaseSoftwareId (Relationship with Software.Id)
  • LinkedSoftwareId (Relationship with Software.Id)

I am struggling to setup my hibernate mapping file, keep getting mapping errors. I have tried the following with no luck:

<set name="linkedSoftware" access="field" cascade="all-delete-orphan" table="LinkedSoftware">
  <many-to-many class="Software" column="BaseSoftwareId" />
  <many-to-many class="Software" column="LinkedSoftwareId" />
</set>

<many-to-one name="LinkedSoftware" column="BaseSoftwareId"       cascade="save-update" class="Software" />
<many-to-one name="LinkedSoftware" column="LinkedSoftwareId" cascade="save- update" class="Software" />-->

Is there anyone that can please point me in the right direction. I tried to google but couldnt really find an answer.

I think you should make table LinkedSotware an entity, too. Then try write your set more like this:

 <set name="LinkedSoftware" table="LinkedSoftware">
  <key>
    <column name="BaseSoftwareId" />
  </key>
  <one-to-many class="LinkedSoftware" />
</set>

But not sure if that is everything, because that will give you into set just the LinkedSoftware instances which have the instance of Software as BaseSoftware . Sorry for my English, hope you understand.

And then in LinkedSoftware mapping:

<many-to-one name="BaseSoftware" class="Software">
<column name="BaseSoftwareId" />
</many-to-one>

<many-to-one name="LinkedSoftware" class="Software">
<column name="LinkedSoftwareId" />
</many-to-one>

This NHibernate: Two foreign keys in the same table against same column could help you. And I would avoid using many-to-many, I think this would be more flexible. For many-to-many explanation see Nhibernate: How to represent Many-To-Many relationships with One-to-Many relationships? .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM