NHibernate xml 继承子类找不到列
NHibernate xml inheritance subclass could not find column
我是第一次使用 NHibernate。我有一个 table 每个 class 层次结构(抽象超级 class)。当我尝试保存 subclass(学生)时,我收到错误消息:
[SqlException (0x80131904): Invalid column name 'Firstname' etc...]
我的 HBM 文件:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="xx"
namespace="xx.Models">
<class name="User" table="[User]" lazy="false">
<id name="UserID" column="UserID">
<generator class="native" />
</id>
<discriminator column="Type" type="String"/>
<property name="Email" column="Email" />
<subclass name="Student" discriminator-value="0">
<property name="Firstname" column="Firstname" />
</subclass>
<subclass name="Company" discriminator-value="1">
<property name="Name" column="Name" />
</subclass>
</class>
</hibernate-mapping>
SQL代码:
CREATE TABLE [dbo].[User] (
UserID INT NOT NULL IDENTITY(1,1),
Type CHAR(1) NOT NULL,
Email VARCHAR(255) NOT NULL,
PRIMARY KEY(UserID)
);
CREATE TABLE [dbo].[Student] (
UserID INT NOT NULL,
Firstname VARCHAR(255) NOT NULL,
PRIMARY KEY(UserID),
FOREIGN KEY(UserID) REFERENCES [User](UserID)
);
CREATE TABLE [dbo].[Company] (
UserID INT NOT NULL,
Name VARCHAR(255) NOT NULL,
PRIMARY KEY(UserID),
FOREIGN KEY(UserID) REFERENCES [User](UserID),
);
我可能缺少配置设置,数据库是 MS SQL,谁能帮帮我?
更新(与更新的问题相关)
简而言之,我们需要映射 <joined-subclass>
而不是 <discriminator>
重点是,我们在需要
时为8.1.1. Table per class hierarchy使用映射
8.1.2. Table per subclass
(很少引用)
A table-per-subclass mapping would look like:
<class name="IPayment" table="PAYMENT">
<id name="Id" type="Int64" column="PAYMENT_ID">
<generator class="native"/>
</id>
<property name="Amount" column="AMOUNT"/>
...
<joined-subclass name="CreditCardPayment" table="CREDIT_PAYMENT">
<key column="PAYMENT_ID"/>
...
</joined-subclass>
<joined-subclass name="CashPayment" table="CASH_PAYMENT">
<key column="PAYMENT_ID"/>
...
</joined-subclass>
<joined-subclass name="ChequePayment" table="CHEQUE_PAYMENT">
<key column="PAYMENT_ID"/>
...
</joined-subclass>
</class>
Four tables are required. The three subclass tables have primary key associations to the superclass table (so the relational model is actually a one-to-one association).
与此类似,我们需要这样的映射:
<class name="User" table="[User]" lazy="false">
<id name="UserID" column="UserID">
<generator class="native" />
</id>
<joined-subclass name="Sutdent" table="Student">
<key column="UserID"/>
<property name="Firstname" column="Firstname" />
...
</joined-subclass>
</class>
原始部分(与原始问题相关)
在这种情况下,错误应该非常清楚:缺少列 'Firstname'
。因为使用 table 每个 class 层次结构 我们只需要在我们的 table 中包含所有列。每个完整的层次结构是一个 table。没有其他 place/table 可以保存数据的地方...
8.1.1. Table per class hierarchy
Exactly one table is required. There is one big limitation of this mapping strategy: columns declared by the subclasses may not have NOT NULL constraints.
万一您问:NHibernate 如何为我更新数据库模式? 请检查:
How to update database table schemas with NHibernate schema generation?
(小摘录,详见上文link)
Configuration cfg ...
cfg.Configure();
...
var update = new SchemaUpdate(cfg);
update.Execute(true, false);
我是第一次使用 NHibernate。我有一个 table 每个 class 层次结构(抽象超级 class)。当我尝试保存 subclass(学生)时,我收到错误消息:
[SqlException (0x80131904): Invalid column name 'Firstname' etc...]
我的 HBM 文件:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="xx"
namespace="xx.Models">
<class name="User" table="[User]" lazy="false">
<id name="UserID" column="UserID">
<generator class="native" />
</id>
<discriminator column="Type" type="String"/>
<property name="Email" column="Email" />
<subclass name="Student" discriminator-value="0">
<property name="Firstname" column="Firstname" />
</subclass>
<subclass name="Company" discriminator-value="1">
<property name="Name" column="Name" />
</subclass>
</class>
</hibernate-mapping>
SQL代码:
CREATE TABLE [dbo].[User] (
UserID INT NOT NULL IDENTITY(1,1),
Type CHAR(1) NOT NULL,
Email VARCHAR(255) NOT NULL,
PRIMARY KEY(UserID)
);
CREATE TABLE [dbo].[Student] (
UserID INT NOT NULL,
Firstname VARCHAR(255) NOT NULL,
PRIMARY KEY(UserID),
FOREIGN KEY(UserID) REFERENCES [User](UserID)
);
CREATE TABLE [dbo].[Company] (
UserID INT NOT NULL,
Name VARCHAR(255) NOT NULL,
PRIMARY KEY(UserID),
FOREIGN KEY(UserID) REFERENCES [User](UserID),
);
我可能缺少配置设置,数据库是 MS SQL,谁能帮帮我?
更新(与更新的问题相关)
简而言之,我们需要映射 <joined-subclass>
而不是 <discriminator>
重点是,我们在需要
时为8.1.1. Table per class hierarchy使用映射8.1.2. Table per subclass
(很少引用)
A table-per-subclass mapping would look like:
<class name="IPayment" table="PAYMENT">
<id name="Id" type="Int64" column="PAYMENT_ID">
<generator class="native"/>
</id>
<property name="Amount" column="AMOUNT"/>
...
<joined-subclass name="CreditCardPayment" table="CREDIT_PAYMENT">
<key column="PAYMENT_ID"/>
...
</joined-subclass>
<joined-subclass name="CashPayment" table="CASH_PAYMENT">
<key column="PAYMENT_ID"/>
...
</joined-subclass>
<joined-subclass name="ChequePayment" table="CHEQUE_PAYMENT">
<key column="PAYMENT_ID"/>
...
</joined-subclass>
</class>
Four tables are required. The three subclass tables have primary key associations to the superclass table (so the relational model is actually a one-to-one association).
与此类似,我们需要这样的映射:
<class name="User" table="[User]" lazy="false">
<id name="UserID" column="UserID">
<generator class="native" />
</id>
<joined-subclass name="Sutdent" table="Student">
<key column="UserID"/>
<property name="Firstname" column="Firstname" />
...
</joined-subclass>
</class>
原始部分(与原始问题相关)
在这种情况下,错误应该非常清楚:缺少列 'Firstname'
。因为使用 table 每个 class 层次结构 我们只需要在我们的 table 中包含所有列。每个完整的层次结构是一个 table。没有其他 place/table 可以保存数据的地方...
8.1.1. Table per class hierarchy
Exactly one table is required. There is one big limitation of this mapping strategy: columns declared by the subclasses may not have NOT NULL constraints.
万一您问:NHibernate 如何为我更新数据库模式? 请检查:
How to update database table schemas with NHibernate schema generation?
(小摘录,详见上文link)
Configuration cfg ...
cfg.Configure();
...
var update = new SchemaUpdate(cfg);
update.Execute(true, false);