gpt4 book ai didi

mysql - NHibernate CreateSqlQuery 和 addEntity

转载 作者:行者123 更新时间:2023-11-30 23:40:39 26 4
gpt4 key购买 nike

hibernate 手册是这样说的:

String sql = "SELECT ID as {c.id}, NAME as {c.name}, " +

   "BIRTHDATE as {c.birthDate}, MOTHER_ID as {c.mother}, {mother.*} " +

"FROM CAT_LOG c, CAT_LOG m WHERE {c.mother} = c.ID";

List loggedCats = sess.createSQLQuery(sql)

  .addEntity("cat", Cat.class)

.addEntity("mother", Cat.class).list()

现在,我所拥有的基本上是一样的。我每行返回两个相同类型的。我正在做这样的选择:

SELECT {ctrl1.*}, {ctrl2.*} FROM tableA AS A
LEFT JOIN tableB AS ctrl1 ON (A.controlID = ctrl1.controlID AND ctrl1.controlOptionType = ? AND ctrl1.controlOptionValue = ?)
LEFT JOIN tableB AS ctrl2 ON (A.controlID = ctrl2.controlID AND ctrl2.controlOptionType = ? AND ctrl2.controlOptionValue = ?)

然后我 addEntity("ctrl1", typeof(mycontrolclass)addEntity("ctrl1", typeof(mycontrolclass)

在我看来,这与他们的示例完全相同。但我得到这个异常(exception):“无法执行查询”,内部异常是“在结果中找不到指定的列”。如果我复制异常中的 sql(它添加了“AS ctrl1_1_3_3_”等)它工作正常。

谢谢。

最佳答案

你到底想做什么?我相信您可能不需要使用它们中的任何一个。

// Using HQL:
var motherId = 25;
var hql = "select c.birthDate, c.mother from Cat c where c.mother.Id = :motherId";
var result = Session.CreateQuery(hql)
.SetParameter("motherId", motherId)
.ToList();

// Using NHibernate.LINQ:
var result = (from cat in Session.Linq<Cat>()
where cat.Mother.Id == motherId
select new { cat.birthDate, cat.mother }).ToList();

关于mysql - NHibernate CreateSqlQuery 和 addEntity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3368298/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com