views:

103

answers:

2

Hi guys,

I've got a "weird" scenario in an app I'm writting, and trying to get to grips on ow I can implement using nHibernate.

The scenario goes like this ... there is a Test. A Test is made up of a series of Testlets ( a Testlet is a set of pre-defined questions, with metadata ... ).

The thing ... while I'm running the test, the testlets that make it up are randomly selected from the database, according to the characteristics of the Test ( it's Level and type that is ... ).

What all this implies is that in the Testlet table in my DB, there shouldn't be a FK to the Test table. I was thinking that a custom in my pointing to a stored proc would do the job - however, when I export my mapping hbms schema to an empty DB, it still creates foreign keys in my Testlet table.

Here's my mapping for Test:

<class name="Test">
    <id name="UID">
      <generator class="guid" />
    </id>
    <property name="Type" />
    <property name="Description" />

    <bag name="Items" generic="true" inverse="false">
      <key column="UID"/>
      <one-to-many class="Testlet"/>
      <loader query-ref="loadTestletByTestID"/>
    </bag>
  </class>

<sql-query name="loadTestletByTestID">
    <load-collection alias="items" role ="Test.Items"/>
    exec [TestletByTestID] ?
  </sql-query>

Is there a way to get around that auto-created foreign key?

Thanks a lot, Angel

A: 

Why not just remove the Items from the mapping, and call a query to load random items and assign them after loading the Test? Then you wouldn't even need a stored procedure.

If it is really important to load it automatically, without code after loading the Test, you could put this code into an NHibernate even. Then it is executed whenever a Test is loaded. But actually, I doubt that it is worth the effort.

Stefan Steinegger
A: 

That's exactly what I did in the end Stefan.

Removed the items from the mapping, and handled it with a query after loading the test :)

angel