我有一个名为 Pilot
的实体类,具有以下定义的命名查询:
@NamedQuery(name="Pilot.findById", query="SELECT p FROM Pilot p where p.id = :id")
另一个名为 Flight
的命名查询:
@NamedQuery(name="Flight.findById", query="SELECT f FROM Flight f where f.id = :id")
然后我有一个方法可以执行以下操作:
TypedQuery<Flight> fQuery = em.createNamedQuery("Flight.findById", Flight.class);
fQuery.setParameter("id", Integer.parseInt(flightId));
Flight f = fQuery.getSingleResult();
TypedQuery<Pilot> pQuery = em.createNamedQuery("Pilot.findById", Pilot.class);
fQuery.setParameter("id", Integer.parseInt(pilotId));
Pilot p = pQuery.getSingleResult();
但由于某种原因,当我调用它时,我收到错误:
...
Caused by: java.lang.IllegalStateException: Query argument id not found in the list of parameters provided during query execution.
...
可能是什么问题?
最佳答案
看起来像是剪切和粘贴错误...
TypedQuery<Pilot> pQuery = em.createNamedQuery("Pilot.findById", Pilot.class);
fQuery.setParameter("id", Integer.parseInt(pilotId));
^^^^^^
您在 fQuery
上设置了两次 ID。尝试将飞行员更改为 pQuery
。像这样:
TypedQuery<Pilot> pQuery = em.createNamedQuery("Pilot.findById", Pilot.class);
pQuery.setParameter("id", Integer.parseInt(pilotId));
^^^^^^
关于Java JPA - "Query argument id not found in the list of parameters provided during query execution",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45379818/