I'm new to Spring Framework and having troubles retrieving a native query result from my application. The application is supposed to retrieve some specific data from my PostgreSQL database and expose it as a REST resource. However, it seems not to be able to map the query results into the entity object i defined. The object is defined the same way the database is made: i've also used the same entity for the insert operation on my logger application.
I've created an Entity class:
package com.umeter.rest;
import ...
@Entity
@Table(name="dati_impianti")
public class DatiImpiantoEntity {
@Id
@GeneratedValue(strategy= GenerationType.IDENTITY)
private Integer id_lettura;
@Column(name="sigla_impianto")
private String sigla_impianto;
@Column(name="timestamp")
private Date timestamp;
@Column(name="lettura")
private Float lettura;
@Column(name="stato")
private Integer stato;
public Integer getId_lettura() {
return id_lettura;
}
public void setId_lettura(Integer id_lettura) {
this.id_lettura = id_lettura;
}
public String getSigla_impianto() {
return sigla_impianto;
}
//other Getters and Setters
A Repository class (with a random nativequery just to try it out).
EDIT: Yes, using the native query is needed because i will be using some native functionality from postgresql that are not available through the normal CRUD methods available. However, this seems not to be the issue as i tried with a "findAll()" method just to get the exact same [null] output
package com.umeter.rest;
import ...
@Repository
public interface DatiRepository extends CrudRepository<DatiImpiantoEntity, Integer> {
@Query(nativeQuery = true, value = "SELECT * from dati_impianti LIMIT 10")
public List<DatiImpiantoEntity> getIstantanea();
}
And a REST Controller:
package com.umeter.rest;
import ...
@RestController
@RequestMapping(path="/api")
public class DatiRestController {
@Autowired
private DatiRepository datiRepository;
@GetMapping(path="/istantanea")
public @ResponseBody List<DatiImpiantoEntity> getIstantanea() {
return datiRepository.getIstantanea();
}
}
However, the result i'm getting is a list of null values (i suppose Spring isn't able to map the query results into my Entity object):
[null,null,null,null,null,null,null,null,null,null]
I'm getting an output when i set the method with a generic "Object" return type (which is excluding a database connection or query problem). However, what i get is not a correct Json output format, and i know this is now how it's supposed to be:
[["MF1","2019-10-11T10:24:23.917+0000",3.814,6,null],["MF1","2019-10-11T10:24:33.867+0000",3.814,6,null],["MF1","2019-10-11T10:24:43.920+0000",3.814,6,null],["MF1","2019-10-11T10:24:53.884+0000",3.814,6,null],["MF1","2019-10-11T10:25:03.864+0000",3.814,6,null],["MF1","2019-10-11T10:25:13.869+0000",3.814,6,null],["MF1","2019-10-11T10:25:23.872+0000",3.814,6,null],["MF1","2019-11-08T13:17:35.175+0000",5511.1,0,null],["MF2","2019-11-08T13:17:35.174+0000",6567.025,0,null],["MF1","2019-11-08T13:25:25.655+0000",5548.027,0,null]]
For completeness purposes, i attach a picture of the database data structure (the id seems not to be really used but it was required by Spring for the insertion process):