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
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
In your Query you have to use class name(User) not table name(users) so your query is "from User"
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;
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 {
// ...
}
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
}
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
.