Duplicate Items Using Join in NHibernate Map
I am trying to retrieve the individual detail rows without having to create an object for the parent. I have a map which joins a parent table with the detail to achieve this:
Table("UdfTemplate");
Id(x => x.Id, "Template_Id");
开发者_开发百科Map(x => x.FieldCode, "Field_Code");
Map(x => x.ClientId, "Client_Id");
Join("UdfFields", join =>
{
join.KeyColumn("Template_Id");
join.Map(x => x.Name, "COLUMN_NAME");
join.Map(x => x.Label, "DISPLAY_NAME");
join.Map(x => x.IsRequired, "MANDATORY_FLAG")
.CustomType<YesNoType>();
join.Map(x => x.MaxLength, "DATA_LENGTH");
join.Map(x => x.Scale, "DATA_SCALE");
join.Map(x => x.Precision, "DATA_PRECISION");
join.Map(x => x.MinValue, "MIN_VALUE");
join.Map(x => x.MaxValue, "MAX_VALUE");
});
When I run the query in NH using:
Session.CreateCriteria(typeof(UserDefinedField))
.Add(Restrictions.Eq("FieldCode", code)).List<UserDefinedField>();
I get back the first row three times as opposed to the three individual rows it should return. Looking at the SQL trace in NH Profiler the query appears to be correct. The problem feels like it is in the mapping but I am unsure how to troubleshoot that process. I am about to turn on logging to see what I can find but I thought I would post here in case someone with experience mapping joins knows where I am going wrong.
Found aboutn about SetResults transformer here :
http://www.coderanch.com/t/216546/ORM/java/Hibernate-returns-duplicate-results
Which makes your code:
Session.CreateCriteria(typeof(UserDefinedField))
.Add(Restrictions.Eq("FieldCode", code))
.SetResultTransformer(CriteriaSpecification.DistinctRootEntity)
.List<UserDefinedField>();
Cheers John
精彩评论