java 方言 SQLite 3 与 Hibernate 5 的问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46990525/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 09:27:47  来源:igfitidea点击:

Problems with dialect SQLite 3 with Hibernate 5

javahibernatesqlitemavenjpa

提问by Unconnu

I'm facing some problems with Hibernate-JPA-Maven

我在使用 Hibernate-JPA-Maven 时遇到了一些问题

It's the first time I use JPA and hibernate and I have some problems : I have a database that I have created with JPA and I would like to make some CRUD tests on it with JUnit5. With the pom.xml I add dependency to work with a SQLite dialect and hibernate, and I have a persistence.xml file. I use Eclipse to run the project to see if the first simple test works and i have this problem :

这是我第一次使用 JPA 和休眠,我遇到了一些问题:我有一个用 JPA 创建的数据库,我想用 JUnit5 对它进行一些 CRUD 测试。使用 pom.xml,我添加了使用 SQLite 方言和休眠的依赖项,并且我有一个 persistence.xml 文件。我使用 Eclipse 运行项目以查看第一个简单测试是否有效,但我遇到了这个问题:

oct. 28, 2017 2:42:32 PM org.hibernate.jpa.internal.util.LogHelper logPersistenceUnitInformation INFO: HHH000204: Processing PersistenceUnitInfo [ name: Bla ...] oct. 28, 2017 2:42:32 PM org.hibernate.Version logVersion INFO: HHH000412: Hibernate Core {5.2.12.Final} oct. 28, 2017 2:42:32 PM org.hibernate.cfg.Environment INFO: HHH000206: hibernate.properties not found oct. 28, 2017 2:42:32 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager INFO: HCANN000001: Hibernate Commons Annotations {5.0.1.Final} oct. 28, 2017 2:42:33 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!) oct. 28, 2017 2:42:33 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator INFO: HHH10001005: using driver [org.sqlite.JDBC] at URL [jdbc:sqlite::memory:] oct. 28, 2017 2:42:33 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator INFO: HHH10001001: Connection properties: {charSet=UTF-8, password=****, user=test} oct. 28, 2017 2:42:33 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator INFO: HHH10001003: Autocommit mode: false oct. 28, 2017 2:42:33 PM org.hibernate.engine.jdbc.connections.internal.PooledConnections INFO: HHH000115: Hibernate connection pool size: 20 (min=1) oct. 28, 2017 2:42:33 PM org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator initiateService WARN: HHH000342: Could not obtain connection to query metadata : Unable to determine Dialect to use [name=SQLite, majorVersion=3]; user must register resolver or explicitly set 'hibernate.dialect' Exception in thread "main" org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment] at . . . Caused by: org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set at . . . ... 14 more

十月 2017 年 2 月 28 日下午 2:42:32 org.hibernate.jpa.internal.util.LogHelper logPersistenceUnitInformation INFO:HHH000204:处理 PersistenceUnitInfo [名称:Bla ...] 十月。2017 年 2 月 28 日下午 2:42:32 org.hibernate.Version logVersion 信息:HHH000412:Hibernate Core {5.2.12.Final} oct. 2017 年 2 月 28 日下午 2:42:32 org.hibernate.cfg.Environment INFO:HHH000206:10 月未找到 hibernate.properties。2017 年 2 月 28 日下午 2:42:32 org.hibernate.annotations.common.reflection.java.JavaReflectionManager 信息:HCANN000001:Hibernate Commons Annotations {5.0.1.Final} 十月。2017 年 2 月 28 日下午 2:42:33 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl 配置警告:HHH10001002:使用 Hibernate 内置连接池(不用于生产!)2017 年 2 月 28 日下午 2:42:33 org.hibernate.engine.jdbc.connections.internal。DriverManagerConnectionProviderImpl buildCreator INFO: HHH10001005: 在 URL [jdbc:sqlite::memory:] 上使用驱动程序 [org.sqlite.JDBC]。28, 2017 2:42:33 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator INFO: HHH10001001: Connection properties: {charSet=UTF-8, password=****, user=test} 2017 年 2 月 28 日下午 2:42:33 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator INFO:HHH10001003:自动提交模式:假八月。2017 年 2 月 28 日下午 2:42:33 org.hibernate.engine.jdbc.connections.internal.PooledConnections 信息:HHH000115:休眠连接池大小:20 (min=1) oct。2017 年 2 月 28 日下午 2:42:33 org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator 启动服务警告:HHH000342:无法获得查询元数据的连接:无法确定方言使用 [name=SQLite,majorVersion=3];用户必须在线程“main”org.hibernate.service.spi.ServiceException 中注册解析器或显式设置“hibernate.dialect”异常:无法在 处创建请求的服务 [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]。. . 引起:org.hibernate.HibernateException:当 'hibernate.dialect' 未设置为时,对 DialectResolutionInfo 的访问不能为空。. . ……还有 14 个 当 'hibernate.dialect' 未设置为 时,对 DialectResolutionInfo 的访问不能为空。. . ……还有 14 个 当 'hibernate.dialect' 未设置为 时,对 DialectResolutionInfo 的访问不能为空。. . ……还有 14 个

