.net - NHibernate mapping table of tables -
i'm trying wrap nhibernate around legacy database (not created me). has can best describe "table of tables," called table_detail. looks this:
column_name | table_value | table_desc -------------+-----------------+--------------------------------- state | ca | california state | ny | new york ... country | | united states country | ca | canada
i'm trying map using fluent nhibernate table-per-class-hierarchy strategy. in other words, have tabledetail class, , subclasses state , country. use column_name discriminator.
tabledetailmap.cs:
public class tabledetailmap : classmap<tabledetail> { public tabledetailmap() { table("table_detail"); compositeid() .keyproperty(x => x.tablevalue, "table_value") .keyproperty(x => x.columnname, "column_name"); map(x => x.columnname).column("column_name"); map(x => x.tabledesc).column("table_desc"); discriminatesubclassesoncolumn("column_name"); } }
statemap.cs:
public class statemap : subclassmap<state> { public statemap() { discriminatorvalue("state"); } }
table_detail has composite key (made of column_name/table_value), , discriminator 1 of fields. problem nhibernate expects both components of composite key referenced in table - shouldn't need be, because 1 defined discriminator.
for example, have address_record table:
line_1 | city | state | zip -----------------+-----------------+-----------+---------------- 123 street | anytown | ca | 12345
the problem comes in when try map "state" field state class. state column in address_record refers half of table_detail primary key - table_value part. column_name part "state" - because it's discriminator, i'd expect should provided. nhibernate doesn't think so, , throws exception:
foreign key (fk3d33e87ca66e339c:address_record [state])) must have same number of columns referenced primary key (table_detail [table_value, column_name])
how can map nhibernate knows automatically provide "state" value second half of composite key?
if can provide more information, please let me know.
i came so-so fix. in base class map (tabledetailmap.cs), specified discriminator, specified "always select value", so:
public class tabledetailmap : classmap<tabledetail> { public tabledetailmap() { ...snip... discriminatesubclassesoncolumn("column_name").alwaysselectwithvalue(); } }
then, in of mapping classes use tabledetail object, had specify use "select" when fetching, so:
public class addressrecordmap : classmap<addressrecord> { public addressrecordmap() { ...snip... references(x => x.state) .column("state") .fetch.select(); ...snip... } }
this still did not work in case had 1 of these objects used part of composite id. i'm not sure how fix that, able work around now.
Comments
Post a Comment