如何解决此错误org.hibernate.HibernateException:当未设置“ hibernate.dialect”时,对DialectResolutionInfo的访问不能为空

时间:2018-07-18 07:46:15

标签: java mysql hibernate

我正在尝试通过mysql连接使用休眠模式 发生此错误 org.hibernate.HibernateException:如果未设置“ hibernate.dialect”,则对DialectResolutionInfo的访问不能为空

  

主类

package com.gontuseries.hibernate;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class Main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Student_Info student=new Student_Info();
//      .buildSessionFactory()
        student.setName("aaa");
        student.setRollNo(1);

        Configuration cfg = new Configuration();

//      SessionFactory sessionFactory = new AnnotationConfiguration().configure();
        SessionFactory sessionFactory = cfg.buildSessionFactory();
        cfg.addClass(Student_Info.class);
        Session session = sessionFactory.openSession();
        session.beginTransaction();

        session.save(student);

        session.getTransaction().commit();
        session.close();
        sessionFactory.close();
    }

}

hibernate.cfg.xml

<hibernate-configuration>
    <session-factory>
        <!-- Database connection settings -->
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/hibernateTutorial</property>
        <property name="connection.username">root</property>
        <property name="connection.password">1370</property>
        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">1</property>

        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>

        <!-- Disable the second-level cache  -->
        <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>

        <!-- Drop the existing tables and create new one -->
        <property name="hbm2ddl.auto">create</property>

<mapping class="com.gontuseries.hibernate.Student_Info"/>
    </session-factory>
</hibernate-configuration>
  

实体类

package com.gontuseries.hibernate;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="STUDENT_INFORMATION")
public class Student_Info {
    @Id
    public int getRollNo() {
        return rollNo;
    }
    public void setRollNo(int rollNo) {
        this.rollNo = rollNo;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    private int rollNo;
    private String name;
}
  

错误

线程“主”中的异常org.hibernate.service.spi.ServiceException:无法创建请求的服务[org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]     在org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:275)     在org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:237)     在org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:214)     在org.hibernate.id.factory.internal.DefaultIdentifierGeneratorFactory.injectServices(DefaultIdentifierGeneratorFactory.java:152)     在org.hibernate.service.internal.AbstractServiceRegistryImpl.injectDependencies(AbstractServiceRegistryImpl.java:286)     在org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:243)     在org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:214)     在org.hibernate.boot.internal.InFlightMetadataCollectorImpl。(InFlightMetadataCollectorImpl.java:179)     在org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:119)     在org.hibernate.boot.model.process.spi.MetadataBuildingProcess.build(MetadataBuildingProcess.java:84)     在org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:474)     在org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:85)     在org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:689)     在org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:724)     在com.gontuseries.hibernate.Main.main(Main.java:19) 由以下原因引起:org.hibernate.HibernateException:当未设置“ hibernate.dialect”时,对DialectResolutionInfo的访问不能为null     在org.hibernate.engine.jdbc.dialect.internal.DialectFactoryImpl.determineDialect(DialectFactoryImpl.java:100)     在org.hibernate.engine.jdbc.dialect.internal.DialectFactoryImpl.buildDialect(DialectFactoryImpl.java:54)     在org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:137)上     在org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:35)     在org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.initiateService(StandardServiceRegistryImpl.java:94)     在org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:263)     ...另外14个

0 个答案:

没有答案