java ee - NamedQuery resultList returning objects, but all values are null -
i have table in database these columns:
id serial not null, name character varying(255) not null, url character varying(255)
the entity:
@entity @table(name = "topmenu") @xmlrootelement @namedqueries({ @namedquery(name = "topmenu.findall", query = "select t topmenu t"), @namedquery(name = "topmenu.findbyid", query = "select t topmenu t t.id = :id")}) public class topmenu implements serializable { private static final long serialversionuid = 1l; @id @generatedvalue(strategy = generationtype.identity) @basic(optional = false) @column(name = "id") private integer id; @basic(optional = false) @column(name = "name") private string name; @column(name = "url") private string url; public topmenu() { } public topmenu(integer id) { this.id = id; } public topmenu(integer id, string name) { this.id = id; this.name = name; } public integer getid() { return id; } public void setid(integer id) { this.id = id; } public string getname() { return name; } public void setname(string name) { this.name = name; } public string geturl() { return url; } public void seturl(string url) { this.url = url; } @override public int hashcode() { int hash = 0; hash += (id != null ? id.hashcode() : 0); return hash; } @override public boolean equals(object object) { // todo: warning - method won't work in case id fields not set if(!(object instanceof topmenu)) { return false; } topmenu other = (topmenu) object; return !((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))); } @override public string tostring() { return "erates.entity.topmenu[ id=" + id + " ]"; } }
netbeans generated jpa entity me. have 5 records in database. when em.createnamedquery("topmenu.findall").getresultlist();
list<topmenu>
5 entries, when following, nothing returned:
for(topmenu t : em.createnamedquery("topmenu.findall").getresultlist()){ logger.warn(t.getname()); }
the named query:
@namedquery(name = "topmenu.findall", query = "topmenu t topmenu t")
output:
2015-06-01 11:23:27,024 warn erates.beans.topmenubean (topmenubean.java:33) - 2015-06-01 11:23:27,034 warn erates.beans.topmenubean (topmenubean.java:33) - 2015-06-01 11:23:27,045 warn erates.beans.topmenubean (topmenubean.java:33) - 2015-06-01 11:23:27,055 warn erates.beans.topmenubean (topmenubean.java:33) - 2015-06-01 11:23:27,066 warn erates.beans.topmenubean (topmenubean.java:33) -
in experience, problems occur when don't used typedqueries , need them be.
try code with
for(topmenu t : em.createnamedquery("topmenu.findall", topmenu.class).getresultlist()){ logger.warn(t.getname()); }
with additional parameter, createnamedquery return typedquery instead of query.
let me know if helps :)
Comments
Post a Comment