Using Hibernate to generate tables with h2

5

I'm developing an application in which I decided to use h2 as the embedded database. This is the first time I'm using this type of database and I'm having trouble generating the tables through hibernate.

My hibernate.cfg.xml :

<hibernate-configuration>
    <session-factory>
    <property name="connection.driver_class">org.h2.Driver</property>
    <property name="connection.username">root</property>
    <property name="connection.password">root</property>
    <property name="connection.url">jdbc:h2:../blcul</property>
    <property name="dialect">org.hibernate.dialect.H2Dialect</property>
    <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>
    <property name="hbm2ddl.auto">create</property>
    <property name="show_sql">true</property>
    <mapping class="domain.Cliente" />  
    </session-factory>
</hibernate-configuration>

My HibernateUtils class (test only):

public class HibernateUtil {

private static SessionFactory sessionFactory;

private static Configuration configurantion;

/**
 * 
 * @return
 */
public static SessionFactory getSessionFactory() {
    return sessionFactory;
}

/**
 * 
 */
public static void initConfiguration() {
    try {
        configurantion = new Configuration().configure("hibernate.cfg.xml");
        StandardServiceRegistryBuilder sb = new StandardServiceRegistryBuilder();
        sb.applySettings(configurantion.getProperties());
        StandardServiceRegistry standardServiceRegistry = sb.build();
        sessionFactory = configurantion
                .buildSessionFactory(standardServiceRegistry);
    } catch (Exception e) {
        System.err.println("Initial SessionFactory creation failed" + e);
        throw new ExceptionInInitializerError(e);
    }
}
}

and in my Frame I initialize hibernate this way:

public Frame() {
    HibernateUtil.initConfiguration();
    createCardLayout();
    setProperties();
}

This Client class I created is just an entity I'm using to test

/**
  * @author Anderson
  * @date 13/04/2014
*/
@Entity
@Table(name = "CLIENTE")
public class Cliente {

private Long id;

private String nome;

/**
 * Construtor
 */
public Cliente() {
}

/**
 * @return the id
 */
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public Long getId() {
    return id;
}

/**
 * @param id the id to set
 */
public void setId(Long id) {
    this.id = id;
}

/**
 * @return the nome
 */
public String getNome() {
    return nome;
}

/**
 * @param nome the nome to set
 */
public void setNome(String nome) {
    this.nome = nome;
}
}

As the sql show is enabled when the hibernate settings are executed the following sql is displayed on the console:

drop table CLIENTE if exists
create table CLIENTE (
    id bigint generated by default as identity,
    nome varchar(255),
    primary key (id)
)

There is no exception. When I look at the h2 console, no table was created:

I can not see where the problem is. The hibernate settings I picked up from the documentation on the h2 website itself.

    
asked by anonymous 03.05.2014 / 23:02

1 answer

3

Use the hibernate.hbm2ddl.auto key and not hbm2ddl.auto.

    
04.05.2014 / 02:25