Skip to Content
Kotlin 编程实践
book

Kotlin 编程实践

by Ken Kousen
July 2020
Intermediate to advanced
235 pages
3h 37m
Chinese
China Machine Press
Content preview from Kotlin 编程实践
194
12
些注解的被 Spring 管理的 Bean 都会自动被标注 open 以用于扩展,这通常是你所需要的。
如果你需要做的还不止于此,仍然可以添加
all-open
插件,但这通常不是必需的。
要查看使用相同插件的 Maven 构建,请使用 Initializr 生成 Maven 项目。但
是,这些概念是相同的。
参阅
kotlin-jpa
插件将在 12.2 节中讨论。
12.2
持久化
Kotlin
数据类
问题
如何在 Kotlin 数据类上使用 Java 持久化 APIJPA)?
解决方案
kotlin-jpa
插件添加到你的构建文件。
讨论
通常,在定义 Kotlin 数据类时,可以将所有必要的属性添加到主构造函数中,参见示例
12-2
示例 12-2:一个拥有主构造函数的数据类
data class Person(val name: String,
val dob: LocalDate)
JPA 的角度来看,这里有两个问题。首先,JPA 需要一个默认的构造函数,除非你
为所有属性都提供默认值,否则该类就不存在这样的构造函数。其次,在数据类中使
val 属性,可以生成不可变的对象,而 JPA 没有被设计为与不可变的对象完美地结
合使用。
首先解决默认构造函数问题,Kotlin 提供了两个插件来解决该问题。使用
no-arg
插件,
你可以选择应为哪些类提供无参构造函数,并可以定义注解来调用它们。其次,在此基
Spring
框架
195
础上,构建
kotlin-jpa
插件。该插件会使用默认构造函数自动配置 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

GraphQL 学习指南

GraphQL 学习指南

Eve Porcello, Alex Banks
Java 技术手册(原书第7版)

Java 技术手册(原书第7版)

Benjamin J. Evans, David Flanagan
Python机器学习基础教程

Python机器学习基础教程

Andreas C. Müller, Sarah Guido

Publisher Resources

ISBN: 9787111659624