babyfish-ct / jimmer

A revolutionary ORM framework for both java and kotlin.
Apache License 2.0
827 stars 80 forks source link

[BUG] 主子表保存时期望子表自动设置父id时报错 #645

Closed pot-mot closed 2 months ago

pot-mot commented 2 months ago

环境:

基本实体:

@Entity
interface Master {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    val id: Int

    @Column(name = "`value`")
    val value: String

    @OneToOne(mappedBy = "master")
    val slave: Slave?
}
@Entity
interface Slave {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    val id: Int

    @Column(name = "`value`")
    val value: String

    @Key
    @OneToOne
    @JoinColumn(name = "master_id")
    val master: Master
}

此时插入报错:

sqlClient.insert(Master {
    value = "master1"
    slave = Slave {
        value = "slave1"
    }
})
java.lang.IllegalArgumentException: Cannot create prop expression for java/kotlin based association property "com.example.entity.Slave.master"
    at org.babyfish.jimmer.sql.ast.impl.PropExpressionImpl.of(PropExpressionImpl.java:84)
    at org.babyfish.jimmer.sql.ast.impl.table.TableImpl.get(TableImpl.java:240)
    at org.babyfish.jimmer.sql.ast.impl.table.TableImpl.get(TableImpl.java:210)
    at org.babyfish.jimmer.sql.ast.impl.mutation.Rows.lambda$findMapByKeys$1(Rows.java:71)
    at org.babyfish.jimmer.sql.ast.impl.mutation.Rows.lambda$findRows$2(Rows.java:123)
    at org.babyfish.jimmer.sql.ast.impl.query.Queries.createQuery(Queries.java:61)
    at org.babyfish.jimmer.sql.ast.impl.mutation.Rows.lambda$findRows$3(Rows.java:117)
    at org.babyfish.jimmer.runtime.Internal.requiresNewDraftContext(Internal.java:136)
    at org.babyfish.jimmer.sql.ast.impl.mutation.Rows.findRows(Rows.java:116)
    at org.babyfish.jimmer.sql.ast.impl.mutation.Rows.findMapByKeys(Rows.java:68)
    at org.babyfish.jimmer.sql.ast.impl.mutation.AbstractPreHandler.findOldMapByKeys(PreHandler.java:260)
    at org.babyfish.jimmer.sql.ast.impl.mutation.UpsertPreHandler.onResolve(PreHandler.java:720)
    at org.babyfish.jimmer.sql.ast.impl.mutation.AbstractPreHandler.resolve(PreHandler.java:428)
    at org.babyfish.jimmer.sql.ast.impl.mutation.UpsertPreHandler.insertedMap(PreHandler.java:668)
    at org.babyfish.jimmer.sql.ast.impl.mutation.PreHandler$1.iterator(PreHandler.java:72)
    at org.babyfish.jimmer.sql.ast.impl.mutation.Saver.saveSelf(Saver.java:207)
    at org.babyfish.jimmer.sql.ast.impl.mutation.Saver.saveAllImpl(Saver.java:107)
    at org.babyfish.jimmer.sql.ast.impl.mutation.Saver.savePostAssociation(Saver.java:196)
    at org.babyfish.jimmer.sql.ast.impl.mutation.Saver.saveAllImpl(Saver.java:116)
    at org.babyfish.jimmer.sql.ast.impl.mutation.Saver.lambda$save$0(Saver.java:48)
    at org.babyfish.jimmer.runtime.Internal.modifyDraft(Internal.java:173)
    at org.babyfish.jimmer.runtime.Internal.lambda$produce$1(Internal.java:42)
    at org.babyfish.jimmer.runtime.Internal.usingDraftContext(Internal.java:102)
    at org.babyfish.jimmer.runtime.Internal.produce(Internal.java:39)
    at org.babyfish.jimmer.sql.ast.impl.mutation.Saver.save(Saver.java:44)
    at org.babyfish.jimmer.sql.ast.impl.mutation.SimpleEntitySaveCommandImpl.executeImpl(SimpleEntitySaveCommandImpl.java:45)
    at org.babyfish.jimmer.spring.cfg.support.SpringConnectionManager.execute(SpringConnectionManager.java:31)
    at org.babyfish.jimmer.sql.ast.impl.mutation.SimpleEntitySaveCommandImpl.execute(SimpleEntitySaveCommandImpl.java:37)
    at org.babyfish.jimmer.sql.ast.impl.mutation.SimpleEntitySaveCommandImpl.execute(SimpleEntitySaveCommandImpl.java:16)
    at org.babyfish.jimmer.sql.kt.impl.KEntitiesImpl.save(KEntitiesImpl.kt:199)
    at org.babyfish.jimmer.sql.kt.KEntities.save$default(KEntities.kt:60)
    at org.babyfish.jimmer.sql.kt.KSqlClient.save(KSqlClient.kt:120)
    at org.babyfish.jimmer.sql.kt.KSqlClient.insert(KSqlClient.kt:128)
    at com.example.TestOneToOneSave.test(TestOneToOneSave.kt:16)
    at java.base/java.lang.reflect.Method.invoke(Method.java:580)
    at java.base/java.util.ArrayList.forEach(ArrayList.java:1596)
    at java.base/java.util.ArrayList.forEach(ArrayList.java:1596)

复现项目: jimmer-simple-kotlin-k2.zip 运行测试即可复现

pot-mot commented 2 months ago

似乎是key与关联父属性为同一属性导致的问题,分步save同样报错:

@Test
fun test() {
    val master1 = sqlClient.insert(Master {
        value = "master1"
    }).modifiedEntity

    sqlClient.save(Slave {
        value = "slave1"
        masterId = master1.id
    })
}
babyfish-ct commented 2 months ago

Try 0.8.160