2015-01-01 58 views
0

我有這個類:爲什麼我在運行我的文件時遇到此錯誤?

TestHibernate1.java

package hibernatetest2; 

import java.util.Date; 
import org.hibernate.*; 
import org.hibernate.SessionFactory; 
import org.hibernate.boot.registry.StandardServiceRegistryBuilder; 
import org.hibernate.cfg.Configuration; 
import org.hibernate.service.ServiceRegistry;  

import org.hibernate.Transaction; 
import org.hibernate.cfg.Configuration; 

public class TestHibernate1 { 
    public static void main(String args[]) throws Exception { 
     Configuration configuration = new Configuration(); 
     configuration.addClass(Personnes.class); 
     ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();   
     SessionFactory sessionFactory1 = configuration.buildSessionFactory(serviceRegistry); 

     Session session = sessionFactory1.openSession(); 
     Transaction tx = null; 
     try { 
      tx = session.beginTransaction(); 
      Personnes personne = new Personnes("nom6", "prenom6", new Date()); 
      session.save(personne); 
      session.flush() ; 
      tx.commit(); 
     } catch (Exception e) { 
      if (tx != null) { 
       tx.rollback(); 
      } 
      throw e; 
     } finally { 
      session.close(); 
     } 

     session.close(); 
    } 
} 

的錯誤是:

Exception in thread "main" org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set 
    at org.hibernate.engine.jdbc.dialect.internal.DialectFactoryImpl.determineDialect(DialectFactoryImpl.java:104) 
    at org.hibernate.engine.jdbc.dialect.internal.DialectFactoryImpl.buildDialect(DialectFactoryImpl.java:71) 
    at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:209) 
    at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:89) 
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:206) 
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:178) 
    at org.hibernate.cfg.Configuration.buildTypeRegistrations(Configuration.java:1885) 
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1843) 
    at hibernatetest2.TestHibernate1.main(TestHibernate1.java:20) 

回答

0

的代碼看似乎Hibernate的配置文件丟失。正確的步驟是(假設休眠版本> = 4.3.5)

Configuration configuration = new Configuration(); 
configuration.configure("hibernate.cfg.xml"); 
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder() 
     .applySettings(configuration.getProperties()).build(); 
SessionFactory sessionFactory = configuration 
     .buildSessionFactory(serviceRegistry); 

驗證您是否遵循正確的步驟。

相關問題