This project contains samples of Kotlin-specific features of Spring Data (Cassandra).
Kotlin allows defaulting for constructor- and method arguments.
Defaulting allows usage of substitute values if a field in the document is absent or simply null
.
Spring Data inspects objects whether they are Kotlin types and uses the appropriate constructor.
@Table
data class Person(@PrimaryKeyColumn(type = PrimaryKeyType.PARTITIONED) val firstname: String? = "", val lastname: String = "White")
operations.cqlOperations.execute(QueryBuilder.insertInto("person").value("firstname", "Walter"))
val person = operations.query<Person>()
.matching(query(where("firstname").isEqualTo("Walter")))
.firstValue()!!
assertThat(person.lastname).isEqualTo("White")
Spring Data exposes methods accepting a target type to either query for or to project results values on.
Kotlin represents classes with its own type, KClass
which can be an obstacle when attempting to obtain a Java Class
type.
Spring Data ships with extensions that add overloads for methods accepting a type parameter by either leveraging generics or accepting KClass
directly.
operations.getTableName<Person>()
operations.getTableName(Person::class)
Declaring repository interfaces using Kotlin allows expressing nullability constraints on arguments and return types. Spring Data evaluates nullability of arguments and return types and reacts to these. Passing null
to a non-nullable argument raises an IllegalArgumentException
, as you're already used to from Kotlin. Spring Data helps you also to prevent null
in query results. If you wish to return a nullable result, use Kotlin's nullability marker ?
. To prevent null
results, declare the return type of a query method as non-nullable. In the case a query yields no result, a non-nullable query method throws EmptyResultDataAccessException
.
interface PersonRepository : CrudRepository<Person, String> {
/**
* Query method declaring a nullable return type that allows to return null values.
*/
fun findOneOrNoneByFirstname(firstname: String): Person?
/**
* Query method declaring a nullable argument.
*/
fun findNullableByFirstname(firstname: String?): Person?
/**
* Query method requiring a result. Throws [org.springframework.dao.EmptyResultDataAccessException] if no result is found.
*/
fun findOneByFirstname(firstname: String): Person
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。