Problem with hibernate entities


I have a problem with hibernate that tells me that entities are not mapped. Here is the error:

    INFO: HHH000397: Using ASTQueryTranslatorFactory
Exception in thread "main" java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: UsersEntity is not mapped [FROM UsersEntity ]

Main Class

    public class Main {
    private static final Session session;

    static {
        try {
            HibernateConnection hibernateConnection = new HibernateConnection();
            session = hibernateConnection.openSession();
        } catch (Throwable ex) {
            throw new ExceptionInInitializerError(ex);

    public static void main (final String[] args) throws Exception {
        try {
            List<UsersEntity> usersEntities = (List<UsersEntity>) session.createQuery("FROM UsersEntity ").list();
            usersEntities.forEach(usersEntity -> {
        } finally {

UsersEntity Class

@Table(name = "users", schema = "amazonviewer", catalog = "")
public class UsersEntity {
    private int id;
    private String name;

    @Column(name = "id")
    public int getId () {
        return id;

    public void setId (int id) { = id;

    @Column(name = "name")
    public String getName () {
        return name;

    public void setName (String name) { = name;

    public boolean equals (Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        UsersEntity that = (UsersEntity) o;
        return id == &&

    public int hashCode () {
        return Objects.hash(id, name);

cfg.xml file

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD//EN"
        <property name="connection.url">jdbc:mysql://localhost:3306</property>
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <mapping class="com.ahernand.entity.MaterialEntity"/>
        <mapping class="com.ahernand.entity.MoviesEntity"/>
        <mapping class="com.ahernand.entity.UsersEntity"/>
        <mapping class="com.ahernand.entity.ViewedEntity"/>

Class of hibernate configurations

public class HibernateConnection {

    private String DB_DRIVER_NAME   = "com.mysql.jdbc.Driver";
    private String DB_URL           = "jdbc:mysql://localhost:3306/amazonviewer";
    private String DB_USERNAME      = "root";
    private String DB_PASSWORD      = "Admin123!";
    private String DIALECT          = "org.hibernate.dialect.MySQLDialect";
    private String SHOW_SQL         = "true";

    private static Configuration config;
    private SessionFactory sessionFactory;
    private Session session;

    public HibernateConnection () {
        config = new Configuration();

        config.setProperty("hibernate.connector.driver_class", DB_DRIVER_NAME);
        config.setProperty("hibernate.connection.url", DB_URL);
        config.setProperty("hibernate.connection.username", DB_USERNAME);
        config.setProperty("hibernate.connection.password", DB_PASSWORD);
        config.setProperty("hibernate.dialect", DIALECT);
        config.setProperty("hibernate.show_sql", SHOW_SQL);

         * Config connection pools
        config.setProperty("hibernate.c3p0.min_size", "5");
        config.setProperty("hibernate.c3p0.max_size", "20");
        config.setProperty("hibernate.c3p0.timeout", "300");
        config.setProperty("hibernate.c3p0.max_statements", "50");
        config.setProperty("hibernate.c3p0.idle_test_period", "3000");

        this.sessionFactory = config.buildSessionFactory();


    public Session openSession() throws HibernateException {

        if (session == null) {
            this.session = this.sessionFactory.openSession();

        return this.session;


    public void reconnect() throws HibernateException {
        this.sessionFactory = config.buildSessionFactory();

asked by Asdrubal Hernandez 22.10.2018 в 01:28

3 answers


This error can be caused by many things, explicitly tells you that you are not mapping your class UsersEntity , but even though you of that error is not necessarily so.

Some solutions to this error are the following:

  • In the Bean @Entity declared in your class try placing the following:

  • What you do is tell him explicitly that this is your entity class, which is what you are calling when you do the createQuery() .

  • For your createQuery("from UsersEntity").List() , you should always keep in mind that what is called is the name of the class, its object, its entity to be more clear, the erroneous case would be to call the base table of data, when you should use the entity class.

  • You are importing the wrong package, for example:

    import org.hibernate.annotations.Entity; (Incorrecto)

    It should be

    import javax.persistence.Entity; (Correcto)
  • Try the following in your CreateQuery query

      session.createQuery("from com.ahernand.entity.UsersEntity") 

    What you do in this case is to use the full path of your object, this can certainly work but it is not the best practice, you should be careful that the class is being imported from its correct package, in the configuration file and in your class.

  • PD : If the solution number four is useful for this, it will be a patch solution for the moment, but I recommend you do not do this, you can try and tell us how you are doing, I have a question about whether you are occupying other configuration files more like for example: persistence.xml or .hbm.xml , if so you could place them in the question, greetings.

    answered by 25.10.2018 в 04:17


    Please verify that the import of the annotation is javax.persistence instead of org.hibernate ...

    import javax.persistence.Entity;
    answered by 25.10.2018 в 15:01

    Only implement the Serializable interface to say that your entity can be serialized and unrealized. Greetings.

    public class UsersEntity implements Serializable { ... }
    answered by 30.10.2018 в 04:46