So I have i try to find out why I have :

所以我试图找出为什么我有:

WARN: HHH000342: Could not obtain connection to query metadata : Unable to determine Dialect to use [name=SQLite, majorVersion=3]; user must register resolver or explicitly set 'hibernate.dialect'

警告:HHH000342:无法获得查询元数据的连接:无法确定方言使用 [name=SQLite,majorVersion=3];用户必须注册解析器或明确设置“hibernate.dialect”

Apparently this is because SQLite 3 and Hibernate 5 are not compatible, so I try this : Does Hibernate Fully Support SQLitebut it was not working for me.

显然这是因为 SQLite 3 和 Hibernate 5 不兼容,所以我尝试这样做:Does Hibernatefully Support SQLite但它对我不起作用。

Then I try to take a lower version of hibernate and modify in my pom.xml the hibernate version and hibernate-core versions to 4.3.11.Final but same, it doesn't solved my problems.

然后我尝试使用较低版本的 hibernate 并在我的 pom.xml 中将 hibernate 版本和 hibernate-core 版本修改为 4.3.11.Final 但相同,它没有解决我的问题。

After many hours on that, I can't figure it out if the problems comes from the version of hibernate and SQLite, or if this is a problem of the configuration in my pom.xml (a missing dependency ?) or it's my persistance.xml, or this comes from my class Test.

经过几个小时后,我无法弄清楚问题是否来自 hibernate 和 SQLite 的版本,或者这是我的 pom.xml 中的配置问题(缺少依赖?)还是我的持久性。 xml,或者这来自我的类​​测试。

File tree of my project

我的项目的文件树

Please let me know if you need more details. Thank you in advance for your help.

如果您需要更多详细信息,请告诉我。预先感谢您的帮助。

My pom.xml file :

我的 pom.xml 文件:

<project
    xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

  <modelVersion>4.0.0</modelVersion>

  <groupId>fr.umlv.orthopro</groupId>
  <artifactId>OrthoPro_brain</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>OrthoPro_brain</name>
  <url>http://maven.apache.org</url>

  <properties>
     <java.version>9</java.version>
     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
     <junit.jupiter.version>5.0.1</junit.jupiter.version>
     <junit.platform.version>1.0.1</junit.platform.version>
     <hibernate.version>5.2.12.Final</hibernate.version>
     <sqlite.version>3.20.1</sqlite.version>
  </properties>

  <build>
     <plugins>
         <plugin>
             <groupId>org.apache.maven.plugins</groupId>
             <artifactId>maven-compiler-plugin</artifactId>
             <version>3.7.0</version>
             <configuration>
                 <source>${java.version}</source>
                 <target>${java.version}</target>
                 <showWarnings>true</showWarnings>
                 <showDeprecation>true</showDeprecation>
             </configuration>
         </plugin>
         <plugin>
             <groupId>org.apache.maven.plugins</groupId>
             <artifactId>maven-javadoc-plugin</artifactId>
             <version>3.0.0-M1</version>
             <configuration>
                 <reportOutputDirectory>${project.build.directory}/javadoc</reportOutputDirectory>
                 <destDir>javadoc</destDir>
                 <nohelp>true</nohelp>
             </configuration>
         </plugin>
     </plugins>
  </build>
  <dependencies>
     <dependency>
         <groupId>org.junit.jupiter</groupId>
         <artifactId>junit-jupiter-engine</artifactId>
         <version>${junit.jupiter.version}</version>
         <scope>test</scope>
     </dependency>
     <dependency>
         <groupId>org.junit.platform</groupId>
         <artifactId>junit-platform-runner</artifactId>
         <version>${junit.platform.version}</version>
         <scope>test</scope>
     </dependency>
     <dependency>
         <groupId>org.hibernate</groupId>
         <artifactId>hibernate-core</artifactId>
         <version>${hibernate.version}</version>
     </dependency>
     <dependency>
         <groupId>org.xerial</groupId>
         <artifactId>sqlite-jdbc</artifactId>
         <version>${sqlite.version}</version>
         <!-- <scope>test</scope> -->
     </dependency>
    <dependency>
        <groupId>org.hibernate.javax.persistence</groupId>
        <artifactId>hibernate-jpa-2.1-api</artifactId>
        <version>1.0.0.Final</version>
    </dependency>
    <dependency>
        <groupId>org.eclipse.persistence</groupId>
        <artifactId>org.eclipse.persistence.jpa</artifactId>
        <version>2.7.0</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>5.2.12.Final</version>
    </dependency>
    <dependency>
        <groupId>javax.xml.bind</groupId>
        <artifactId>jaxb-api</artifactId>
        <version>2.3.0</version>
    </dependency>

  </dependencies>
 </project>

