ai didi

variables - 语法错误解析 JPQL : An identification variable must be provided for a range variable declaration

转载 作者:行者123 更新时间:2023-12-01 08:53:51 24 4
gpt4 key购买 nike

尝试运行此代码时:

List<Users> list = em.createQuery("select balance b from Users where b.userName = '" + user_name.getText() +"'", Users.class).getResultList();

我遇到此错误消息:

Syntax error parsing [select balance b from Users where b.userName = 'a']. [28, 28] An identification variable must be provided for a range variable declaration.



我想从在 user_name 文本字段中输入名称的用户那里检索整数余额。
我是这个主题的初学者,互联网提供了复杂的信息。有人可以请解释什么是错的吗?

用户类的一部分
@Entity
@Table(name = "users")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "Users.findAll", query = "SELECT u FROM Users u"),
@NamedQuery(name = "Users.findByUserName", query = "SELECT u FROM Users u WHERE u.userName = :userName"),
@NamedQuery(name = "Users.findByBalance", query = "SELECT u FROM Users u WHERE u.balance = :balance"),
@NamedQuery(name = "Users.findByFirstName", query = "SELECT u FROM Users u WHERE u.firstName = :firstName"),
@NamedQuery(name = "Users.findByLastName", query = "SELECT u FROM Users u WHERE u.lastName = :lastName"),
@NamedQuery(name = "Users.findByIban", query = "SELECT u FROM Users u WHERE u.iban = :iban"),
@NamedQuery(name = "Users.findByCharacterSlots", query = "SELECT u FROM Users u WHERE u.characterSlots = :characterSlots"),
@NamedQuery(name = "Users.findByLastPayment", query = "SELECT u FROM Users u WHERE u.lastPayment = :lastPayment"),
@NamedQuery(name = "Users.findByMonthsPayed", query = "SELECT u FROM Users u WHERE u.monthsPayed = :monthsPayed"),
@NamedQuery(name = "Users.findByPassword", query = "SELECT u FROM Users u WHERE u.password = :password"),
@NamedQuery(name = "Users.findByBanned", query = "SELECT u FROM Users u WHERE u.banned = :banned")})
public class Users implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Basic(optional = false)
@Column(name = "user_name")
private String userName;
@Column(name = "balance")
private Integer balance;
@Column(name = "first_name")
private String firstName;
@Column(name = "last_name")
private String lastName;
@Column(name = "iban")
private String iban;
@Column(name = "character_slots")
private Integer characterSlots;
@Column(name = "last_payment")
@Temporal(TemporalType.DATE)
private Date lastPayment;
@Column(name = "months_payed")
private Integer monthsPayed;
@Column(name = "password")
private String password;
@Column(name = "banned")
private Boolean banned;
@ManyToMany(mappedBy = "usersCollection")
private Collection<Servers> serversCollection;
@ManyToMany(mappedBy = "usersCollection")
private Collection<Characters> charactersCollection;

public Users() {
}

public Users(String userName, Integer balance, String firstName, String lastName, String iban, Integer characterSlots, Date lastPayment, Integer monthsPayed, String password, Boolean banned) {
this.userName = userName;
this.balance = balance;
this.firstName = firstName;
this.lastName = lastName;
this.iban = iban;
this.characterSlots = characterSlots;
this.lastPayment = lastPayment;
this.monthsPayed = monthsPayed;
this.password = password;
this.banned = banned;
}

最佳答案

在查询用户实体时,您应该声明一个标识变量。您正在查询 Users 实体,因此变量声明应该在 User 而不是 balance 属性上。

此外,您还想检索整数类型的用户实体的余额属性。因此,泛型的使用应该正确地指明正确的类型。

试试这个:

List<Integer> list = em.createQuery("select u.balance from Users u where u.userName = '" + user_name.getText() +"'", Integer.class).getResultList();

或者,如果只预期单个结果:
Integer balance = em.createQuery("select u.balance from Users u where u.userName = '" + user_name.getText() +"'", Integer.class).getSingleResult();

防止 SQL 注入(inject)攻击的更正确的实现是使用参数化查询:
TypedQuery<Integer> query = em.createQuery("select u.balance from Users u where u.userName = :user_name", Integer.class);
query.setParameter("user_name", user_name.getText());
Integer balance = query.getSingleResult();

关于variables - 语法错误解析 JPQL : An identification variable must be provided for a range variable declaration,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32956735/

24 4 0
文章推荐: amazon-web-services - 在 Amazon 的 CodeDeploy 上获取成功/失败构建的通知
文章推荐: python - Flask - 多个文件夹中的应用程序
文章推荐: python - 是否可以将关系数据库表存储为 Elasticsearch 中的不同索引如果是,那么如何在索引中传递关系
文章推荐: python - Azure Web 应用服务上的 Anaconda 安装
行者123
个人简介

我是一名优秀的程序员,十分优秀!

滴滴打车优惠券免费领取
滴滴打车优惠券
全站热门文章
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com