我的查询返回一个空列表而不是一个条目

时间:2017-06-01 11:28:20

标签: mysql java-ee apache-cayenne

我正在尝试从表中获取用户: enter image description here

我通过表单保存了这些值(因此cayenne配置似乎很好)。

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,而是一个值(测试用户)。

我做错了什么?

谢谢,

1 个答案:

答案 0 :(得分:2)

Cayenne返回null而不是对象的原因是,在结果集中找不到PK字段或其值为NULL。 在你的情况下,我认为Cayenne在结果中找不到PK,因为它似乎被定义为userid id DB,而在Cayenne代码中我看到它是USERID

请让我给你一些关于你的代码的一般建议。 每个请求创建ServerRuntime并不是真正有效的解决方案,您应该将其保留为应用程序单例,有关详细信息,请参阅this question。此外,ObjectSelect(或SelectQuery如果您使用的是较旧版本的Cayenne)是一种更好(并且更灵活)的选择对象的方式。

你可以像这样使用它:

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