2017-06-01 54 views
0

我想從表中得到我的用戶: enter image description here我的查詢返回一個空列表,而不是一個條目

我通過表單保存此值(所以卡宴配置似乎也不錯)。

的UserInfoFactory類:

public class UserInfoFactory extends _UserInfo implements Serializable { 
    private static final long serialVersionUID = 1L; 

    public static ObjectContext getContext() { 
     @SuppressWarnings("deprecation") 
     ServerRuntime runtime = new ServerRuntime("cayenne-myapplication.xml"); 
     return runtime.newContext(); 
    } 

    @SuppressWarnings("unchecked") 
    public static List<UserInfoFactory> getUsersInfo() { 

     SQLTemplate select = new SQLTemplate(UserInfoFactory.class, 
       "SELECT * FROM mam.userinfo"); 

     return getContext().performQuery(select); 
    } 
} 

我_UserInfo類是:

package com.example.myapplication.model; 

import org.apache.cayenne.CayenneDataObject; 

/** 
* Class _UserInfo was generated by Cayenne. 
* It is probably a good idea to avoid changing this class manually, 
* since it may be overwritten next time code is regenerated. 
* If you need to make any customizations, please use subclass. 
*/ 
public abstract class _UserInfo extends CayenneDataObject { 

    public static final String ADDRESS_PROPERTY = "address"; 
    public static final String EMAIL_PROPERTY = "email"; 
    public static final String FIRSTNAME_PROPERTY = "firstname"; 
    public static final String LASTNAME_PROPERTY = "lastname"; 
    public static final String PHONENUMBER_PROPERTY = "phonenumber"; 
    public static final String USERPASSWORD_PROPERTY = "userpassword"; 
    public static final String USERPRIVILEGES_PROPERTY = "userprivileges"; 

    public static final String USERID_PK_COLUMN = "USERID"; 

    public void setAddress(String address) { 
     writeProperty(ADDRESS_PROPERTY, address); 
    } 
    public String getAddress() { 
     return (String)readProperty(ADDRESS_PROPERTY); 
    } 

    public void setEmail(String email) { 
     writeProperty(EMAIL_PROPERTY, email); 
    } 
    public String getEmail() { 
     return (String)readProperty(EMAIL_PROPERTY); 
    } 

    public void setFirstname(String firstname) { 
     writeProperty(FIRSTNAME_PROPERTY, firstname); 
    } 
    public String getFirstname() { 
     return (String)readProperty(FIRSTNAME_PROPERTY); 
    } 

    public void setLastname(String lastname) { 
     writeProperty(LASTNAME_PROPERTY, lastname); 
    } 
    public String getLastname() { 
     return (String)readProperty(LASTNAME_PROPERTY); 
    } 

    public void setPhonenumber(Integer phonenumber) { 
     writeProperty(PHONENUMBER_PROPERTY, phonenumber); 
    } 
    public Integer getPhonenumber() { 
     return (Integer)readProperty(PHONENUMBER_PROPERTY); 
    } 

    public void setUserpassword(String userpassword) { 
     writeProperty(USERPASSWORD_PROPERTY, userpassword); 
    } 
    public String getUserpassword() { 
     return (String)readProperty(USERPASSWORD_PROPERTY); 
    } 

    public void setUserprivileges(Short userprivileges) { 
     writeProperty(USERPRIVILEGES_PROPERTY, userprivileges); 
    } 
    public Short getUserprivileges() { 
     return (Short)readProperty(USERPRIVILEGES_PROPERTY); 
    } 

} 

我打電話這樣的方法:

List<UserInfoFactory> users = UserInfoFactory.getUsersInfo(); 

但用戶列表返回NULL 。

enter image description here 這不應該是null,而是一個值(測試用戶)。

我做錯了什麼?

謝謝,

回答

2

原因卡宴返回null代替一個目的是,PK場或者在結果集中未發現,或者其值NULL。 在你的情況下,我認爲卡宴無法找到結果中的PK,因爲它似乎被定義爲userid id DB,在Cayenne代碼中,我看到它是USERID

請讓我給你一些關於你的代碼的一般建議。 爲每個請求創建ServerRuntime並不是真正有效的解決方案,您應該將其保留爲應用程序單例,有關詳細信息,請參閱this question。此外,ObjectSelect(或SelectQuery,如果您使用舊版Cayenne)是一種更好(更靈活)的選擇對象的方式。

您可以使用它像這樣:

List<UserInfoFactory> result = ObjectSelect.query(UserInfoFactory.class) 
            .select(context); 
+0

完美!我非常感謝這個決議,我花了太多時間。我期待像'unreconize USERID'之類的錯誤。感謝您的改進。我會盡力去做! – Bob

+1

@FannyV歡迎您!至於'unreconize USERID',這是使用SLQTemplate時遇到的一個問題,它試圖盡力而爲,但默默地失敗。 – Nikita

+0

關於你的好建議的問題(感謝那個,對不起,我需要改進/學習'最佳實踐'):如果我想選擇一個計數(列名),ObjectSelect或SelectQuery是可以的?我應該隨時選擇PK,對吧?在這種情況下,我應該使用SQL模板嗎?我什麼時候可以使用SQL模板? – Bob

相關問題