I am beginning with spring boot and JPA 2.0. My api runs a query from a mysql db that has a single record. For some reason the API is returning the query resultset nonstop.
Funny is that if I return the size of the Vector it works fine, but when I return the List<> it runs multiple times the same code.
I suspect that I might have to set some flag for connection stoppage or something. Any ideas of what might be causing this?
//controller
@RestController
public class ClientController {
@RequestMapping("/client")
public List<Client> client(@RequestParam(value="name", defaultValue="World") String name) {
Client clientModel = new Client();
List<Client> clients = clientModel.getClients();
//return clients.size();
return clients;
}
}
//model
public List<Client> getClients() {
try {
EntityManagerFactory emf = Persistence
.createEntityManagerFactory("CRM");
EntityManager em = emf.createEntityManager();
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Client> q = cb.createQuery(Client.class);
Root<Client> c = q.from(Client.class);
q.select(c);
List<Client> clients = em.createQuery(q).getResultList();
// ArrayList<Client> clients = (ArrayList<Client>) q.select(c);
em.close();
emf.close();
return clients;
} catch (Exception e) {
throw e;
}
}
Didn't find the root cause of the problem but found a solution. In the first version I was implementing the query in the same class where I defined my Client @entity. I have created a different class 'ClientRepository' and moved the query into it. The problem stopped.
I guess JPA doesn't like that...
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.