I do not know if this is normal, but Hibernate starts the process, inserts the object into the database, but its java process is open.
What do I have to do to make this process stop automatically after insertion?
Here is a code that exemplifies the problem:
TestHibernate.java
package testehibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
public class TesteHibernate
{
public static void main(String[] args)
{
Configuration configuration = new Configuration().configure();
StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties());
SessionFactory factory = configuration.buildSessionFactory(builder.build());
Session session = factory.openSession();
Teste teste = new Teste();
teste.setNome("Alexandre");
teste.setEmail("[email protected]");
session.beginTransaction();
session.save(teste);
session.getTransaction().commit();
session.close();
factory.close(); // Não muda nada, o processo continua sem encerrar
}
}
Test.java
package testehibernate;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class Teste
{
@Id
private int id;
private String nome;
private String email;
public int getId()
{
return id;
}
public void setId(int id)
{
this.id = id;
}
public String getNome()
{
return nome;
}
public void setNome(String nome)
{
this.nome = nome;
}
public String getEmail()
{
return email;
}
public void setEmail(String email)
{
this.email = email;
}
}
Test Table:
CREATE TABLE 'teste' (
'id' int(11) NOT NULL,
'nome' varchar(255) NOT NULL,
'email' varchar(255) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
ALTER TABLE 'teste'
ADD PRIMARY KEY ('id');
TO_INCREMENT for table 'teste'
ALTER TABLE 'teste'
MODIFY 'id' int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=14;
hibernate.cfg.xml
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/testdb</property>
<property name="connection.username">root</property>
<property name="connection.password"></property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Mapping files -->
<mapping class="testehibernate.Teste"/>
</session-factory>
</hibernate-configuration>
Following modified code and working with Murilo's hint to close the registry:
package testehibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
public class TesteHibernate
{
public static void main(String[] args)
{
Configuration configuration = new Configuration().configure();
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
SessionFactory factory = configuration.buildSessionFactory(serviceRegistry);
Session session = factory.openSession();
Teste teste = new Teste();
teste.setNome("Alfredo");
teste.setEmail("[email protected]");
session.beginTransaction();
session.save(teste);
session.getTransaction().commit();
session.close();
factory.close();
StandardServiceRegistryBuilder.destroy(serviceRegistry);
}
}