My persistence.xml file :

我的persistence.xml 文件:

 <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_1.xsd"
                 version="2.1">

        <persistence-unit name="first_test" transaction-type="RESOURCE_LOCAL">
          <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
           <class>fr.umlv.orthpro.db.User</class>
           <class>fr.umlv.orthpro.db.Rule</class>
           <class>fr.umlv.orthpro.db.UserRule</class>
           <class>fr.umlv.orthpro.db.Sentence</class>

            <properties>
                <property name="dialect" value="org.hibernate.dialect.SQLiteDialect" />
                <property name="javax.persistence.jdbc.driver" value="org.sqlite.JDBC" />
                <property name="javax.persistence.jdbc.url" value="jdbc:sqlite::memory:" />
                <property name="javax.persistence.jdbc.user" value="test" />
                <property name="javax.persistence.jdbc.password" value="test" />
                <property name="hibernate.show_sql" value="true" />
                <property name="format_sql" value="true" />
                <property name="hibernate.connection.charSet" value="UTF-8" />
                <property name="hibernate.hbm2ddl.auto" value="create" />
            </properties>
        </persistence-unit>
    </persistence>

This is my class to run test on the database :

这是我在数据库上运行测试的类:

package fr.umlv.orthopro.db;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

public class Test {
      EntityManagerFactory emfactory = Persistence.createEntityManagerFactory( "first_test" );

      EntityManager entitymanager = emfactory.createEntityManager( );
      entitymanager.getTransaction( ).begin( );

      User quentin = new User( ); 
      quentin.setId(1201);
      quentin.setAdmin(false);

      entitymanager.persist( quentin );
      entitymanager.getTransaction( ).commit( );
      entitymanager.close( );
      emfactory.close( );
    }
}

EDIT : I change
<property name="dialect" value="org.hibernate.dialect.SQLiteDialect" />to
<property name="hibernate.dialect" value="org.hibernate.dialect.SQLiteDialect" />and now I have this :

编辑:我改变
<property name="dialect" value="org.hibernate.dialect.SQLiteDialect" />
<property name="hibernate.dialect" value="org.hibernate.dialect.SQLiteDialect" />现在我有这样的:

Unable to resolve name [org.hibernate.dialect.SQLiteDialect] as strategy [org.hibernate.dialect.Dialect]

无法将名称 [org.hibernate.dialect.SQLiteDialect] 解析为策略 [org.hibernate.dialect.Dialect]

回答by hogi

I am using SQLite 3.10 with Hibernate 5.2 with the following configuration:

我在 Hibernate 5.2 中使用 SQLite 3.10,配置如下:

pom.xml:

pom.xml:

<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <version>5.2.12.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.xerial/sqlite-jdbc -->
<dependency>
    <groupId>org.xerial</groupId>
    <artifactId>sqlite-jdbc</artifactId>
    <version>3.20.1</version>
    <scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/com.zsoltfabok/sqlite-dialect -->
<dependency>
    <groupId>com.zsoltfabok</groupId>
    <artifactId>sqlite-dialect</artifactId>
    <version>1.0</version>
</dependency>

persistence.xml:

持久性.xml:

<persistence version="2.1"
    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_1.xsd">

    <persistence-unit name="first_test" transaction-type="RESOURCE_LOCAL">
        <properties>
            <property name="hibernate.archive.autodetection" value="class" />
            <property name="hibernate.dialect" value="org.hibernate.dialect.SQLiteDialect" />
            <property name="hibernate.connection.driver_class" value="org.sqlite.JDBC" />
            <property name="hibernate.connection.username" value="" />
            <property name="hibernate.connection.password" value="" />
            <property name="hibernate.connection.user" value="" />
            <property name="hibernate.connection.autocommit" value="true"/>
            <property name="hibernate.connection.url" value="jdbc:sqlite:sqlite.db"/>
            <property name="hibernate.show_sql" value="true"/>
            <property name="hibernate.flushMode" value="ALWAYS" />
            <property name="hibernate.cache.use_second_level_cache" value="false" />
            <property name="hibernate.cache.provider_class" value="org.hibernate.cache.NoCacheProvider" />

            <!-- create https://docs.jboss.org/hibernate/orm/5.0/manual/en-US/html/ch03.html -->
            <property name="hibernate.hbm2ddl.auto" value="validate or create" />
        </properties>
    </persistence-unit>

</persistence>

Of course you have to change the value of hibernate.connection.urland hibernate.hbm2ddl.autoand maybe other properties to your needs.

当然,你必须改变的价值hibernate.connection.urlhibernate.hbm2ddl.auto也许其他属性您的需求。