Fluent NHibernate - HasOne mapped to a ReferencesAny -
i have following poco classes:
public class container { public virtual int64 containerid { get; protected set; } public virtual string name { get; set; } public virtual location location { get; set; } } public abstract class location { public virtual int64 locationid { get; protected set; } public virtual string name { get; set; } } public class uniquelocation : location { public virtual container container { get; set; } } public class sharedlocation : location { public sharedlocation() { this.containers = new list<container>(); } public virtual ilist<container> containers { get; set; } }
and following fluent mapping:
public class containermap: classmap<container> { public containermap() { table("containers"); id(x => x.containerid); map(x => x.name); referencesany(x => x.location).identitytype<int64>().entitytypecolumn("locationtype").entityidentifiercolumn("locationid") .addmetavalue<uniquelocation>("u") .addmetavalue<sharedlocation>("s"); } } public class locationmap : classmap<location> { public locationmap() { table("locations"); id(x => x.locationid); map(x => x.name); } } public class uniquelocationmap : subclassmap<uniquelocation> { public uniquelocationmap() { hasone(x => x.container).propertyref(x => x.location).foreignkey("locationid").cascade.all().constrained(); } } public class sharedlocationmap : subclassmap<sharedlocation> { public sharedlocationmap() { hasmany(x => x.containers).keycolumn("locationid"); } }
the problem hasone() mapping generates following exception: "broken column mapping for: container.location of: uniquelocation, type object expects 2 columns, 1 mapped".
how tell hasone() use/map both locationtype , locationid?
afaik conditions not possible on entity references except using formulas. design seems strange because nasty change unique location shared location.
what want can done using:
reference(x => x.container).formula("(select c.id container c c.locationid = id , c.locationtype = 'u')");
but prefere
class location { ... public virtual bool isunique { { return container.count == 1; } } }
Comments
Post a Comment