Hibernate error - QuerySyntaxException: users is not mapped [from users]

前端 未结 19 2206
感动是毒
感动是毒 2020-11-30 20:14

I\'m trying to get a list of all the users from \"users\" table and I get the following error:

org.hibernate.hql.internal.ast.QuerySyntaxException: users is          


        
相关标签:
19条回答
  • 2020-11-30 21:07

    I got this issue when i replaced old hibernate-core library with hibernate-core-5.2.12. However all my configuration was ok. And i fixed this issue by creating sessionfactory this way:

    private static SessionFactory buildsSessionFactory() {
        try {
            if (sessionFactory == null) {
                StandardServiceRegistry standardRegistry = new StandardServiceRegistryBuilder()
                        .configure("/hibernate.cfg.xml").build();
                Metadata metaData = new MetadataSources(standardRegistry)
                        .getMetadataBuilder().build();
                sessionFactory = metaData.getSessionFactoryBuilder().build();
            }
            return sessionFactory;
        } catch (Throwable th) {
    
            System.err.println("Enitial SessionFactory creation failed" + th);
    
            throw new ExceptionInInitializerError(th);
    
        }
    }
    

    Hope it helps someone

    0 讨论(0)
  • 2020-11-30 21:08

    In your Query you have to use class name(User) not table name(users) so your query is "from User"

    0 讨论(0)
  • 2020-11-30 21:11

    You must type in the same name in your select query as your entity or class(case sensitive) . i.e. select user from className/Entity Name user;

    0 讨论(0)
  • 2020-11-30 21:11

    I also came across this issue while using the Quarkus microservice framework:

    public class SomeResource {
    
      @GET
      @RolesAllowed({"basic"})
      public Response doSomething(@Context SecurityContext context) {
        // ...
      }
    }
    
    // this will generate an QuerySyntax exception, as the authorization module
    // will ignore the Entity annotation and use the class name instead.
    @Entity(name = "users")
    @UserDefinition
    public class User {
      // ...
    }
    
    // do this instead
    @Entity
    @Table(name = "users")
    @UserDefinition
    public class User {
      // ...
    }
    
    0 讨论(0)
  • 2020-11-30 21:13

    I recommend this pattern:

    @Entity(name = User.PERSISTANCE_NAME)
    @Table(name = User.PERSISTANCE_NAME )
    public class User {    
        static final String PERSISTANCE_NAME = "USER";
    
        // Column definitions here
    
    }
    
    0 讨论(0)
  • 2020-11-30 21:14

    with org.hibernate.hql.internal.ast.QuerySyntaxException: users is not mapped [from users], you are trying to select from the users table. But you are annotating your class with @Table( name = "Users" ). So either use users, or Users.

    0 讨论(0)
提交回复
热议问题