gpt4 book ai didi

java - 方言 SQLite 3 与 Hibernate 5 的问题

转载 作者:搜寻专家 更新时间:2023-11-01 02:20:18 25 4
gpt4 key购买 nike

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

这是我第一次使用 JPA 和 hibernate,我遇到了一些问题:我有一个用 JPA 创建的数据库,我想用 JUnit5 对其进行一些 CRUD 测试。通过 pom.xml,我添加了依赖项以使用 SQLite 方言和 hibernate ,并且我有一个 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

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

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'

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

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

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

File tree of my project

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

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

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

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

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

编辑:我改变
<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]

最佳答案

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

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>

持久性.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>

当然,您必须根据需要更改 hibernate.connection.urlhibernate.hbm2ddl.auto 的值以及其他属性。

关于java - 方言 SQLite 3 与 Hibernate 5 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46990525/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com