Sandro_Machado 3 de ago. de 2016
Posta o código que faz a conexão, para analisarmos,
tem algo não instanciado.
Exception in thread "main" java.lang.NullPointerException
masterkeymasterkey 18 de ago. de 2016
Bom dia Sandro, segue o código:
package com.livro.capitulo3.conexao ;
import org.hibernate.SessionFactory ;
import org.hibernate.cfg.Configuration ;
public class HibernateUtil {
private static final SessionFactory sessioFactory = buildSessionFactory ();
private static SessionFactory buildSessionFactory () {
try {
Configuration cfg = new Configuration ();
cfg . configure ();
return cfg . buildSessionFactory ();
} catch ( Throwable e ) {
System . out . println ( "Criação inicial do objeto SessionFactory falhou. Erro: " + e );
throw new ExceptionInInitializerError ( e );
}
}
public static SessionFactory getSessionFactory (){
return sessioFactory ;
}
}
package com.livro.capitulo3.conexao;
import org.hibernate.classic.Session;
import com.livro.capitulo3.conexao.HibernateUtil;
public class ConectaHibernateMySQL {
public static void main(String[] args){
Session sessao = null;
try {
sessao = (Session) HibernateUtil.getSessionFactory().openSession();
System.out.println(“Conectou”);
} finally {
sessao.close();
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<property name= "dialec" > org.hibernate.dialect.MySQL5InnoDBDialect</property>
<property name= "connection.driver_class" > com.mysql.jdbc.Driver</property>
<property name= "connection.url" > jdbc:mysql://localhost/agenda</property>
<property name= "connection.username" > root</property>
<property name= "connection.password" > masterkey</property>
<property name= "current_session_context_class" > thread</property>
<property name= "c3po.min_size" > 5</property>
<property name= "c3po.max_size" > 20</property>
<property name= "c3po.timeout" > 300</property>
<property name= "c3po.max_statement" > 50</property>
<property name= "c3po.idle_test_period" > 3000</property>
<property name= "show_sql" > true</property>
<property name= "format_sql" > true</property>
<property name= "generate_statistics" > true</property>
<property name= "use_sql_comments" > true</property>
Sandro_Machado 18 de ago. de 2016
Você está iniciando a Transaction em algum outro lugar?
senão você deve abrir, tente o abaixo (não testei)
package com.livro.capitulo3.conexao ;
import org.hibernate.classic.Session ;
import com.livro.capitulo3.conexao.HibernateUtil ;
public class ConectaHibernateMySQL {
public static void main ( String [] args ){
Session sessao = null ;
Transaction transacao = null ;
try {
sessao = ( Session ) HibernateUtil . getSessionFactory (). openSession ();
transacao = sessao . beginTransaction ();
System . out . println ( "Conectou" );
} finally {
sessao . close ();
}
}
}
masterkeymasterkey 21 de ago. de 2016
Bom dia Sandro,
implementei o código sugerido por você e o erro agora é esse:
1 [ main ] INFO org . hibernate . Version - HHH000412 : Hibernate Core { 5.2.1 . Final }
5 [ main ] INFO org . hibernate . cfg . Environment - HHH000206 : hibernate . properties not found
7 [ main ] INFO org . hibernate . cfg . Environment - HHH000021 : Bytecode provider name : javassist
74 [ main ] WARN org . hibernate . orm . deprecation - HHH90000012 : Recognized obsolete hibernate namespace < a href = "http://hibernate.sourceforge.net/hibernate-configuration" > http : // hibernate . sourceforge . net / hibernate - configuration </ a > . Use namespace < a href = "http://www.hibernate.org/dtd/hibernate-configuration" > http : // www . hibernate . org / dtd / hibernate - configuration </ a > instead . Support for obsolete DTD / XSD namespaces may be removed at any time .
568 [ main ] INFO org . hibernate . annotations . common . Version - HCANN000001 : Hibernate Commons Annotations { 5.0.1 . Final }
802 [ main ] WARN org . hibernate . orm . connections - HHH10001002 : Using Hibernate built - in connection pool ( not for production use ! )
824 [ main ] INFO org . hibernate . orm . connections - HHH10001005 : using driver [ com.mysql.jdbc.Driver ] at URL [ jdbc:mysql://localhost/agenda ]
825 [ main ] INFO org . hibernate . orm . connections - HHH10001001 : Connection properties : { user = root , password =**** }
825 [ main ] INFO org . hibernate . orm . connections - HHH10001003 : Autocommit mode : false
832 [ main ] INFO org . hibernate . engine . jdbc . connections . internal . DriverManagerConnectionProviderImpl - HHH000115 : Hibernate connection pool size : 20 ( min = 1 )
Sun Aug 21 10 : 35 : 46 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 + requirements 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 .
1518 [ main ] INFO org . hibernate . dialect . Dialect - HHH000400 : Using dialect : org . hibernate . dialect . MySQL5Dialect
Exception in thread “ main ” java . lang . NullPointerException
at com . livro . capitulo3 . conexao . Conecta . main ( Conecta . java : 18 )
Poderia por favor avaliar?
masterkeymasterkey 22 de ago. de 2016
Bom dia, eu faço isso no código como mostra o exemplo abaixo:
package com.livro.capitulo3.conexao ;
import org.hibernate.SessionFactory ;
import org.hibernate.cfg.Configuration ;
public class HibernateUtil {
private static final SessionFactory sessioFactory = buildSessionFactory ();
private static SessionFactory buildSessionFactory () {
try {
Configuration cfg = new Configuration ();
cfg . configure ( “ hibernate . cfg . xml ” );
return cfg . buildSessionFactory ();
} catch ( Throwable e ) {
System . out . println ( "Criação inicial do objeto SessionFactory falhou. Erro: " + e );
throw new ExceptionInInitializerError ( e );
}
}
public static SessionFactory getSessionFactory (){
return sessioFactory ;
}
}
O problema é que quando seto o
cfg.configure(“hibernate.cfg.xml”);
aparece o seguinte erro:
Como proceder?
Att;