簡體   English   中英

將JPA + H2與Spring Boot + Kotlin一起使用-引導錯誤

[英]Using JPA + H2 with Spring Boot + Kotlin - Error booting

我有一個非常簡單的示例 Spring Boot + Kotlin項目。 我添加了所有基本依賴項:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.jetbrains.kotlin</groupId>
        <artifactId>kotlin-stdlib-jdk8</artifactId>
        <version>${kotlin.version}</version>
    </dependency>
    <dependency>
        <groupId>org.jetbrains.kotlin</groupId>
        <artifactId>kotlin-reflect</artifactId>
    </dependency>
    <dependency>
        <groupId>org.jetbrains.kotlin</groupId>
        <artifactId>kotlin-test</artifactId>
        <version>${kotlin.version}</version>
        <scope>test</scope>
    </dependency>

我已經用JPA注釋為兩個模型類添加了注釋:

@Entity
class Author(
     @Id @GeneratedValue(strategy = GenerationType.AUTO) val id: Long,
     val firstName: String,
     val lastName: String,
     @ManyToMany(mappedBy = "authors") val books: Set<Book> = emptySet()
)

@Entity
class Book(
     @Id @GeneratedValue(strategy = GenerationType.AUTO)
     val id: Long,
     @ManyToMany @JoinTable(
          name = "author_book",
          joinColumns = [JoinColumn(name = "book_id")],
          inverseJoinColumns = [(JoinColumn(name = "author_id"))])
     val author: Set<Author> = emptySet(),
     val title: String,
     val label: String,
     val publisher: String
)

我有一個基本的Main:

@SpringBootApplication
open class Spring5webappApplication {

    companion object {
        @JvmStatic
        fun main(args: Array<String>) {
            SpringApplication.run(Spring5webappApplication::class.java, *args)
        }
    }
}

但是當我啟動時,我得到了一些錯誤堆棧。

你能給我一些線索嗎? 我在錯誤中進行了搜索,但答案卻無關緊要。 謝謝。

您的代碼中有錯字。 書中的屬性稱為作者而不是作者。

所以這是正確的代碼。

val authors: Set<Author> = emptySet(),

始終查看stacktrace中的最后一個異常

Caused by: org.hibernate.AnnotationException: 
    mappedBy reference an unknown target entity property: 
    guru.springframework.spring5webapp.model.Book.authors in guru.springframework.spring5webapp.model.Author.books

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM