HibernateException:未设置“hibernate.dialect”时,对DialectResolutionInfo的访问不能为空

2022-09-03 09:12:24

使用 maven 创建休眠测试项目。

当我运行项目时,它会生成异常:

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) 

但是在主类中,必需的属性被设置了。不知道为什么程序是 genrAating 异常。

主要类别:

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();
    }
}
}

在 POM 文件中:

<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>

任何想法问题可能在哪里??

客户模型 cLass:

@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

您没有正确初始化配置、服务注册表和会话网站。从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()


答案 2

这似乎是一个古老的问题,但现在当我使用4.3.1最终的Hibernate版本时,我也遇到了这个问题。在阅读了许多答案之后,似乎他们真的没有在文档中处理它,但我浏览了文档,我认为他们有必要的信息。休眠配置不必位于 .properties 文件中,但它也可以在 xml 文件中。

只需确保在调用 的实例之前调用 。这是因为 configure() 实际上从默认的 cfg 加载配置.xml文件,而构建实际上使用它。configure()build()StandardServiceRegistryBuilder

要理解这一点,你可以做一件事....在调用之前....调用并打印它...这是一张地图。configure()getSettings()StandardServiceRegistryBuilder

您将不会在 cfg 中看到任何休眠属性.xml

现在调用并打印地图....你会看到...你已经拥有了你所有的财产。configure()getSettings()


推荐