Tengo un problema con hibernate que me dice que no estan mapeada las entidades. aqui esta el error:
INFO: HHH000397: Using ASTQueryTranslatorFactory
Exception in thread "main" java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: UsersEntity is not mapped [FROM UsersEntity ]
Clase Main
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();
}
}
}
Clase UsersEntity
@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);
}
}
Archivo cfg.xml
<?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>
Clase de configuraciones de hibernate
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();
}
}