代码之家  ›  专栏  ›  技术社区  ›  Luís Soares Cilan

将JPA+H2与Spring Boot+Kotlin一起使用-错误引导

  •  0
  • Luís Soares Cilan  · 技术社区  · 7 年前

    我有一个非常简单的 sample 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
    )
    

    我有一个基本的主线:

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

    But when I boot, I get a bit error stack.

    你能给我一些线索吗?我在谷歌上搜索了错误,但答案太不相关了。谢谢

    1 回复  |  直到 7 年前
        1
  •  2
  •   Simon Martinelli    7 年前

    您的代码中有一个输入错误。 书中的属性称为author,而不是authors。

    所以这是正确的代码。

    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