babyfish-ct / jimmer

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

[BUG] 多层级联删除时多对多属性校验出错 #647

Closed pot-mot closed 2 months ago

pot-mot commented 2 months ago

环境

实体结构

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

    val name: String

    @OneToMany(mappedBy = "model")
    val entities: List<GenEntity>
}
@Entity
interface GenEntity {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    val id: Long

    @ManyToOne
    @OnDissociate(DissociateAction.DELETE)
    val model: GenModel

    /**
     * 上级实体
     */
    @ManyToMany
    @JoinTable(
        name = "gen_super_entity_mapping",
        joinColumnName = "inherit_entity_id",
        inverseJoinColumnName = "super_entity_id"
    )
    val superEntities: List<GenEntity>

    /**
     * 上级实体 ID 视图
     */
    @IdView("superEntities")
    val superEntityIds: List<Long>

    /**
     * 继承实体
     */
    @ManyToMany(mappedBy = "superEntities")
    val inheritEntities: List<GenEntity>

    /**
     * 继承实体 ID 视图
     */
    @IdView("inheritEntities")
    val inheritEntityIds: List<Long>

    val name: String
}

表结构


CREATE TABLE `gen_model`
(
    `id`            bigint       NOT NULL AUTO_INCREMENT,
    `name`          varchar(500) NOT NULL,
    PRIMARY KEY (`id`)
);

CREATE TABLE `gen_entity`
(
    `id`            bigint       NOT NULL AUTO_INCREMENT,
    `model_id`      bigint       NOT NULL,
    `name`          varchar(500) NOT NULL,
    CONSTRAINT `fk_entity_model` FOREIGN KEY (`model_id`) REFERENCES `gen_model` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT,
    PRIMARY KEY (`id`)
);

CREATE TABLE `gen_super_entity_mapping`
(
    `super_entity_id`   bigint NOT NULL,
    `inherit_entity_id` bigint NOT NULL,
    PRIMARY KEY (`super_entity_id`, `inherit_entity_id`),
    CONSTRAINT `fk_super_entity_mapping_super_entity` FOREIGN KEY (`super_entity_id`) REFERENCES `gen_entity` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT,
    CONSTRAINT `fk_super_entity_mapping_inherit_entity` FOREIGN KEY (`inherit_entity_id`) REFERENCES `gen_entity` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT
);

抛出异常

val model1 = sqlClient.insert(GenModel {
    name = "model1"
}).modifiedEntity

val superEntity = sqlClient.insert(GenEntity {
    name = "superEntity"
    model = toIdOnly(model1)
}).modifiedEntity

sqlClient.insert(GenEntity {
    name = "inheritEntity"
    model = toIdOnly(model1)
    superEntityIds = listOf(superEntity.id)
})

sqlClient.deleteByIds(GenModel::class, listOf(model1.id))
java.lang.IllegalArgumentException: The back property "top.potmot.entity.GenEntity.superEntities" is not association property with column defined or middle table
    at org.babyfish.jimmer.sql.ast.impl.mutation.DeleteContext.<init>(DeleteContext.java:66)
    at org.babyfish.jimmer.sql.ast.impl.mutation.DeleteContext.backPropOf(DeleteContext.java:96)
    at org.babyfish.jimmer.sql.ast.impl.mutation.MiddleTableOperator.backPropOf(MiddleTableOperator.java:68)
    at org.babyfish.jimmer.sql.ast.impl.mutation.ChildTableOperator.lambda$middleTableOperators$3(ChildTableOperator.java:538)
    at org.babyfish.jimmer.sql.ast.impl.mutation.AbstractOperator.createMiddleTableOperators(AbstractOperator.java:145)
    at org.babyfish.jimmer.sql.ast.impl.mutation.ChildTableOperator.middleTableOperators(ChildTableOperator.java:533)
    at org.babyfish.jimmer.sql.ast.impl.mutation.ChildTableOperator.disconnect(ChildTableOperator.java:179)
    at org.babyfish.jimmer.sql.ast.impl.mutation.ChildTableOperator.disconnect(ChildTableOperator.java:102)
    at org.babyfish.jimmer.sql.ast.impl.mutation.Deleter.execute(Deleter.java:152)
    at org.babyfish.jimmer.sql.ast.impl.mutation.DeleteCommandImpl.executeImpl(DeleteCommandImpl.java:53)
    at org.babyfish.jimmer.spring.cfg.support.SpringConnectionManager.execute(SpringConnectionManager.java:31)
    at org.babyfish.jimmer.sql.ast.impl.mutation.DeleteCommandImpl.execute(DeleteCommandImpl.java:38)
    at org.babyfish.jimmer.sql.ast.impl.mutation.DeleteCommandImpl.execute(DeleteCommandImpl.java:17)
    at org.babyfish.jimmer.sql.kt.impl.KEntitiesImpl.deleteAll(KEntitiesImpl.kt:273)
    at org.babyfish.jimmer.sql.kt.KEntities.deleteAll$default(KEntities.kt:113)
    at org.babyfish.jimmer.sql.kt.KSqlClient.deleteByIds(KSqlClient.kt:230)
    at org.babyfish.jimmer.sql.kt.KSqlClient.deleteByIds$default(KSqlClient.kt:229)
    at top.potmot.service.ModelService.delete$lambda$15(ModelService.kt:138)
    at org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:140)
    at top.potmot.service.ModelService.delete(ModelService.kt:137)

复现

jimmer-simple-kotlin-k2.zip

babyfish-ct commented 2 months ago

Try 0.8.161