NHibernate component with a one-to-many relation f

2019-08-20 08:14发布

问题:

Say I have a Queue table and a Job table. In the Job table there is a foreign key column QueueId for the Queue table, i.e.

Queue.Id <-- Job.QueueId

Using Fluent NHibernate it is pretty straightforward to map this to a property in the Queue class, i.e.

/* QueueMap */
HasMany(x => x.Jobs)
   .KeyColumnNames.Add("QueueId");

But assume I have a very good reason to have a class inbetween, say something like:

public class Queue 
{
    public Group Group { get; set; }
}

public class Group
{
    public IList<Job> Jobs { get; private set; }
}

Then I need to map this using a Component, i.e.

/* QueueMap */

Component(
    x => x.Group,
    y => y.HasMany(x => x.Jobs).KeyColumnNames.Add("QueueId")
);

When I do this I get the following:

{"could not initialize a collection: 
[Queue.Group.Jobs#832fc413-c282-48e8-8cb6-d2a70b0b8de4]
[SQL: SELECT values0_.QueueId as QueueId1_, values0_.Id as Id1_, values0_.Id 
 as Id16_0_, (....) FROM dbo.Jobs values0_ WHERE values0_.QueueId=?]"}

Any idea as to what I'm doing wrong...

回答1:

Solved. This was caused by a mapping problem in the JobMap.