From 46e18c9efa89613eaffcbef31142b1453c457f93 Mon Sep 17 00:00:00 2001 From: vince Date: Mon, 8 Jul 2024 20:55:24 +0800 Subject: [PATCH] flushChanges & delete throw SQLException --- .../src/main/kotlin/org/ktorm/entity/Entity.kt | 3 +++ .../src/test/kotlin/org/ktorm/entity/EntityTest.kt | 14 ++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/ktorm-core/src/main/kotlin/org/ktorm/entity/Entity.kt b/ktorm-core/src/main/kotlin/org/ktorm/entity/Entity.kt index b0f00884..83fc2997 100644 --- a/ktorm-core/src/main/kotlin/org/ktorm/entity/Entity.kt +++ b/ktorm-core/src/main/kotlin/org/ktorm/entity/Entity.kt @@ -23,6 +23,7 @@ import java.io.ObjectInputStream import java.io.ObjectOutputStream import java.io.Serializable import java.lang.reflect.Proxy +import java.sql.SQLException import kotlin.reflect.KClass import kotlin.reflect.full.isSubclassOf import kotlin.reflect.jvm.jvmErasure @@ -168,6 +169,7 @@ public interface Entity> : Serializable { * @see add * @see update */ + @Throws(SQLException::class) public fun flushChanges(): Int /** @@ -191,6 +193,7 @@ public interface Entity> : Serializable { * @see update * @see flushChanges */ + @Throws(SQLException::class) public fun delete(): Int /** diff --git a/ktorm-core/src/test/kotlin/org/ktorm/entity/EntityTest.kt b/ktorm-core/src/test/kotlin/org/ktorm/entity/EntityTest.kt index f3bb3211..b7c20047 100644 --- a/ktorm-core/src/test/kotlin/org/ktorm/entity/EntityTest.kt +++ b/ktorm-core/src/test/kotlin/org/ktorm/entity/EntityTest.kt @@ -1,5 +1,6 @@ package org.ktorm.entity +import org.h2.jdbc.JdbcSQLIntegrityConstraintViolationException import org.junit.Test import org.ktorm.BaseTest import org.ktorm.database.Database @@ -397,6 +398,19 @@ class EntityTest : BaseTest() { assert(e.flushChanges() == 1) } + @Test + fun testExceptionThrowsByProxy() { + try { + val e = database.employees.find { it.id eq 1 } ?: throw AssertionError() + e.department = Department() + e.flushChanges() + + throw AssertionError("failed") + } catch (e: JdbcSQLIntegrityConstraintViolationException) { + assert(e.message!!.contains("NULL not allowed for column \"department_id\"")) + } + } + @Test fun testHasColumnValue() { val p1 = Parent()