5
Good afternoon !!
I’m new with Hibernate and I’m two days trying to implement the creation of tables with it, I’ve already followed several tutorials, but it doesn’t work.
Can someone help me ?
Hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.drive_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://127.0.0.1:3306/testebanco</property>
<property name="connection.username">root</property>
<property name="connection.password">102030</property>
<!-- JDBC connection pool(use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</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.internal.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">create</property>
<!-- Mapeamento das entidades -->
<mapping class="teste.DAO.Estado" />
</session-factory>
Class of session creation:
public class HibernateUtil {
private static SessionFactory sf = createSessionFactory();
public static SessionFactory getSf() {
return sf;
}
private static SessionFactory createSessionFactory(){
try{
Configuration cfg = new Configuration().configure();
ServiceRegistry registro = new StandardServiceRegistryBuilder().applySettings(cfg.getProperties()).build();
SessionFactory f = cfg.buildSessionFactory(registro);
return f;
}catch(Throwable ex){
System.err.println("Erro ao criar a sessão " + ex);
throw new ExceptionInInitializerError(ex);
}
}
}
I’m using Junit to perform the tests:
public class HibernateUtilTeste {
@Test
public void conectar(){
Session sessao = HibernateUtil.getSf().openSession();
sessao.close();
HibernateUtil.getSf().close();
}
}
State-class:
@Entity
@Table
public class Estado extends GenericID {
private String nome;
private String sigla;
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public String getSigla() {
return sigla;
}
public void setSigla(String sigla) {
this.sigla = sigla;
}
Class of ID
@SuppressWarnings("serial")
@MappedSuperclass
public class GenericID implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
}
Log:
Log ------------- jul 09, 2016 2:04:51 pm org.hibernate.Version logVersion INFO: HHH000412: Hibernate Core {5.2.1.Final} Jul 09, 2016 2:04:51 pm org.hibernate.cfg.Environment INFO: HHH000206: Hibernate.properties not found Jul 09, 2016 2:04:51 pm org.hibernate.cfg.Environment buildBytecodeProvider INFO: HHH000021: Bytecode Provider name : javassist Jul 09, 2016 2:04:51 pm org.hibernate.boot.jaxb.Internal.stax.Localxmlresourceresolver resolveEntity WARN: HHH90000012: Recognized obsolete Hibernate namespace http://hibernate.sourceforge.net/hibernate-configuration. Use namespace http://www.hibernate.org/dtd/hibernate-configuration Instead. Support for obsolete DTD/XSD namespaces may be Removed at any time. jul 09, 2016 2:04:51 pm org.hibernate.Annotations.common.Reflection.java.Javareflectionmanager INFO: HCANN000001: Hibernate Commons Annotations {5.0.1.Final} Jul 09, 2016 2:04:51 pm org.hibernate.engine.jdbc.Connections.internal.Drivermanagerconnectionproviderimpl configure WARN: HHH10001002: Using Hibernate built-in Connection pool (not for Production use!) Jul 09, 2016 2:04:51 pm org.hibernate.engine.jdbc.Connections.internal.Drivermanagerconnectionproviderimpl buildCreator INFO: HHH10001005: using driver [null] at URL [jdbc:mysql:/127.0.0.1:3306/testebank] Jul 9, 2016 2:04:51 PM org.hibernate.engine.jdbc.Connections.internal.Drivermanagerconnectionproviderimpl buildCreator INFO: HHH10001001: Connection properties: {user=root, drive_class=com.mysql.jdbc.Driver, password=****} Jul 09, 2016 2:04:51 PM org.hibernate.engine.jdbc.Connections.internal.Drivermanagerconnectionproviderimpl buildCreator INFO: HHH10001003: Autocommit mode: false Jul 09, 2016 2:04:51 pm org.hibernate.engine.jdbc.Connections.internal.Pooledconnections INFO: HHH000115: Hibernate Connection pool size: 1 (min=1) Sat Jul 09 14:04:52 BRT 2016 WARN: Establishing SSL Connection without server’s Identity Verification is not Recommended. According to Mysql 5.5.45+, 5.6.26+ and 5.7.6+ Purchases SSL Connection must be established by default if Explicit option isn’t set. For compliance with existing Applications not using SSL the verifyServerCertificate Property is set to 'false'. You need either to explicitly disable SSL by Setting useSSL=false, or set useSSL=true and provide truststore for server Certificate Verification. jul 09, 2016 2:04:52 pm org.hibernate.dialect.Dialect INFO: HHH000400: Using dialect: org.hibernate.dialect.Mysql5innodbdialect Jul 09, 2016 2:04:52 PM org.hibernate.tool.schema.Internal.Schemacreatorimpl applyImportSources INFO: HHH000476: Executing import script 'org.hibernate.tool.schema.Internal.exec.Scriptsourceinputnonexistentimpl@45385f75' Jul 09, 2016 2:04:53 pm org.hibernate.engine.jdbc.Connections.internal.Drivermanagerconnectionproviderimpl STOP INFO: HHH10001008: Cleaning up Connection pool [jdbc:mysql:/127.0.0.1:3306/testbank]
Add to your question what you’ve already managed to do.
– DiegoAugusto
Like your class State?
– DiegoAugusto
One approach you can follow: use some Quick start, which starts a project, already with the persistence artifacts ready. One of the easy ways is to use the Java EE, Dinamic web project(Maven) perspective. After that, see how it was implemented.
– Marco Aurelio
I am using Maven, when I run the project it does not generate error nor one, but also does not create the table.
– Diego Dantas
http://answall.com/questions/71578/hibernate-correctmente-configurado-n%C3%a3o-inserts-into-database/102019#102019
– DiegoAugusto
The database testebanco already exists? Hibernate can only create the tables if the bank already exists.
– Sandro Queiroz Jr
I had to post here because I do not have enough points yet to comment , but this tutorial here is well explained , I do not know if you’ve tried it . Hibernate Tutorial
– Adliano Alves