Java JPA 查询不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20873359/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 04:55:44  来源:igfitidea点击:

JPA Query does not work

javajpa

提问by Mato

Can you help me please with this problem? I want use this method for find the specific nick in my database (It made with Apache Derby). I have used the EntityManager and mapping Persistence - Entity classes from database in the NetBeans.

你能帮我解决这个问题吗?我想使用这种方法在我的数据库中找到特定的昵称(它是用 Apache Derby 制作的)。我在 NetBeans 中使用了 EntityManager 和映射持久性 - 来自数据库的实体类。

public static boolean findByNick(String nick) {

    List<eng.db.User> ret;
    EntityManager em = getEntityManager();

    Query q = em.createQuery("SELECT * FROM User u WHERE u.nick =:nick");
    q.setParameter("nick", nick);
    ret = q.getResultList();
    em.close();

    boolean hodnota = false;

    if (ret.isEmpty()) {
        hodnota = true;
    }

    return hodnota;
}

I get this error:

我收到此错误:

java.lang.IllegalArgumentException: An exception occurred while creating a query in EntityManager:
Exception Description: Syntax error parsing [SELECT * FROM User u WHERE u.nick =:nick)].
[21, 21] A select statement must have a FROM clause.
[7, 7] The left expression is missing from the arithmetic expression.
[9, 20] The right expression is not an arithmetic expression.
[41, 42] The query contains a malformed ending.

java.lang.IllegalArgumentException:在 EntityManager 中创建查询时发生
异常:异常描述:语法错误解析 [SELECT * FROM User u WHERE u.nick =:nick)]。
[21, 21] select 语句必须有一个 FROM 子句。
[7, 7] 算术表达式中缺少左表达式。
[9, 20] 正确的表达式不是算术表达式。
[41, 42] 查询包含格式错误的结尾。

Where is the problem please?

请问问题出在哪里?

回答by Diversity

It should be

它应该是

UPDATE

更新

according to your comment now the query should be:

根据您现在的评论,查询应该是:

SELECT u.userID, u.enterKey, u.nick 
FROM User u
WHERE u.nick = ?

or with named param

或使用命名参数

SELECT u.userId, u.enterKey, u.nick 
FROM User u
WHERE u.nick = :nick

where usereID, enterKey and nick are fields(properties) of your entity type User.

其中 usereID、enterKey 和 nick 是您的实体类型 User 的字段(属性)。

Your User class should look pretty much like this

你的 User 类应该看起来像这样

@Entity
public class User {

      @Id
      private long userId;

     @column(name="EnterKey"
      private String enterKey;

      @column(name="nick")
      private String nick;

      // setter getter
}

Regard that this hql and you use the class and property names of the object model which you defined as arguments for the query.

考虑到此 hql 和您使用定义为查询参数的对象模型的类和属性名称。

in your query Hibernate or any other JPA implementation creates a list of objects of type User using the mapping you defined. The expression *can not be associated with an object name of this type. The equivalent to *in sql is in object related query languages just the of the alias of the from clause in your case "u" since the from clause looks like this:

在您的查询 Hibernate 或任何其他 JPA 实现中,使用您定义的映射创建用户类型的对象列表。表达式*不能与此类型的对象名称相关联。*在 sql 中的等价物在对象相关的查询语言中只是 from 子句在您的案例“u”中的别名,因为 from 子句如下所示:

From User u

If you want just to select separate fields of your Entity you have to declare

如果您只想选择实体的单独字段,则必须声明

 Select alias.property
 from Entity alias

or in your special case

或者在你的特殊情况下

 Select u.nick
 From User u

In this case instances of type User are created and the field nick is set.

在这种情况下,创建了 User 类型的实例并设置了字段 nick。

回答by Glenn Lane

If nickis the primary key of your entity (@Id), then use:

如果nick是实体的主键 ( @Id),则使用:

return em.find(eng.db.User, nick) != null;

Or if not:

或者,如果不是:

Query q = em.createQuery("SELECT TRUE FROM User u WHERE u.nick =:nick");
q.setParameter("nick", nick);
return !q.getResultList().isEmpty();

By returning a simple boolean, you minimize the DB response.

通过返回一个简单的布尔值,您可以最小化数据库响应。