I have an Entity class calle Publication, a method called getAllPublication that return List<Publication>
, but my query inside the method has a resultList of type List<Object[]>
, how can I retrieve a list of publication entity fromthe List<Object[]>
: -here the 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 FROM 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 is the entity class
package entities;
import java.io.Serializable;
import javax.persistence.*;
@Entity
@NamedQuery(name="Publication.findAll", query="SELECT p FROM 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 your Entity looks like:
package entities;
import java.io.Serializable;
import javax.persistence.*;
@Entity
@NamedQuery(name=Publication.FIND_ALL, query="SELECT p FROM 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;
}
}
You need to create a typed query by doing:
TypedQuery<Publication> q;
em.getTransaction().begin();
q=em.createQuery("SELECT c.titrePublication,
c.datePublication, c.corps,p.login FROM Publication c JOIN c.employee p ",
Publication.class);
Edit: as the other answers indicate, the query needs to be changed too to return all that is in the Publication table.
JPA provides an SqlResultSetMapping and resultClass
that allows you to map whatever returns from your native query into an Entity
You have to define Query NamedNativeQuery
in your Publication.
@NamedNativeQuery(name="findPublication", query="SELECT c.titrePublication, c.datePublication, c.corps,p.login FROM Publication c JOIN c.employee p", resultClass=Publication.class)
login property is not define in Publication class, you need add login peroperty in Publication with getter/setter because your selecting login from employee table using join
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.