I am having some redundant primary key issues.
I have an Item which contains many reports. I have mapped them as shown below. I can do Session.QueryOver(Of Item).List and there are no extra columns generated. I can also do Session.QueryOver(Of Report).List and there are no extra columns generated.
However, once I try to traverse the relationship from Item to Reports, I get the SQL query shown below. Can anyone tell me why? Thanks in advance!
Item Mapping:
Public Class ItemMapping
Inherits ClassMap(Of Item)
Public Sub New()
Table("Items")
Id(Function(x) x.ItemID)
HasMany(Function(x) x.Reports).KeyColumn("ItemID").Inverse().Cascade.All()
End Sub
End Class
Report Mapping:
Public Class ReportMapping
Inherits ClassMap(Of Report)
Public Sub New()
Table("Reports")
Id(Function(x) x.ReportID)
References(Function(x) x.Item).Column("ItemID")
Map(Function(x) x.ReportName)
End Sub
End Class
SQL Result:
SELECT repor0_.ItemID as ItemID1_,
repor0_.ReportID as Rep1_1_,
repor0_.ReportID as Rep1_4_0_,
repor0_.ReportName as Rep2_4_0_,
repor0_.ItemID as ItemID4_0_ FROM dbo.Reports repor0_ WHERE repor0_.ItemID=@p0;@p0 = 1266 [Type: Int32 (0)]
This is not a bug according to nhusers group. Apparently NHibernate uses one column for the ID and second one for the foreign key. There are also other cases where some of the columns are sent twice. According to the thread it is not worth it to optimize the extra column away because it doesn’t generate additional I/O usage and in normal cases the extra column does not cause too much network traffic.