2010-12-05 94 views
0

如何這NHibernate的HQL(作品)轉換......問題轉換到HQL Linq到NHibernate的

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到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

+1

變化 「從手機P」 到 「從對」 – Phill 2010-12-05 02:35:49

回答

5

「從手機P」 改爲 「從對」