开发者

Fluent NHibernate Many to One Mapping Problem

开发者 https://www.devze.com 2023-04-03 14:27 出处:网络
Edit I have the answer and will post it up tomorrow. I have a class that appears to be using the parent primary key column instead of it\'s own primary key column.

Edit

I have the answer and will post it up tomorrow.

I have a class that appears to be using the parent primary key column instead of it's own primary key column.

There are two classes involved here ActionHistory and ActionData. The parent开发者_如何学JAVA class, ActionHistory, represents an action and details who performed the action. The child class, ActionData, represents a piece of data that was created by the action. Each ActionHistroy has a number of ActionData and this is represented through a many to one mapping.

The classes are:

public class ActionHistory
{
    public virtual int Code { get; set; } 
    public virtual string Doneby { get; set; }
    public virtual IList<ActionData> _ActionData{ get; set;}
}

public class ActionData
{
    public virtual int Code { get; set; }
    public virtual double Data{ get; set; }
    public virtual ActionHistory _ActionHistory{ get; set; }
}

My mappings are:

public class ActionHistoryClassMap : ClassMap<ActionHistory>
{
    public ActionHistoryClassMap ()
    {
        Table("ACTIONHISTORY");
        Id(x => x.Code, "CODE").GeneratedBy.Assigned();
        Map(x => x.Doneby, "DONEBY");
        HasMany(x => x._Actiondata).KeyColumn("CODE").AsBag().Cascade.All();
    }
}

public class ActionDataClassMap : ClassMap<ActionData>
{
    public ActionDataClassMap ()
    {
        Table("ACTIONDATA");
        Id(x => x.Code, "CODE").GeneratedBy.Assigned();
        Map(x => x.Data, "DATA");
        References(x => x._ActionHistory, "ACTIONHISTORYID");
    }
}

The problem is when an ActionHistory class is queried the correct ActionHistory is returned but the HistoryData that it contains is wrong. A list of HistoryData should be returned but only one HistoryData is returned and it's Code value is the Code values of the parent ActionHistory class. I believe my problem stems form the fact that both classes have the same primary key column name and I'm not handling this properly. The columns that that are related are CODE from the ACTIONHISTORY table and ACTIONHISTORYID from the ACTIONDATA table. The database is fixed so I can't rename any columns. In this case data is only read from the database and not saved or updated.

I downloaded NHibernate Profiler to help me with this problem and have found the exact point in the SQL where the problem occurs.

This is the SQL query that is generated to return the ActionData (where 18 is the ActionHistory primary key):

SELECT actiondat0_.CODE              as CODE1_,
   actiondat0_.CODE              as CODE9_0_,
   actiondat0_.DATA              as DATA9_0_,
   actiondat0_.ACTIONHISTORYID  as ACTIONHI4_9_0_
FROM   ACTIONDATA actiondat0_
WHERE  actiondat0_.CODE = 18 /* @p0 */

The last line should be:

WHERE  actiondat0_.ACTIONHISTORYID = 18 /* @p0 */

But I just do not understand why the mapping is not generating the right query.


I know it was something blindingly obvious and after hours of getting nowhere it of course jumps out at me.

The ActionHistory class map should of course be:

public class ActionHistoryClassMap : ClassMap<ActionHistory>
{
    public ActionHistoryClassMap ()
    {
        Table("ACTIONHISTORY");
        Id(x => x.Code, "CODE").GeneratedBy.Assigned();
        Map(x => x.Doneby, "DONEBY");
        HasMany(x => x._Actiondata).KeyColumn("ACTIONHISTORYID")
            .AsBag().Cascade.All();
    }
}

The difference being the KeyColumn method is now ACTIONHISTORYID as opposed to CODE. I didn't cop that KeyColumn should reference the ActionHistory key column. It seems obvious now and it didn't help that all other times I implemented it in this project the columns with the relationship always had the same name.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号