2017-04-04 113 views
1

我想知道是否有可能以某種方式查詢我用作主鍵的類的某個屬性。 我的想法與下面的代碼類似。隨着_key.NAME我接受和錯誤消息,列不存在的,我試過TestOjectPK.NAME過,但沒有成功:Apache Ignite SQL查詢組合主鍵NET

var fieldsQuery = new SqlFieldsQuery("select _val,_key from TestObject where _key.NAME= ? and VALUE= ?", "name1","value"); 
IQueryCursor<IList> queryCursor = cache.QueryFields(fieldsQuery); 

這是我的課代表3列,他們在哪裏2某個表是PK。

class TestObjectPK : IBinarizable 
{ 
    private Int32 id; 
    private String name; 

    [QuerySqlField] 
    public string NAME 
    { 
     get{return this.name;} 
     set{this.name = value;} 
    } 

    [QuerySqlField] 
    public Int32 ID 
    { 
     get{return this.id;} 
     set{this.id = value;} 
    } 

    public void WriteBinary(IBinaryWriter writer) 
    { 
     writer.WriteInt("ID", ID); 
     writer.WriteString("NAME", NAME); 
    } 

    public void ReadBinary(IBinaryReader reader) 
    { 
     ID = reader.ReadInt("ID"); 
     NAME = reader.ReadString("NAME"); 
    } 

    public override int GetHashCode() 
    { 
     return id.GetHashCode() + name.GetHashCode(); 
    } 

    public override bool Equals(object obj) 
    { 
     return id == ((TestObjectPK)obj).id && name == ((TestObjectPK)obj).NAME; 
    } 
} 


class TestObject : IBinarizable 
{ 
    private String value2; 
    [QuerySqlField] 
    public String VALUE 
    { 
     get { return this.value2; } 
     set { this.value2 = value; } 
    } 


    public void WriteBinary(IBinaryWriter writer) 
    { 
     writer.WriteString("VALUE", VALUE); 
    } 

    public void ReadBinary(IBinaryReader reader) 
    { 
     VALUE = reader.ReadString("VALUE"); 
    } 
    public override int GetHashCode() 
    { 
     return VALUE.GetHashCode(); 
    } 

    public override bool Equals(object obj) 
    { 
     return VALUE == ((TestObject)obj).VALUE; 
    } 

} 

在此先感謝您。

回答

1

所有從鍵和值對象標記爲[QuerySqlField]的字段都直接在SQL表中結束。

因此正確的查詢是

select _val,_key from TestObject where NAME=? and VALUE=? 

還要確保用於SQL配置這兩個鍵和值類型:

new CacheConfiguration("TestObject", 
     new QueryEntity(typeof(TestObjectPK), typeof(TestObject))) 
+0

帕維爾我想你說的話。但Ignite仍會拋出與NAME列未找到相關的錯誤。也許這是和創建緩存時出錯? (新的CacheConfiguration(「TestObject」,typeof(TestObject)));} ICache cache = ignite.GetOrCreateCache TestObjectPK testObjectPK = new TestObjectPK(); testObjectPK.NAME =「h1」; testObjectPK.ID = 1; TestObject testObject = new TestObject(); testObject.VALUE =「value1」; (testObjectPK,testObject);' 謝謝 – John

+0

準確地說,要使用查詢中的鍵字段,您還必須指定鍵類型:'new CacheConfiguration(「TestObject」,new QueryEntity(typeof(TestObjectPK),typeof(TestObject )))'。我編輯了答案。 –

+1

謝謝。對於有同樣問題的人,我創建了一個帶有完整示例的GitHub存儲庫:https://github.com/juanpale/ExampleApacheIgniteQueryCompositeObjectNET – John