当前位置: 首页 > 知识库问答 >
问题:

HibernateException:当未设置“Hibernate.Dialction”时,对DialectResolutionInfo的访问不能为null

苏畅
2023-03-14

使用Maven创建Hibernate测试项目。

org.hibernate.engine.jdbc.connections.internal.ConnectionProviderInitiator initiateService
WARN: HHH000181: No appropriate connection provider encountered, assuming application will be supplying connections
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 org.hibernate.hibernatetest.App.main(App.java:33)
Exception in thread "main" java.lang.NullPointerException
at org.hibernate.hibernatetest.App.main(App.java:51) 
public class App {

public static void main(String[] args) {
    Configuration configuration;
    ServiceRegistry serviceRegistry;
    SessionFactory sessionFactory;
    Session session = null;
    try {
        configuration = new Configuration();


        serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
        configuration.setProperty("hibernate.dialect ", "com.applerao.hibernatesqlite.dialect.SQLiteDialect");
        configuration.setProperty("hibernate.connection.url ", "jdbc:sqlite:TailorDB.db");
        configuration.setProperty("hibernate.connection.driver_class ", "org.sqlite.JDBC");
        configuration.setProperty("hibernate.connection.username ", "");
        configuration.setProperty("hibernate.connection.password ", "");
        sessionFactory = configuration.buildSessionFactory(serviceRegistry);

        session = sessionFactory.openSession();

        CustomerModel c = new CustomerModel();
        c.setID(5);
        c.setNIC_Number("691201631345");
        c.setFirstName("Zee");
        c.setNumber("55225522");
        c.setLastName("Jan");
        c.setCustomerCode("Zee-123");

        session.beginTransaction();
        session.save(c);
        session.getTransaction().commit();
    } catch (HibernateException e) {
        e.printStackTrace();
    } finally {
        session.close();
    }
}
}
<dependencies>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>4.3.0.Final</version>
    </dependency>
    <dependency>
        <groupId>org.xerial</groupId>
        <artifactId>sqlite-jdbc</artifactId>
        <version>3.7.2</version>
    </dependency>
    <dependency>
        <groupId>com.applerao</groupId>
        <artifactId>hibernatesqlite</artifactId>
        <version>1.0</version>
    </dependency>
</dependencies>

知道问题出在哪里吗??

CustomerModel类:

@Entity
@Table(name = "Customer")

public class CustomerModel {

@Id
@GeneratedValue
@Column(name = "c_id")
int ID;
@Column(name = "c_code")
String customerCode;
@Column(name = "c_fname")
String firstName;
@Column(name = "c_mname")
String middleName;
@Column(name = "c_lname")
String lastName;
@Column(name = "c_nic")
String NIC_Number;
@Column(name = "c_email")
String email;
@Column(name = "c_pnumber")
String number;
}

共有1个答案

西门淮晨
2023-03-14

您没有正确初始化configuration、serviceRegistry和sessionFactory。从Hibernate 4.3.2.final中介绍了StandardServiceRegistryBuilder。请按照以下顺序进行初始化,例如。

    Configuration configuration = new Configuration();
    configuration.configure("com/jeecourse/config/hibernate.cfg.xml");

    ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(
            configuration.getProperties()).build();
    sessionFactory = configuration.buildSessionFactory(serviceRegistry);

因此,在您的代码中,您错过了以下步骤:configuration.configure()

 类似资料: