0

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.

2
  • 1
    Reflectkotlinclass is not available at run time, can you check if correct and all dependencies are added? Commented Jan 11, 2018 at 1:43
  • I'll add kotlin-reflect artifact and update the question. Commented Jan 11, 2018 at 1:46

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the tip! Btw, do you know why kotlin-reflect is needed?
Because Kotlin has his own reflection API and if you have a library that has native support for Kotlin this reflection dependency is needed. I would recommend to always add it. What Spring Boot version are you using?
Thanks. I'm using 2.0.0.M7 (it's just a Udemy sample project)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.