Version 1.1.3

- Add check on references
This commit is contained in:
2024-12-05 20:13:41 +01:00
parent cbeda381cc
commit be16104216
3 changed files with 15 additions and 3 deletions

View File

@@ -20,10 +20,16 @@ class Reference<S : Persistable>(
var id: Long = initialValue.id
operator fun getValue(thisRef: Persistable, property: KProperty<*>): S {
check(currentTransaction() != null) {
"No transaction available"
}
return currentTransaction()?.find(cls.kotlin, id) ?: throw IllegalStateException("Reference not found")
}
operator fun setValue(thisRef: Persistable, property: KProperty<*>, value: S) {
check(currentTransaction() != null) {
"No transaction available"
}
currentTransaction()?.store(value)
id = value.id
}
@@ -40,10 +46,16 @@ class NullableReference<S : Persistable>(
var id: Long? = initialValue?.id
operator fun getValue(thisRef: Persistable, property: KProperty<*>): S? {
check(currentTransaction() != null) {
"No transaction available"
}
return currentTransaction()?.find(cls.kotlin, id ?: 0L)
}
operator fun setValue(thisRef: Persistable, property: KProperty<*>, value: S?) {
check(currentTransaction() != null) {
"No transaction available"
}
if (value != null) {
// todo: only store if not already stored?
currentTransaction()?.store(value)