java - retrieving List<Entity> from List<Object[]> JPA -
i have entity class calle publication, method called getallpublication return list<publication>
, query inside method has resultlist of type list<object[]>
, how can retrieve list of publication entity fromthe list<object[]>
: -here method :
public list<publication> getallpublication() { list<object[]> listepublication; query q; em.gettransaction().begin(); q=em.createquery("select c.titrepublication, c.datepublication, c.corps,p.login publication c join c.employee p "); listepublication = q.getresultlist(); //arraylist<publication> results = new arraylist<publication>(); //for (object[] resultat : listepublication) //results.add((publication) resultat[0]);*/ em.gettransaction().commit(); return results; }
thanks in advance. here entity class
package entities; import java.io.serializable; import javax.persistence.*; @entity @namedquery(name="publication.findall", query="select p publication p") public class publication { @id @column(name="\"idpublication\"") private integer idpublication; private string corps; @column(name="\"datepublication\"") private string datepublication; @column(name="\"titrepublication\"") private string titrepublication; @manytoone(fetch=fetchtype.lazy) @joincolumn(name="auteur") private employee employee; public publication() { } public integer getidpublication() { return this.idpublication; } public void setidpublication(integer idpublication) { this.idpublication = idpublication; } public string getcorps() { return this.corps; } public void setcorps(string corps) { this.corps = corps; } public string getdatepublication() { return this.datepublication; } public void setdatepublication(string datepublication) { this.datepublication = datepublication; } public string gettitrepublication() { return this.titrepublication; } public employee getemployee() { return employee; } public void setemployee(employee employee) { this.employee = employee; } public void settitrepublication(string titrepublication) { this.titrepublication = titrepublication; } }
try
public list<publication> getallpublication() { typedquery<publication> query = em. createnamedquery(publication.find_all, publication.class); return query.getresultlist(); }
and entity looks like:
package entities; import java.io.serializable; import javax.persistence.*; @entity @namedquery(name=publication.find_all, query="select p publication p") public class publication { public static final string find_all = "publication.findall"; @id @column(name="\"idpublication\"") private integer idpublication; private string corps; @column(name="\"datepublication\"") private string datepublication; @column(name="\"titrepublication\"") private string titrepublication; @manytoone(fetch=fetchtype.lazy) @joincolumn(name="auteur") private employee employee; public publication() { } public integer getidpublication() { return this.idpublication; } public void setidpublication(integer idpublication) { this.idpublication = idpublication; } public string getcorps() { return this.corps; } public void setcorps(string corps) { this.corps = corps; } public string getdatepublication() { return this.datepublication; } public void setdatepublication(string datepublication) { this.datepublication = datepublication; } public string gettitrepublication() { return this.titrepublication; } public employee getemployee() { return employee; } public void setemployee(employee employee) { this.employee = employee; } public void settitrepublication(string titrepublication) { this.titrepublication = titrepublication; } }
Comments
Post a Comment