简体   繁体   中英

Using JPA + H2 with Spring Boot + Kotlin - Error booting

I have a very simple sample Spring Boot + Kotlin project. I added all the basic dependencies:

    <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>

I have annotated two model classes with JPA annotations:

@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()
)

and

@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
)

I have a basic Main:

@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.

Could you give me some clue on this? I googled the errors but the answers are too unrelated. Thanks.

You have a typo in your code. The attribute in book is called author instead of authors.

So this is the correct code.

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

Always look at the last exception in the 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM