JPA查询异常

发布于 2024-09-26 11:05:35 字数 678 浏览 3 评论 0原文

我有一个使用 EntityManager 构建的查询:

Query q = em
    .createQuery("SELECT * FROM :table WHERE username = :username AND password = MD5(:password)")
    .setParameter("table", User.class.getName())
    .setParameter("username", txtLogin.getText())
    .setParameter("password", passPassword.getPassword())
;

User user = (User) q.getSingleResult();

但出现异常:

线程“AWT-EventQueue-0”中出现异常 java.lang.IllegalArgumentException:一个 创建时发生异常 在 EntityManager 中查询:
例外 描述:解析语法错误 查询 [SELECT * FROM :table WHERE 用户名 = :用户名和密码 = MD5(:密码)],第 1 行,第 7 列: 意外标记 [*]。

如何修复它?

查询中不能使用*吗?

I have a query builded with EntityManager:

Query q = em
    .createQuery("SELECT * FROM :table WHERE username = :username AND password = MD5(:password)")
    .setParameter("table", User.class.getName())
    .setParameter("username", txtLogin.getText())
    .setParameter("password", passPassword.getPassword())
;

User user = (User) q.getSingleResult();

but I get an exception:

Exception in thread "AWT-EventQueue-0"
java.lang.IllegalArgumentException: An
exception occurred while creating a
query in EntityManager:
Exception
Description: Syntax error parsing the
query [SELECT * FROM :table WHERE
username = :username AND password =
MD5(:password)], line 1, column 7:
unexpected token [*].

How to fix it ?

Is it impossible to use * in queries ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

生寂 2024-10-03 11:05:35

JPQL 语法与 SQL 不同,您可以

Select T from Thingy T

代替

Select * from Thingy

但这只是问题的一部分。 SELECT t FROM :table t 也不起作用,因为 from 子句中不允许使用参数,而只能在 where 子句中使用参数。因此,您必须这样做:

Query q = em
    .createQuery("SELECT u FROM " + User.class.getName()
    + "u WHERE username = :username AND password = MD5(:password)")
    .setParameter("username", txtLogin.getText())
    .setParameter("password", passPassword.getPassword())
    ;

此外,JPQL 中没有 MD5() 函数,因此要使用 MD5,您必须在 Java 代码中执行此操作,或者使用本机 SQL 查询。

JPQL syntax is different from SQL, you do

Select T from Thingy T

instead of

Select * from Thingy

But that's only part of your problem. SELECT t FROM :table t won't work either, as parameters are not allowed in the from clause, but only in the where clause. So you must do it something like this:

Query q = em
    .createQuery("SELECT u FROM " + User.class.getName()
    + "u WHERE username = :username AND password = MD5(:password)")
    .setParameter("username", txtLogin.getText())
    .setParameter("password", passPassword.getPassword())
    ;

Also, there is no MD5() function in JPQL, so to use MD5 you either have to do that in java code or use a native SQL query.

三寸金莲 2024-10-03 11:05:35

是的,你不能像这样使用 *

这是如何做到的。注意,即使 SELECT 也是可选的

 Query q = em
    .createQuery("FROM " + User.class.getName() + " WHERE username = :username AND password = MD5(:password)")
    .setParameter("username", txtLogin.getText())
    .setParameter("password", passPassword.getPassword())
    ;

User user = (User) q.getSingleResult();

,使用 SELECT 你可以这样做:

Query q = em
    .createQuery("SELECT us FROM " + User.class.getName() + "us WHERE username = :username AND password = MD5(:password)")
    .setParameter("username", txtLogin.getText())
    .setParameter("password", passPassword.getPassword())
    ;

Yes you cannot use * like that.

This is how to do it. Note even the SELECT is optional

 Query q = em
    .createQuery("FROM " + User.class.getName() + " WHERE username = :username AND password = MD5(:password)")
    .setParameter("username", txtLogin.getText())
    .setParameter("password", passPassword.getPassword())
    ;

User user = (User) q.getSingleResult();

With SELECT you can do like this:

Query q = em
    .createQuery("SELECT us FROM " + User.class.getName() + "us WHERE username = :username AND password = MD5(:password)")
    .setParameter("username", txtLogin.getText())
    .setParameter("password", passPassword.getPassword())
    ;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文