So,
I've been searching for examples of Hibernate and learning a little...
Now, I have some working examples with 2 simple tables with a OneToMany relation, using Hibernate 4.2.7 and Posgresql JDBC 9.2.
I'm facing some problems with load time.
The first problem that I encountered was a slow connection time at this line:
Quote:
Oct 31, 2013 3:07:35 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000046: Connection properties: {user=es2p1, password=****, autocommit=true, release_mode=auto}
I found a solution, was fixed adding this to the hibernate.cfg.xml:
Quote:
<!-- Avoid JDBC to load all MetaData -->
<property name="hibernate.temp.use_jdbc_metadata_defaults">false</property>
Now... the actual problem that I've not found a solution is on this line:
Quote:
INFO: HHH000399: Using default transaction strategy (direct JDBC transactions)
Oct 31, 2013 3:18:40 PM org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory <init>
INFO: HHH000397: Using ASTQueryTranslatorFactory
main.java: Starting insert at 2013/10/31 15:35:44
DepartamentoService.java: Inserting Record
DepartamentoService.java: session.beginTrasaction()
It takes at least 2 and a half minute to go on... immediately after it, all the queries are executed almost instantly.
Please take a loot at the printed times.
Quote:
main.java: Starting insert at 2013/10/31 15:35:44
DepartamentoService.java: Inserting Record
DepartamentoService.java: session.beginTrasaction()
DepartamentoService.java: session.save()
Hibernate: select nextval ('controle_academico_t01.departamento_id_seq')
DepartamentoService.java: tx.commit()
Hibernate: insert into controle_academico_t01.departamento (nome, id) values (?, ?)
DepartamentoService.java: Done
main.java: Ending insert at 2013/10/31 15:37:51
main.java: Starting insert at 2013/10/31 15:37:51
DepartamentoService.java: Inserting Record
DepartamentoService.java: session.beginTrasaction()
DepartamentoService.java: session.save()
Hibernate: select nextval ('controle_academico_t01.departamento_id_seq')
DepartamentoService.java: tx.commit()
Hibernate: insert into controle_academico_t01.departamento (nome, id) values (?, ?)
DepartamentoService.java: Done
main.java: Ending insert at 2013/10/31 15:37:51
main.java: Starting insert at 2013/10/31 15:37:51
main.java: Ending insert at 2013/10/31 15:37:51
main.java: Starting insert at 2013/10/31 15:37:51
main.java: Ending insert at 2013/10/31 15:37:51
main.java: Starting insert at 2013/10/31 15:37:51
main.java: Ending insert at 2013/10/31 15:37:51
My code structure:
Hibernate.cfg.xml:
Code:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
<property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/controleacademico</property>
<property name="hibernate.default_schema">controle_academico_t01</property>
<property name="hibernate.connection.username">es2p1</property>
<property name="hibernate.connection.password">123456</property>
<!-- Avoid JDBC to load all MetaData -->
<property name="hibernate.temp.use_jdbc_metadata_defaults">false</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Logging SQL to stdout -->
<property name="show_sql">true</property>
<!-- Mapping files -->
<mapping class="com.entities.Curso" />
<mapping class="com.entities.Departamento" />
</session-factory>
</hibernate-configuration>
The method being stucked (without println's):
Code:
public Integer adicionarDepartamento(Departamento departamento) {
session = sessionFactory.openSession();
Integer departamentoID = -1;
Transaction tx = null;
try{
tx = session.beginTransaction();
departamentoID = (Integer) session.save(departamento);
tx.commit();
}catch (HibernateException e) {
if (tx!=null) tx.rollback();
e.printStackTrace();
} finally {
session.close();
}
return departamentoID;
}
The sessionFactory comes from HibernateFactory...
Code:
public HibernateFactory() {
sessionFactory = null;
serviceRegistry = null;
try
{
// Hibernate Configuration
Configuration configuration = new Configuration();
configuration.configure();
serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
}
catch (Throwable ex)
{
System.err.println("Failed to create sessionFactory object."+ ex);
throw new ExceptionInInitializerError(ex);
}
}
public SessionFactory getSessionFactory() {
return this.sessionFactory;
}
}
My main (without println's):
Code:
public static void main(String[] args) {
// Hibernate configurator
HibernateFactory factory = new HibernateFactory();
Departamento departamento1 = new Departamento("Departamento 1");
Departamento departamento2 = new Departamento("Departamento 2");
DepartamentoService dService = new DepartamentoService(factory.getSessionFactory());
dService.adicionarDepartamento(departamento1);
dService.adicionarDepartamento(departamento2);
Curso curso1 = new Curso("Curso1", 10, departamento1);
Curso curso2 = new Curso("Curso2", 500, departamento1);
Curso curso3 = new Curso("Curso3", 100, departamento2);
CursoService cService = new CursoService(factory.getSessionFactory());
cService.adicionarCurso(curso1);
cService.adicionarCurso(curso2);
cService.adicionarCurso(curso3);
cService.listCursos();
}
If you need more data, please, ask me...
I'm stucked at this little problem.
Thank you in advance!