Home Using JPA + H2 with Spring Boot + Kotlin - Error booting
Reply: 1

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

Luís Soares
1#
Luís Soares Published in 2018-01-11 01:40:08Z

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.

Simon Martinelli
2#
Simon Martinelli Reply to 2018-01-11 08:27:47Z

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
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.318358 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO