I finally answered my question :))
I was debugging a lot and errors are not thrown (I don't know why)...
I had to create an hibernate.cfg.xml
that looks like this:
<?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>
<!-- JDBC connection pool (use the built-in) -->
<property name="hibernate.connection.pool_size">1</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="hibernate.current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="hibernate.cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="hibernate.show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hibernate.hbm2ddl.auto">update</property>
</session-factory>
</hibernate-configuration>
and then in code I used like this
public DBReaderImpl(DataSource source)
{
this.source = source;
Properties hibernateProperties = new Properties();
hibernateProperties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
hibernateProperties.setProperty("hibernate.connection.driver_class", "com.mysql.jdbc.Driver");
hibernateProperties.setProperty("hibernate.connection.url", "jdbc:mysql://" + source.getHost() + "/" + source.getDataBase());
hibernateProperties.setProperty("hibernate.connection.username", source.getUsername());
hibernateProperties.setProperty("hibernate.connection.password", source.getPassword());
hibernateProperties.setProperty("hibernate.hbm2ddl.auto", "validate");
Configuration configuration = new Configuration();
configuration.setProperties(hibernateProperties);
configuration.addAnnotatedClass(Route.class);
SessionFactory sessionFactory = configuration.configure().buildSessionFactory(
new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry());
Session session = sessionFactory.openSession();
session.beginTransaction();
List<Route> routes = session.createQuery("SELECT r FROM Route r").list();
for (Route route : routes)
{
System.out.println(route);
}
session.close();
}
Perhaps I will use reflection to read all my @Entity
classes to add all annotated classes to my configuration