hibernate 休眠SQLite:无法获取连接,因为驱动程序管理器未正确初始化

jdg4fx2g  于 2023-02-16  发布在  SQLite
关注(0)|答案(1)|浏览(248)

我尝试使用一个数据库系统,而不必运行一个单独的数据库程序。我决定使用Hibernate SQLite。我在这里变得绝望。
有没有人能告诉我我在做什么,或者我在哪里可以找到更多的帮助?我在谷歌上失败了,ChatGPT也一如既往地没有帮助。
对于每一个如何试图说服我使用例如MariaDB,不。我需要它是一个java应用程序。不多,不少...
源文件\main\资源\休眠配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <!-- <property name="hibernate.connection.url">jdbc:sqlite:path/to/your/database.db</property> -->
        <property name="hibernate.connection.driver_class">org.sqlite.JDBC</property>
        <property name="hibernate.connection.url">jdbc:sqlite:database.db</property>
        <property name="hibernate.dialect">org.hibernate.dialect.SQLiteDialect</property>
        <property name="hibernate.show_sql">true</property>
        <property name="hibernate.hbm2ddl.auto">create</property>
        <mapping class="de.minetrain.kekbot.database.test.Person"/>
    </session-factory>
</hibernate-configuration>

源\主\资源\持久性.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_2.xsd" version="2.2">
    <persistence-unit name="my-persistence-unit" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <class>de.minetrain.kekbot.database.test.Person</class>
        <!-- <class>com.example.MyOtherEntity</class> -->
        <properties>
            <property name="hibernate.connection.driver_class" value="org.sqlite.JDBC" />
            <!-- <property name="hibernate.connection.url" value="jdbc:sqlite:path/to/your/database.db" /> -->
            <property name="hibernate.connection.url" value="jdbc:sqlite:database.db" />
            <property name="hibernate.connection.username" value="" />
            <property name="hibernate.connection.password" value="" />
            <property name="hibernate.dialect" value="org.hibernate.dialect.SQLiteDialect" />
            <property name="hibernate.hbm2ddl.auto" value="update" />
            <!-- <property name="hibernate.show_sql" value="true" /> -->
        </properties>
    </persistence-unit>
</persistence>

pom.xml

</ependencies>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>5.6.1.Final</version>
        </dependency>
        <dependency>
            <groupId>org.xerial</groupId>
            <artifactId>sqlite-jdbc</artifactId>
            <version>3.34.0</version>
        </dependency>
    </dependencies>

休眠应用程序类:

package de.minetrain.kekbot.database.test;

import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class HibernateUtil {
    private static final Logger logger = LoggerFactory.getLogger(HibernateUtil.class);
    private static final SessionFactory sessionFactory = buildSessionFactory();
    
    private static SessionFactory buildSessionFactory() {
        
        try {
            Class.forName("org.sqlite.JDBC");
        } catch (ClassNotFoundException e) {
            logger.error("Dant find class: ",e);
        }
        
        try {
            Configuration configuration = new Configuration().configure("hibernate.cfg.xml"); // may wrong?
            StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties());
            return configuration.buildSessionFactory(builder.build());
        } catch (Throwable ex) {
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }
    
    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }
}

实体类:

package de.minetrain.kekbot.database.test;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class Person {
    
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Long id;
    
    private String name;
    private int age;
    
    
    public Long getId() {
        return id;
    }
    
    public void setId(Long id) {
        this.id = id;
    }
    
    public String getName() {
        return name;
    }
    
    public void setName(String name) {
        this.name = name;
    }
    
    public int getAge() {
        return age;
    }
    
    public void setAge(int age) {
        this.age = age;
    }

}

我试图写入数据库的类:

package de.minetrain.kekbot.database.test;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

public class Database {
    public static void test(){
        SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
        Session session = sessionFactory.openSession();
        // Verwenden Sie die Session, um Datenbankoperationen auszuführen
//      session.close();
        
        Transaction tx = null;
        try {
            tx = session.beginTransaction();
            Person person = new Person();
            person.setName("Max Mustermann");
            person.setAge(30);
            session.save(person);
            tx.commit();
        } catch (HibernateException ex) {
            if (tx != null) {
                tx.rollback();
            }
            ex.printStackTrace();
        } finally {
            session.close();
        }
        
        

    }
}

我只是想学习使用SQLite,并尝试将一个新的人对象写入数据库文件。
错误:

[20:00:15] >> WARN << [org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator] - HHH000342: Could not obtain connection to query metadata java.lang.IllegalStateException: Cannot get a connection as the driver manager is not properly initialized

Initial SessionFactory creation failed.org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]

Exception in thread "main" java.lang.ExceptionInInitializerError
Caused by: org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
Caused by: org.hibernate.boot.registry.selector.spi.StrategySelectionException: Unable to resolve name [org.hibernate.dialect.SQLiteDialect] as strategy [org.hibernate.dialect.Dialect]
Caused by: org.hibernate.boot.registry.classloading.spi.ClassLoadingException: Unable to load class [org.hibernate.dialect.SQLiteDialect]
Caused by: java.lang.ClassNotFoundException: Could not load requested class : org.hibernate.dialect.SQLiteDialect
xmq68pz9

xmq68pz91#

我已经设法修改了删除项。在@andrewJames的帮助下,我发现我必须使用一种方言,而我现在没有。我还注意到我使用的是旧版本的hib和co。
对不起提前,为每一个有经验的编码员如何得到头痛从我的愚蠢。我不是新的编码,但我是非常新的数据库和处理依赖的外部只是API包的事情。我试图谷歌作为mutch我可以找到,并测试每件事我可以。但我结束了不得不再次要求。我很抱歉:/
我已经改变了一些东西,现在有了这个pom.xml:

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <version>6.1.6.Final</version>
</dependency>
<dependency>
    <groupId>org.xerial</groupId>
    <artifactId>sqlite-jdbc</artifactId>
    <version>3.40.0.0</version>
</dependency>
<dependency>
    <groupId>com.github.gwenn</groupId>
    <artifactId>sqlite-dialect</artifactId>
    <version>0.1.2</version>
</dependency>

我改变了.xml字段中的方言:

hibernate.cfg.xml: 
   <property name="hibernate.dialect">org.sqlite.hibernate.dialect.SQLiteDialect</property>

persistence.xml:
   <property name="hibernate.dialect" value="org.sqlite.hibernate.dialect.SQLiteDialect" />

然而,现在我从处理这个问题中得到了好处:

[00:07:48] >> INFO << [org.hibernate.Version] - HHH000412: Hibernate ORM core version 6.1.6.Final
Initial SessionFactory creation failed.java.util.ServiceConfigurationError: org.hibernate.boot.spi.MetadataBuilderInitializer: Provider org.sqlite.hibernate.dialect.SQLiteMetadataBuilderInitializer could not be instantiated
Exception in thread "main" java.lang.ExceptionInInitializerError
Caused by: java.util.ServiceConfigurationError: org.hibernate.boot.spi.MetadataBuilderInitializer: Provider org.sqlite.hibernate.dialect.SQLiteMetadataBuilderInitializer could not be instantiated
Caused by: java.lang.NoClassDefFoundError: org/hibernate/dialect/function/SQLFunction
Caused by: java.lang.ClassNotFoundException: org.hibernate.dialect.function.SQLFunction

相关问题