将 HQL 转换为 Linq-to-NHibernate 时出现问题

发布于 2024-10-05 16:33:17 字数 2512 浏览 8 评论 0原文

如何将此 NHibernate HQL(有效)...

static IList<Phone> PhoneList()
{
    ISession session = OpenSession();

    IQuery query = session.CreateQuery("from Phone p where p.Kontact.ContactName = :_contact_name");
    query.SetParameter("_contact_name", "lennon");

    IList<Phone> contacts = query.List<Phone>();

    return contacts;        
}

...转换为 Linq-to-NHibernate(无效):

static IList<Phone> PhoneListUsingLinq()
{
    string contactName = "lennon";

    ISession session = OpenSession();

    var contacts = from Phone p in session.Query<Phone>()
        where p.Kontact.ContactName == contactName
        select p;

    return contacts.ToList();

}

对象:

public class Contact
{
    public virtual int ContactId { get; set; }
    public virtual string ContactName { get; set; }

    public virtual IList<Phone> Phones { get; set; }


}

public class Phone
{  
    public virtual Contact Kontact { get; set; }
    public virtual int PhoneId { set; get; }
    public virtual string PhoneNumber { get; set; }
}

这是这两个对象的 .hbm.xml 映射文件:

<?xml version="1.0" encoding="utf-8" ?>

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" auto-import="true" assembly="TestTwoTable" namespace="TestTwoTable">

  <class name="Contact" table="contact">
    <id name="ContactId" column="contact_id">
      <generator class="sequence">
        <param name="sequence">contact_contact_id_seq</param>
      </generator>
    </id>

    <property name="ContactName" column="contact_name"/>

    <bag name="Phones" inverse="true">
     <key column="contact_id"/>
     <one-to-many class="Phone"/>
    </bag>

  </class>

  <class name="Phone" table="phone">
    <id name="PhoneId" column="phone_id">
      <generator class="sequence">
        <param name="sequence">phone_phone_id_seq</param>
      </generator>
    </id>

    <property name="PhoneNumber" column="phone_number"/>

    <many-to-one name="Kontact" column="contact_id" class="Contact" not-null="true"/>

  </class>

</hibernate-mapping>

这有什么问题?

var contacts = from Phone p in session.Query<Phone>()
               where p.Kontact.ContactName == contactName
               select p;

注意:我使用的是 NHibernate 3

How to convert this NHibernate HQL(works) ...

static IList<Phone> PhoneList()
{
    ISession session = OpenSession();

    IQuery query = session.CreateQuery("from Phone p where p.Kontact.ContactName = :_contact_name");
    query.SetParameter("_contact_name", "lennon");

    IList<Phone> contacts = query.List<Phone>();

    return contacts;        
}

...to Linq-to-NHibernate(not working):

static IList<Phone> PhoneListUsingLinq()
{
    string contactName = "lennon";

    ISession session = OpenSession();

    var contacts = from Phone p in session.Query<Phone>()
        where p.Kontact.ContactName == contactName
        select p;

    return contacts.ToList();

}

The objects:

public class Contact
{
    public virtual int ContactId { get; set; }
    public virtual string ContactName { get; set; }

    public virtual IList<Phone> Phones { get; set; }


}

public class Phone
{  
    public virtual Contact Kontact { get; set; }
    public virtual int PhoneId { set; get; }
    public virtual string PhoneNumber { get; set; }
}

Here's the .hbm.xml mapping file of that two objects:

<?xml version="1.0" encoding="utf-8" ?>

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" auto-import="true" assembly="TestTwoTable" namespace="TestTwoTable">

  <class name="Contact" table="contact">
    <id name="ContactId" column="contact_id">
      <generator class="sequence">
        <param name="sequence">contact_contact_id_seq</param>
      </generator>
    </id>

    <property name="ContactName" column="contact_name"/>

    <bag name="Phones" inverse="true">
     <key column="contact_id"/>
     <one-to-many class="Phone"/>
    </bag>

  </class>

  <class name="Phone" table="phone">
    <id name="PhoneId" column="phone_id">
      <generator class="sequence">
        <param name="sequence">phone_phone_id_seq</param>
      </generator>
    </id>

    <property name="PhoneNumber" column="phone_number"/>

    <many-to-one name="Kontact" column="contact_id" class="Contact" not-null="true"/>

  </class>

</hibernate-mapping>

What's wrong with this?

var contacts = from Phone p in session.Query<Phone>()
               where p.Kontact.ContactName == contactName
               select p;

Note: I'm using NHibernate 3

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

慈悲佛祖 2024-10-12 16:33:17

将“来自电话 p”更改为“来自 p”

Change "from Phone p" to "from p"

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