Spring Boot 无法加载模块:orm:sqlite的hibernate-community-dialects

wj8zmpe1  于 2023-04-20  发布在  Spring
关注(0)|答案(2)|浏览(193)

bounty明天到期。回答此问题可获得+200声望奖励。Bussiere希望引起更多关注此问题。

我想用springboot和hibernate做一个poc:
这是我的版本。gradle:

plugins {
    id 'java'
    id 'org.springframework.boot' version '3.0.5'
    id 'io.spring.dependency-management' version '1.1.0'
}

group = 'com.server'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '17'

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    implementation 'org.xerial:sqlite-jdbc:3.8.11.2'
    implementation 'org.hibernate:hibernate-core:6.2.0.Final'
    // https://mavenlibs.com/maven/dependency/org.hibernate.orm/hibernate-community-dialects
    implementation 'org.hibernate.orm:hibernate-community-dialects:6.2.0.Final'

}

tasks.named('test') {
    useJUnitPlatform()
}

还有我的application.properties

spring.jpa.database-platform=org.hibernate.community.dialect.SQLiteDialect
spring.jpa.hibernate.ddl-auto=create
spring.datasource.url = jdbc:sqlite:dbfile.db
spring.datasource.driver-class-name = org.sqlite.JDBC
hibernate.hbm2ddl.auto=create
spring.datasource.initialization-mode=always
hibernate.show_sql=true
spring.datasource.username = admin
spring.datasource.password = admin
spring.jpa.hibernate.ddl-auto=create-drop

但是当我启动它的时候我有一个:
java.lang.ClassNotFoundException:无法加载模块:org.hibernate.orm:hibernate-community-dialects
你知道吗?
问候

envsm3lx

envsm3lx1#

hibernate-community-dialects降级到版本6.1.7.Final。与版本6.2.0.Final一样,它使用了hibernate-core包中的某个类,如AlterTableUniqueDelegate,但在hibernate-core版本6.2.0.Final中尚未存在。
更新build.gradle文件

plugins {
    id 'java'
    id 'org.springframework.boot' version '3.0.5'
    id 'io.spring.dependency-management' version '1.1.0'
}

group = 'com.server'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '17'

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    implementation 'org.xerial:sqlite-jdbc:3.8.11.2'
    implementation 'org.hibernate:hibernate-core:6.1.7.Final'
    // https://mavenlibs.com/maven/dependency/org.hibernate.orm/hibernate-community-dialects
    implementation 'org.hibernate.orm:hibernate-community-dialects:6.1.7.Final'

}

tasks.named('test') {
    useJUnitPlatform()
}
3b6akqbq

3b6akqbq2#

我也有新版本的hibernate-community-dialects的问题,但与以前的版本6.1.7.Final一切都为我工作正常。

plugins {
    id 'java'
    id 'org.springframework.boot' version '3.0.5'
    id 'io.spring.dependency-management' version '1.1.0'
}

group = 'com.server'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '17'

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    implementation 'org.xerial:sqlite-jdbc:3.8.11.2'
    implementation 'org.hibernate:hibernate-core:6.2.0.Final'
    implementation 'org.hibernate.orm:hibernate-community-dialects:6.1.7.Final'

}

tasks.named('test') {
    useJUnitPlatform()
}

Sping Boot 不提供对SQLite数据库的配置支持,所以你需要公开我们自己的DataSource bean:

package com.server;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

import javax.sql.DataSource;

@Configuration
public class SQLiteConfig {

    @Autowired
    private Environment env;

    @Bean
    public DataSource dataSource() {
        final DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName(env.getProperty("spring.datasource.driver-class-name"));
        dataSource.setUrl(env.getProperty("spring.datasource.url"));
        dataSource.setUsername(env.getProperty("spring.datasource.username"));
        dataSource.setPassword(env.getProperty("spring.datasource.password"));
        return dataSource;
    }
}

相关问题