z_inx
z_inx

Reputation: 355

Creation of the database tables with Hibernate

people!

I made a small project using Servlets / JBoss / Hibernate / Mysql.

I put Hibernate to generate tables automatically. My question is only that: When these table are generated? Tables should be created when I rise the JBoss or when I call the servlet in the browser?

Because I realized that they are created only when I call one of my Servlets, and I imagined they would be created when I rise the JBoss.

Sorry if it's silly.

Here is my Class:

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="estacoes")
public class Estacao {

@Id
@GeneratedValue
private int id;

private String nome;
private String endereco;
private String temperatura;
private String energia;
private String porta;
private String sinal;    
private String bateria;

...getters/setters...

My persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
 http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">

<persistence-unit name="arduinoserver">

    <!-- provedor/implementacao do JPA -->
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <non-jta-data-source>java:jboss/datasources/MysqlDS</non-jta-data-source>

    <!-- entidade mapaeada -->    
    <class>arduinoserver.beans.Estacao</class>

    <properties>
        <!-- propriedades do hibernate -->
        <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
        <property name="hibernate.show_sql" value="true" />
        <property name="hibernate.hbm2ddl.auto" value="update" />
        <property name="hibernate.connection.charSet" value="UTF-8" />
    </properties>
</persistence-unit>

And my DAO:

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

import arduinoserver.beans.Estacao;

public class EstacaoDAO {

protected EntityManager entityManager;

public EstacaoDAO() {
    entityManager = getEntityManager();
}

private EntityManager getEntityManager() {
    EntityManagerFactory factory =     Persistence.createEntityManagerFactory("arduinoserver");
    if(entityManager == null) {
        entityManager = factory.createEntityManager();
    }

    return entityManager;
}

public Estacao getById(final int id) {
    return entityManager.find(Estacao.class, id);
}

@SuppressWarnings("unchecked")
public List<Estacao> findAll() {
    return entityManager.createQuery("FROM estacoes").getResultList();
}

public void persist(Estacao estacao) {
    try {
        entityManager.getTransaction().begin();
//          entityManager.persist(estacao);
        entityManager.merge(estacao);
        entityManager.getTransaction().commit();
    } catch(Exception ex) {
        ex.printStackTrace();
        entityManager.getTransaction().rollback();
    }
}




}

Upvotes: 0

Views: 72

Answers (1)

Rosty
Rosty

Reputation: 130

As far as I know Hibernate creates the tables when the SessionFactory is created. The application server doesn't automatically create one on its own, therefor the tables are usually not created on the server start.

Upvotes: 1

Related Questions