[英]Spring boot API criteria returning multiple times
I am beginning with spring boot and JPA 2.0. 我从Spring Boot和JPA 2.0开始。 My api runs a query from a mysql db that has a single record.
我的api从具有单个记录的mysql数据库运行查询。 For some reason the API is returning the query resultset nonstop.
由于某些原因,API会不停地返回查询结果集。
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. 有趣的是,如果我返回Vector的大小,它可以正常工作,但是当我返回List <>时,它会多次运行相同的代码。
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.
在第一个版本中,我在定义Client @entity的同一类中实现查询。 I have created a different class 'ClientRepository' and moved the query into it.
我创建了一个不同的类“ ClientRepository”,并将查询移入其中。 The problem stopped.
问题停止了。
I guess JPA doesn't like that... 我猜JPA不喜欢这样...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.