Visahan
Visahan

Reputation: 1192

Hibernate QuerySyntaxException, Table not mapped

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

Upvotes: 0

Views: 1275

Answers (2)

JB Nizet
JB Nizet

Reputation: 691635

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.

Upvotes: 2

bruno.zambiazi
bruno.zambiazi

Reputation: 1482

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

"from AdminRoles AR where AR.username = ?"

Upvotes: 1

Related Questions