[英]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.