5
I’m developing an application in which I decided to use H2 as an embedded database. It is the first time that I am using this type of database and I am having problems to generate the tables by Hibernate.
mine 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 class Hibernateutils (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 this capability when the Hibernate settings are executed the following sql is shown in the console :
drop table CLIENTE if exists
create table CLIENTE (
id bigint generated by default as identity,
nome varchar(255),
primary key (id)
)
No exception. When I look at the H2 console, no table was created :
I don’t see where the problem is. The settings of Hibernate I got from the documentation on the site of H2.
He complained that the project was not creating the tables. The solution to his problem is the wrong configuration they were using. That wouldn’t be an answer?
– uaiHebert
Thanks for the help @uaiHebert. It worked. Since Hibernate is a JPA implementation, shouldn’t using the property as "hmb2ddl.auto" work as well? This property is not provided in the specification?
– Anderson
If I am not mistaken, only in version 2.2 of JPA this configuration became part of the specification. If you use this setting with eclipseLink it will not work. [=
– uaiHebert
@Anderson If the answer solved your problem, don’t forget to vote and mark it as the correct answer (the little Heckmark under the votes)
– Gabe
@uaiHebert What does this key do? I’m kind of (very... quite) ignorant about hibernate.
– Gabe
It serves to control the relationship between database and your database schema. You have the following options: do nothing (default), validate (validate the entire database comparing to the schema, ideal for production), drop the tables and create everything again, update the database with the current schema (imagine you added a column), etc.
– uaiHebert