I'm trying to get my user from the table :
I saved this values via a form (so the cayenne configuration seems to be good).
the UserInfoFactory class is :
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);
}
}
My _UserInfo class is :
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);
}
}
I'm calling the method like that :
List<UserInfoFactory> users = UserInfoFactory.getUsersInfo();
But the users list return null.
That should not be null but one value (test user).
What did I do wrong?
Thanks,
Reason for Cayenne to return null
instead of an object is that PK field either not found in the result set or its value is NULL
.
In your case I think that Cayenne can't find PK in the result as it seems that it's defined as userid
id DB and in Cayenne code I see it's USERID
.
And please let me give you some general advise about your code. Creating ServerRuntime per request is not really effective solution, you should keep it as application singleton, see this question for details. Moreover ObjectSelect (or SelectQuery if you are using older version of Cayenne) is a better (and really more flexible) way of selecting objects.
You can use it like this:
List<UserInfoFactory> result = ObjectSelect.query(UserInfoFactory.class)
.select(context);