Skip to content

Commit

Permalink
flushChanges & delete throw SQLException
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentlauvlwj committed Jul 8, 2024
1 parent fb0c257 commit 46e18c9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
3 changes: 3 additions & 0 deletions ktorm-core/src/main/kotlin/org/ktorm/entity/Entity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -168,6 +169,7 @@ public interface Entity<E : Entity<E>> : Serializable {
* @see add
* @see update
*/
@Throws(SQLException::class)
public fun flushChanges(): Int

/**
Expand All @@ -191,6 +193,7 @@ public interface Entity<E : Entity<E>> : Serializable {
* @see update
* @see flushChanges
*/
@Throws(SQLException::class)
public fun delete(): Int

/**
Expand Down
14 changes: 14 additions & 0 deletions ktorm-core/src/test/kotlin/org/ktorm/entity/EntityTest.kt
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 46e18c9

Please sign in to comment.