Hibernate Search 需要 javax.persistence.EntityManager

huangapple go评论69阅读模式
英文:

Hibernate Search is required javax.persistence.EntityManager

问题

I'm trying make test application using Hibernate Search. I have note repository which extend SimpleJpaRepository with required parameter jakarta.persistence.EntityManager. But when i try to create search session Search.session(entityManager) I get an error that the Search.session() need javax.persistence.EntityManager. So question is how can i create Search session using jakarta.persistence.EntityManager?

NoteRepository:

@Repository
@Transactional
class NoteRepositoryImpl(
    private val entityManager: EntityManager,
) : SimpleJpaRepository<NoteEntity, Long>(NoteEntity::class.java, entityManager), NoteRepository {

    override fun search(query: String): List<NoteEntity> {
        val session = Search.session(entityManager)

        val result = session.search(NoteEntity::class.java)
            .where { it.match().fields("title", "tag", "content").matching(query) }
            .fetch(0, 100)
        return result.hits().map { it as NoteEntity }
    }
}

Gradle file:

plugins {
    id 'org.springframework.boot' version '3.0.4'
    id 'io.spring.dependency-management' version '1.1.0'
    id 'org.jetbrains.kotlin.jvm' version '1.7.22'
    id 'org.jetbrains.kotlin.plugin.spring' version '1.7.22'
    id "org.jetbrains.kotlin.plugin.jpa" version "1.7.22"
    id 'org.jetbrains.kotlin.plugin.allopen' version '1.7.22'
}

allOpen {
    annotation("jakarta.persistence.Entity")
    annotation("jakarta.persistence.Embeddable")
    annotation("jakarta.persistence.MappedSuperclass")
}

configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
}

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'com.fasterxml.jackson.module:jackson-module-kotlin'
    implementation 'org.jetbrains.kotlin:kotlin-reflect'
    developmentOnly 'org.springframework.boot:spring-boot-devtools'

    implementation 'org.hibernate.search:hibernate-search-mapper-orm:6.1.8.Final'
    implementation 'org.hibernate.search:hibernate-search-backend-lucene:6.1.8.Final'

    runtimeOnly 'org.postgresql:postgresql'

    annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

tasks.withType(KotlinCompile) {
    kotlinOptions {
        freeCompilerArgs = ['-Xjsr305=strict']
        jvmTarget = '17'
    }
}

(Note: The code sections you provided appear to have HTML encoding, such as " and <. Make sure to use the appropriate characters in your actual code, as these are used to represent quotation marks and angle brackets.)

英文:

I'm trying make test application using Hibernate Search. I have note repository which
extend SimpleJpaRepository with required parameter jakarta.persistence.EntityManager. But when i try to create search session Search.session(entityManager) I get an error that the Search.session() need javax.persistence.EntityManager. So question is how can i create Search session using jakarta.persistence.EntityManager?

NoteRepository:

@Repository
@Transactional
class NoteRepositoryImpl(
private val entityManager: EntityManager,
) : SimpleJpaRepository<NoteEntity, Long>(NoteEntity::class.java, entityManager), NoteRepository {
override fun search(query: String): List<NoteEntity> {
val session = Search.session(entityManager)
val result = session.search(NoteEntity::class.java)
.where { it.match().fields("title", "tag", "content").matching(query) }
.fetch(0, 100)
return result.hits().map { it as NoteEntity }
}
}

Gradle file:

plugins {
id 'org.springframework.boot' version '3.0.4'
id 'io.spring.dependency-management' version '1.1.0'
id 'org.jetbrains.kotlin.jvm' version '1.7.22'
id 'org.jetbrains.kotlin.plugin.spring' version '1.7.22'
id "org.jetbrains.kotlin.plugin.jpa" version "1.7.22"
id 'org.jetbrains.kotlin.plugin.allopen' version '1.7.22'
}
allOpen {
annotation("jakarta.persistence.Entity")
annotation("jakarta.persistence.Embeddable")
annotation("jakarta.persistence.MappedSuperclass")
}
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'com.fasterxml.jackson.module:jackson-module-kotlin'
implementation 'org.jetbrains.kotlin:kotlin-reflect'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
implementation 'org.hibernate.search:hibernate-search-mapper-orm:6.1.8.Final'
implementation 'org.hibernate.search:hibernate-search-backend-lucene:6.1.8.Final'
runtimeOnly 'org.postgresql:postgresql'
annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
tasks.withType(KotlinCompile) {
kotlinOptions {
freeCompilerArgs = ['-Xjsr305=strict']
jvmTarget = '17'
}
}

答案1

得分: 3

由于您正在使用基于jakarta.*的Spring Boot 3,您需要使用Jakarta版本的orm-mapper:

implementation 'org.hibernate.search:hibernate-search-mapper-orm-jakarta:6.1.8.Final'
英文:

Since you are using Spring Boot 3, which is based on jakarta.* you'd need to use a jakarta version of orm-mapper:

implementation 'org.hibernate.search:hibernate-search-mapper-orm-jakarta:6.1.8.Final'

huangapple
  • 本文由 发表于 2023年3月31日 22:49:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/75899893.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定