Skip to content

[BUG] - The behavior of the save command when saving intermediate tables is inconsistent with the document. #1354

@L1yp

Description

@L1yp

Jimmer Version

0.10.6

JDK Version

JDK 21

Database

SQLite

OS

Windows

Expected behavior

Automatically handle duplicate primary keys when saving intermediate tables

Actual behavior

Cannot execute SQL statement: insert into USER_ROLE_MAPPING(USER_ID, ROLE_ID) values(?, ?), variables: [1, 1]
org.babyfish.jimmer.sql.exception.ExecutionException: Cannot execute SQL statement: insert into USER_ROLE_MAPPING(USER_ID, ROLE_ID) values(?, ?), variables: [1, 1]
at org.babyfish.jimmer.sql.runtime.DefaultExecutor.execute(DefaultExecutor.java:73)
at org.babyfish.jimmer.sql.runtime.ExecutorForLog.prettyLog(ExecutorForLog.java:151)

Description

The error occurred because the direct INSERT operation when saving the intermediate table did not handle duplicate entries.

@SpringBootTest
class UserRoleTests {

    @Resource
    lateinit var sqlClient: KSqlClient


    @Test
    fun test() {
        val user = User {
            this.id = 1
            this.createdAt = System.currentTimeMillis()
            this.roleList().addBy {
                this.id = 1
                this.createdAt = System.currentTimeMillis()
            }
            this.roleList().addBy {
                this.id = 2
                this.createdAt = System.currentTimeMillis()
            }
        }
        sqlClient.save(user) {
            setMode(SaveMode.INSERT_IF_ABSENT)
            setAssociatedMode(User::roleList, AssociatedSaveMode.APPEND_IF_ABSENT)
        }
        sqlClient.save(user) {
            setMode(SaveMode.INSERT_IF_ABSENT) // UPSERT mode is OK
            setAssociatedMode(User::roleList, AssociatedSaveMode.APPEND_IF_ABSENT)
        }
    }
}
2026-03-08T21:51:19.148+08:00  INFO 115272 --- [jimmer-example] [    Test worker] o.b.jimmer.sql.runtime.ExecutorForLog    : Execute SQL===>
Purpose: MUTATE
SQL: insert into USER(
    ID, CREATED_AT
) values(
        ? /* 1 */,
        ? /* 1772977878995 */
) on conflict(
    ID
) do nothing
Affected row count: 0
JDBC response status: success
Time cost: 0ms
<===Execute SQL
2026-03-08T21:51:19.149+08:00  INFO 115272 --- [jimmer-example] [    Test worker] o.b.jimmer.sql.runtime.ExecutorForLog    : Execute SQL===>
Purpose: MUTATE
SQL: insert into ROLE(
    ID, CREATED_AT
) values(
        ? /* 1 */,
        ? /* 1772977879000 */
) on conflict(
    ID
) do nothing
Affected row count: 0
JDBC response status: success
Time cost: 0ms
<===Execute SQL
2026-03-08T21:51:19.150+08:00  INFO 115272 --- [jimmer-example] [    Test worker] o.b.jimmer.sql.runtime.ExecutorForLog    : Execute SQL===>
Purpose: MUTATE
SQL: insert into ROLE(
    ID, CREATED_AT
) values(
        ? /* 2 */,
        ? /* 1772977879000 */
) on conflict(
    ID
) do nothing
Affected row count: 0
JDBC response status: success
Time cost: 0ms
<===Execute SQL
2026-03-08T21:51:19.153+08:00  INFO 115272 --- [jimmer-example] [    Test worker] o.b.jimmer.sql.runtime.ExecutorForLog    : Execute SQL===>
Purpose: MUTATE
SQL: insert into USER_ROLE_MAPPING(USER_ID, ROLE_ID) values(? /* 1 */, ? /* 1 */)
JDBC response status: failed<org.babyfish.jimmer.sql.exception.ExecutionException>
Time cost: 1ms
<===Execute SQL

Cannot execute SQL statement: insert into USER_ROLE_MAPPING(USER_ID, ROLE_ID) values(?, ?), variables: [1, 1]
org.babyfish.jimmer.sql.exception.ExecutionException: Cannot execute SQL statement: insert into USER_ROLE_MAPPING(USER_ID, ROLE_ID) values(?, ?), variables: [1, 1]
	at org.babyfish.jimmer.sql.runtime.DefaultExecutor.execute(DefaultExecutor.java:73)
	at org.babyfish.jimmer.sql.runtime.ExecutorForLog.prettyLog(ExecutorForLog.java:151)

Reproduction steps

reproduced repo

run v.boot.UserRoleTests#test

Generated SQL

No response

Relation Model

No response

Screenshots

No response

Logs

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions