我遇到了Hibernate-JPA-Maven的一些问题这是我第一次使用JPA和hibernate,我遇到了一些问题:我有一个用JPA创建的数据库,我想用JUnit5对它进行一些CRUD测试.使用pom.xml我添加依赖项以使用SQLite方言和hibernate,并且...
我遇到了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中的配置问题(缺少依赖项?),或者这是我的持久性,我无法弄明白. xml,或者这来自我的类??Test.
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>
persistence.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.url和hibernate.hbm2ddl.auto的值,并根据您的需要更改其他属性.
本文标题为:java – 使用Hibernate 5的方言SQLite 3的问题
基础教程推荐
- Spring Boot教程之提高开发效率必备工具lombok 2023-05-08
- SpringCloud服务网关Gateway的使用教程详解 2023-05-18
- SpringCloud Ribbon与OpenFeign详解如何实现服务调用 2023-05-19
- 简单解析java方法在调用在内存中的执行过程 2023-02-27
- SpringSecurity详解整合JWT实现全过程 2023-03-21
- Spring myBatis数据库连接异常问题及解决 2022-11-29
- 浅谈Servlet转发到JSP页面的路径问题(必看) 2023-07-31
- 为何HashSet中使用PRESENT而不是null作为value 2023-06-10
- Mybatis结果集映射与生命周期详细介绍 2023-06-10
- Mybatis-Plus中and()和or()的使用与原理详解 2023-05-14