Я работаю над приложением, которое помогает пользователю проверять свои "bank accounts"
и переходить с учетной записи на другую через приложение. Я использую веб-службу отдыха для выполнения операций sql. И у меня проблема в функции, которая переводится из одной учетной записи в другую. Этот метод должен обновить таблицу "compte"
(банковский счет), которая содержит следующие столбцы: "id"
, "currencyId"
, "clientId"
, "montant"
(сумма).
@GET
@Path("transaction/{id1}/{id2}/{amount}")
@Produces({"application/xml", "application/json"})
public String Transfer(@PathParam("id1") String compte1 ,@PathParam("id2") String compte2,@PathParam("amount") int amount ) {
Compte c1 = new Compte();
c1.setId(compte1);
c1=(Compte) (em.createQuery("select montant from Compte where id=:id").setParameter("id", c1).getSingleResult());
c1.setMontant(c1.getMontant()-amount);
Query query1=em.createQuery("update compte set montant=:montantVal where compte.id=:compte1");
query1.setParameter("montantVal", c1);
query1.setParameter("compte1", c1);
query1.executeUpdate();
Compte c2 = new Compte();
c2.setId(compte2);
c2.setMontant(em.createQuery("select montant from compte where id=:id").setParameter("id", c2).getFirstResult());
c2.setMontant(c2.getMontant()+amount);
Query query2=em.createQuery("update compte set montant=:montantVal where compte.id=':compte2'");
query2.setParameter("montantVal", c2);
query2.setParameter("compte2", c2);
query2.executeUpdate();
Этот код дает следующую ошибку:
Exception [EclipseLink-0] (Eclipse Persistence Services - 2.5.0.v20130507-3faac2b): org.eclipse.persistence.exceptions.JPQLException
Exception Description: Syntax error parsing [select montant from Compte where id=:id].
[27, 27] An identification variable must be provided for a range variable declaration.
проблема заключается в том, что em.createQuery
использует инструкцию строки JPQL, поэтому она должна быть
em.createQuery("select c.montant from Compte c where c.id=:id")
Где Compte - это имя класса, хотя на вашем порту непонятно имя ваших сущностей, я предполагаю, что у вас есть что-то вроде этого.
@Entity
public class Compte{
@Id
private int id;
private int currencyId;
private double montant;
}
помните, что JPQL использует имя сущностей и свойств, а не имя таблиц и столбцов.
Для объявления переменной диапазона должна быть указана идентификационная переменная.
Определяется как c в запросе выше.
select montant from Compte c where c.id=:id