当Id未映射时通过Id列表加载实体的正确方法

发布于 2024-08-07 21:38:22 字数 2087 浏览 4 评论 0原文

我有以下代码

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

埋葬我深情 2024-08-14 21:38:22

请尝试使用 IdEq 来代替。它需要另一个析取。

Disjunction idsRestriction = Restrictions.Disjunction();
entityInfo
  .Select(x => x.Id)
  .ToList()
  .ForEach(id => idsRestriction.Add(Restriction.IdEq(id)));
disjunction.Add(idsRestriction);

注意:这将产生类似 (id = 1 OR id = 2 OR id = 3 ...) 而不是 (id IN (1, 2, 3)

不能百分百确定它是否有效,但应该有效。

Try IdEq instead. It needs another Disjunction.

Disjunction idsRestriction = Restrictions.Disjunction();
entityInfo
  .Select(x => x.Id)
  .ToList()
  .ForEach(id => idsRestriction.Add(Restriction.IdEq(id)));
disjunction.Add(idsRestriction);

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.

小红帽 2024-08-14 21:38:22

可以使用IdEq,或者使用关键字“id”(全部小写)来引用主键

You can use IdEq, or use the keyword "id" (all lower case) to refer to the primary key

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文