为什么我在运行文件时遇到此错误?

时间:2015-01-01 11:37:41

标签: java hibernate mysql-connector

我有这堂课:

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)

1 个答案:

答案 0 :(得分:0)

查看代码似乎缺少hibernate配置文件。正确的步骤是(假设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);

验证您是否遵循了正确的步骤。