当Id未映射时通过Id列表加载实体的正确方法
我有以下代码
string idName = builder.IdentifierName;
Disjunction disjunction = Restrictions.Disjunction();
IList ids = new ArrayList(entityInfos.Length);
foreach (var entityInfo in entityInfos)
{
ids.Add(entityInfo .Id);
}
disjunction.Add(Restrictions.In(idName, ids));
criteria.Add(disjunction);
criteria.List();
(我没有编写它,它是 NHibernate.Search 的简化代码)
idName
的值是正确的(“Id”
)。
在我的映射中,我没有将 Id 映射到实体属性,它仅是数据库:
<id column="Id" type="int">
<generator class="native" />
</id>
当我运行代码时,它会抛出异常:
[QueryException: could not resolve property: Id of: MyType]
NHibernate.Persister.Entity.AbstractPropertyMapping.ToType(String propertyName) +326
NHibernate.Loader.Criteria.CriteriaQueryTranslator.GetTypeUsingProjection(ICriteria subcriteria, String propertyName) +416
NHibernate.Criterion.InExpression.AssertPropertyIsNotCollection(ICriteriaQuery criteriaQuery, ICriteria criteria) +41
NHibernate.Criterion.InExpression.ToSqlString(ICriteria criteria, ICriteriaQuery criteriaQuery, IDictionary`2 enabledFilters) +49
NHibernate.Criterion.Junction.ToSqlString(ICriteria criteria, ICriteriaQuery criteriaQuery, IDictionary`2 enabledFilters) +448
NHibernate.Loader.Criteria.CriteriaQueryTranslator.GetWhereCondition(IDictionary`2 enabledFilters) +354
NHibernate.Loader.Criteria.CriteriaJoinWalker..ctor(IOuterJoinLoadable persister, CriteriaQueryTranslator translator, ISessionFactoryImplementor factory, ICriteria criteria, String rootEntityName, IDictionary`2 enabledFilters) +516
NHibernate.Loader.Criteria.CriteriaLoader..ctor(IOuterJoinLoadable persister, ISessionFactoryImplementor factory, CriteriaImpl rootCriteria, String rootEntityName, IDictionary`2 enabledFilters) +220
NHibernate.Impl.SessionImpl.List(CriteriaImpl criteria, IList results) +364
NHibernate.Impl.CriteriaImpl.List(IList results) +63
NHibernate.Impl.CriteriaImpl.List() +46
创建限制和条件以便我可以通过 Id
即使它没有被映射?
I have the following code
string idName = builder.IdentifierName;
Disjunction disjunction = Restrictions.Disjunction();
IList ids = new ArrayList(entityInfos.Length);
foreach (var entityInfo in entityInfos)
{
ids.Add(entityInfo .Id);
}
disjunction.Add(Restrictions.In(idName, ids));
criteria.Add(disjunction);
criteria.List();
(I haven't written it, it's simplified code from NHibernate.Search)
Value of idName
is correct ("Id"
).
In my mapping I do not have Id mapped to an entity property, it is DB-only:
<id column="Id" type="int">
<generator class="native" />
</id>
When I run the code, it throws an exception:
[QueryException: could not resolve property: Id of: MyType]
NHibernate.Persister.Entity.AbstractPropertyMapping.ToType(String propertyName) +326
NHibernate.Loader.Criteria.CriteriaQueryTranslator.GetTypeUsingProjection(ICriteria subcriteria, String propertyName) +416
NHibernate.Criterion.InExpression.AssertPropertyIsNotCollection(ICriteriaQuery criteriaQuery, ICriteria criteria) +41
NHibernate.Criterion.InExpression.ToSqlString(ICriteria criteria, ICriteriaQuery criteriaQuery, IDictionary`2 enabledFilters) +49
NHibernate.Criterion.Junction.ToSqlString(ICriteria criteria, ICriteriaQuery criteriaQuery, IDictionary`2 enabledFilters) +448
NHibernate.Loader.Criteria.CriteriaQueryTranslator.GetWhereCondition(IDictionary`2 enabledFilters) +354
NHibernate.Loader.Criteria.CriteriaJoinWalker..ctor(IOuterJoinLoadable persister, CriteriaQueryTranslator translator, ISessionFactoryImplementor factory, ICriteria criteria, String rootEntityName, IDictionary`2 enabledFilters) +516
NHibernate.Loader.Criteria.CriteriaLoader..ctor(IOuterJoinLoadable persister, ISessionFactoryImplementor factory, CriteriaImpl rootCriteria, String rootEntityName, IDictionary`2 enabledFilters) +220
NHibernate.Impl.SessionImpl.List(CriteriaImpl criteria, IList results) +364
NHibernate.Impl.CriteriaImpl.List(IList results) +63
NHibernate.Impl.CriteriaImpl.List() +46
What is the correct way to create a restriction and criteria so that I can search by Id
even if it is not mapped?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请尝试使用
IdEq
来代替。它需要另一个析取。注意:这将产生类似
(id = 1 OR id = 2 OR id = 3 ...)
而不是(id IN (1, 2, 3)
。不能百分百确定它是否有效,但应该有效。
Try
IdEq
instead. It needs another Disjunction.Note: this will produce something like
(id = 1 OR id = 2 OR id = 3 ...)
instead of(id IN (1, 2, 3)
.Not hundred percent sure if it works, but it should.
可以使用IdEq,或者使用关键字“id”(全部小写)来引用主键
You can use IdEq, or use the keyword "id" (all lower case) to refer to the primary key