找到你要的答案

Q:Hibernate QuerySyntaxException, Table not mapped

Q:Hibernate querysyntaxexception,表映射

I'm following this Tutorial. I have added another DAO where i'm retrieving the admin_roles table. The method looks like this

public List findAllAdminRoleByUserName(String userName) {
    List<AdminRoles> users = new ArrayList<AdminRoles>();

    Query hqlQuery = sessionFactory.getCurrentSession().createQuery("from admin_roles AdminRoles where AdminRoles.username = ?");
    users =  hqlQuery.setString(0, userName).list();

    if (users.size() > 0) {
        return users;
    } else {
        return null;
    }
}

When I try to retrieve i'm getting the following error

HTTP Status 500 - Request processing failed; nested exception is org.hibernate.hql.internal.ast.QuerySyntaxException: admin_roles is not mapped [from admin_roles AdminRoles where AdminRoles.username = ?]

I am able to get values from the admin table mentioned in this tutorial, also I created some other tables from which i'm able to get values. But only this table is not being mapped. I also tried by changing the name of the table from "admin_roles" to adminroles(in the database and in code) I still get the same error.

The relevant class looks like this. Also the entity annotation is javax

@Entity
@Table(name = "admin_roles", uniqueConstraints = @UniqueConstraint(columnNames = { "role", "username" }))
public class AdminRoles{

Am I missing something? Thanks in advance

我遵循这个教程。我添加了一道在我的admin_roles检索表。这个方法看起来像这样

public List findAllAdminRoleByUserName(String userName) {
    List<AdminRoles> users = new ArrayList<AdminRoles>();

    Query hqlQuery = sessionFactory.getCurrentSession().createQuery("from admin_roles AdminRoles where AdminRoles.username = ?");
    users =  hqlQuery.setString(0, userName).list();

    if (users.size() > 0) {
        return users;
    } else {
        return null;
    }
}

当我试图取回我得到以下错误

HTTP Status 500 - Request processing failed; nested exception is org.hibernate.hql.internal.ast.QuerySyntaxException: admin_roles is not mapped [from admin_roles AdminRoles where AdminRoles.username = ?]

我可以从本教程中提到的管理表中获取值,我也创建了一些其他表格,从中我可以得到值。但只有这个表没有被映射。我也试图通过更改表的名称从“admin_roles”adminroles(在数据库和代码)我仍然得到同样的错误。

相关类看起来像这样。同时实体标注:

@Entity
@Table(name = "admin_roles", uniqueConstraints = @UniqueConstraint(columnNames = { "role", "username" }))
public class AdminRoles{

我错过了什么吗?先谢谢了。

answer1: 回答1:

You're confusing tables and entities. Tables are a relational database concept. They're mapped to entities, which are Java classes. HQL uses entities. Always. Never tables.

BTW, the message is not "Table not mapped". It's "admin_roles is not mapped". And that's very different. HQL uses entities, so it expects admin_roles in your query to be a mapped entity. Not a table name. And you don't have any entity named admin_roles.

The query should be

select ar from AdminRoles ar where ar.username = ?

That assumes there is a mapped field/property named username in the AdminRoles entity class, of course.

你混淆了表格和实体。表是关系数据库概念。他们映射到实体,这是java类。HQL用实体。总是。不表。

顺便说一句,这个消息不是“表映射”。它的“admin_roles不是映射”。这是非常不同的。HQL用实体,所以预计在你的查询admin_roles被映射的实体。不是表名。和你没有任何实体命名admin_roles。

查询应该是

select ar from AdminRoles ar where ar.username = ?

假设有一个映射字段/属性在AdminRoles实体类命名的用户名,当然。

answer2: 回答2:

You need to use the entity name in you query. Try like this:

"from AdminRoles AR where AR.username = ?"

您需要在查询中使用实体名。这样尝试:

"from AdminRoles AR where AR.username = ?"
mysql  sql  spring  hibernate