nullable, non-nullable reference

This commit is contained in:
2024-05-06 20:11:01 +02:00
parent 5fe320581b
commit 68562160f1
2 changed files with 50 additions and 21 deletions

View File

@@ -5,6 +5,7 @@ import nl.astraeus.nl.astraeus.persistence.Persistent
import nl.astraeus.nl.astraeus.persistence.TransactionLog
import nl.astraeus.nl.astraeus.persistence.reference.reference
import nl.astraeus.nl.astraeus.persistence.reference.referenceCollection
import org.junit.jupiter.api.assertThrows
import java.io.File
import kotlin.test.Test
@@ -31,15 +32,16 @@ class TestReferences {
override var version: Long = 0,
val name: String,
val age: Int,
company: Company
) : Persistable, Cloneable {
var company: Company? by reference()
var company: Company by reference(company)
companion object {
private const val serialVersionUID: Long = 1L
}
override fun toString(): String {
return "Person(id=$id, version=$version, name='$name', age=$age, company=$company)"
return "Person(id=$id, version=$version, name='$name', age=$age)"
}
}
@@ -57,31 +59,35 @@ class TestReferences {
)
pst.transaction {
val company = find(Company::class, 1L) ?: Company(
val company = Company(
id = 1L,
name = "ACME"
)
if (company.persons.isEmpty()) {
val person = Person(
id = 0L,
name = "John Doe",
age = 25
)
person.company = company
store(person)
company.persons.add(person)
} else {
for (person in company.persons) {
println("Found stored Person: $person")
}
}
val person = Person(
id = 0L,
name = "John Doe",
age = 25,
company = company
)
store(person)
company.persons.add(person)
store(company)
for (person in company.persons) {
println("Person: $person")
for (p in company.persons) {
println("Person: $p")
}
delete(company)
// company is gone, can't get it through person anymore
assertThrows<IllegalStateException> {
println("Company in person: ${person.company}")
}
delete(person)
}
}