mysql - (Fluent) NHibernate composite-id 问题 : MySQL complains that the parameter index is out of bounds

标签 mysql fluent-nhibernate nhibernate-mapping parameters composite-id

我正在使用 Fluent NHibernate 创建一个 ASP.NET MVC 项目,由用户提交和投票(上/下)。

当然,用户可以投票提交 Up 或 Down。为了记录这一点,我创建了一个中间表 SubmissionVote,其中包含以下字段:

submissionId (int)
userId (int)
score (enum: up/down)

这是我的映射:

提交

Id(x => x.Id);
...
References(x => x.User).Column("userID").Not.LazyLoad();
HasMany(x => x.Votes).Cascade.All().KeyColumn("submissionID").Table("SubmissionVote").Not.LazyLoad();
Table("Submission");

用户

Id(x => x.Id);
...
HasMany(x => x.Submission).Inverse().KeyColumn("userID").Cascade.All();
HasMany(x => x.SubmissionVotes).KeyColumn("userID").Cascade.All().Table("SubmissionVote");

投稿投票

Map(x => x.Vote).Column("score");
CompositeId().KeyProperty(x => x.Submission.Id, "submissionID").KeyProperty(x => x.User.Id, "userID");
References(x => x.Submission).Column("submissionID");
References(x => x.User).Column("userID");

由于我的数据库中的中间表有一个分数字段,我创建了 SubmissionVote 类来表示它。该类中的用户和提交字段与其各自的类是多对一的关系。

问题是这样的:SubmissionVote 类需要有一个 ID 属性,否则 NHibernate 会产生错误。情况不应如此,因为 SubmissionVote 的 ID 是一个复合 ID,如映射中所示。

我可以忍受,但它会导致另一个问题。当我尝试使用 session.SaveOrUpdate(submission) 保存其中包含一些 SubmissionVotes 的提交时,我收到此错误,即 MySQL 抛出的 ArrayOutOfBounds 异常,如以下问题:How can I use a composite-id with a class as the id field in fluent nhibernate? .

查看 NHibernate 日志,我可以看到发送到 MySQL 的查询。

14:53:07.358 [9] DEBUG ... - Building an IDbCommand object for the SqlString: INSERT INTO `SubmissionVote` (score, submissionID, userID) VALUES (?, ?, ?)
14:53:07.361 [9] DEBUG ... - binding 'Up' to parameter: 0
14:53:07.367 [9] DEBUG NHibernate.Type.Int32Type - binding '183' to parameter: 1
14:53:07.367 [9] DEBUG NHibernate.Engine.IdentifierValue - unsaved-value: 0
14:53:07.367 [9] DEBUG NHibernate.Type.Int32Type - binding '2' to parameter: 2
14:53:07.367 [9] DEBUG NHibernate.Type.Int32Type - binding '0' to parameter: 3

显然,NHibernate 试图在查询中包含一个默认的“ID”值('0')(可能是我添加到 SubmissionVote 的 ID 属性)。

这两个问题有关联吗?我该如何解决这个问题?

非常感谢!

编辑

这是由 NHibernate 生成的我的(部分但相关的)模式。

create table submission (
    Id INTEGER NOT NULL AUTO_INCREMENT,
   CreationDate DATETIME,
   BeginDate DATETIME,
   EndDate DATETIME,
   Content VARCHAR(255),
   userID INTEGER,
   primary key (Id)
)

create table User (
Id INTEGER NOT NULL AUTO_INCREMENT,
   UserName VARCHAR(255) unique,
   Password VARCHAR(255),
   Email VARCHAR(255),
   primary key (Id)
)

create table `submissionVote` (
submissionID INTEGER not null,
   userID INTEGER not null,
   score VARCHAR(255),
   primary key (submissionID, userID)
)

alter table `submissionVote` 
    add index (submissionID), 
    add constraint FKC2AE73C56C66D2D5 
foreign key (submissionID) 
references submission (Id)

alter table `submissionVote` 
add index (userID), 
add constraint FKC2AE73C5EC6C1277 
foreign key (userID) 
references User (Id)

alter table submission 
 add index (userID), 
 add constraint FKE6354599EC6C1277 
 foreign key (userID) 
 references User (Id)

最佳答案

我找到了解决方案!实际上,我必须向 NHibernate 指定 composite-id 也恰好是我的引用。

新映射如下所示:

Map(x => x.Vote).Column("score");
CompositeId().KeyReference(x => x.Submission, "submissionID").KeyReference(x => x.User, "userID");

关于mysql - (Fluent) NHibernate composite-id 问题 : MySQL complains that the parameter index is out of bounds,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2036036/

相关文章:

hibernate - 什么是 hibernate 和n- hibernate ?

fluent-nhibernate - 覆盖 IAutoMappingOverride 中的组件列

c# - CustomType 对 FluentNHibernate 的引用

nhibernate - 在NHibernate 3.2中通过代码将Enum映射为字符串

php - 使用 php 的阿拉伯语 Mysql 排序规则

fluent-nhibernate - 使用 fluent-nhibernate,是否有约定使一对多关系中的外键列不为空?

NHibernate 使用复合键将两个表连接成一个实体

mysql - 一个相当复杂的自增

mysql - 在 MySQL 中同时运行多个创建触发器

php - 为什么别名会导致我的查询在 phpMyAdmin 中失败?