在我正在处理的项目中,我有以下实体:分析师,客户端和承包商。每个都继承自基类 User 。
public abstract class User {
public virtual int Id { get; set; }
public virtual string Username { get; set; }
public virtual string FullName { get; set; }
}
然后我将其他类从基类继承为:
public class Analyst : User {
// Empty class. There are no additional properties for an analyst.
}
public class Client : User {
// Empty class. There are no additional properties for a client.
}
public class Contractor : User {
public int TotalJobs { get; set; }
public int JobsInProgress { get; set; }
}
对于上面的类,我有以下表结构:
USER
----
UserId
Username
FullName
UserType (1 = Analyst, 2 = Client, 3 = Contractor)
CONTRACTOR
----------
UserId
TotalJobs
JobsInProgress
Analyst和Client类没有表格。
我想知道如何为Contractor类编写NHibernate映射文件。对于其他类,我创建了一个用户映射文件,并将Client和Analyst添加为子类。如何映射Contractor类?
答案 0 :(得分:2)
这非常适合8.1.4. Mixing table per class hierarchy with table per subclass
中描述的方法<subclass name="Contractor" discriminator-value=3>
<join table="CONTRACTOR">
<key column="UserId"/>
<property name="TotalJobs"/>
<property name="JobsInProgress"/>
</join>
</subclass>
答案 1 :(得分:1)
您是否尝试过子类的子类? http://www.javalobby.org/java/forums/t18300.html