I have a problem with hibernate that tells me that the 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 -> {
System.out.println(usersEntity.getName());
});
} finally {
session.close();
}
}
}
UsersEntity class
@Entity
@Table(name = "users", schema = "amazonviewer", catalog = "")
public class UsersEntity {
private int id;
private String name;
@Id
@Column(name = "id")
public int getId () {
return id;
}
public void setId (int id) {
this.id = id;
}
@Basic
@Column(name = "name")
public String getName () {
return name;
}
public void setName (String name) {
this.name = name;
}
@Override
public boolean equals (Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
UsersEntity that = (UsersEntity) o;
return id == that.id &&
Objects.equals(name, that.name);
}
@Override
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"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<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"/>
</session-factory>
</hibernate-configuration>
hibernate configurations class
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();
}
}
This error can be caused by many things, it explicitly tells you that it is not mapping its class
UsersEntity
, but even though it gives you this error, it is not necessarily so.Some solutions to this error are as follows:
In the Bean
@Entity
declared in your class, try placing the following:What you're doing is telling it explicitly that this is your entity class, which is what you're calling when you do the
createQuery()
.For your
createQuery("from UsersEntity").List()
, you must always keep in mind that what is called is the name of the class, its object, its entity to be clearer, the wrong case would be to call the database table, when you must use the entity class.You are important the wrong package, for example:
Should be
Try the following in your CreateQuery
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 must make sure that the class is being imported from its correct package, in the configuration file and in your class .
PS : If solution number four works for you, it will be a patch solution for the moment, but I recommend you not to do this, you can try and tell us how it goes, I have a question about whether you are using other configuration files, such as :
persistence.xml
or.hbm.xml
, if so, could you place them in the question, greetings.Hasdrubal.
Please check that the import annotation is javax.persistence instead of org.hibernate...
It just implements the Serializable interface to say that your entity can be serialized and derealized. Greetings.