babyfish-ct / jimmer

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

使用createDelete时无法支持逻辑删除 #709

Closed tonyxu721 closed 1 month ago

tonyxu721 commented 1 month ago

@Entity @Table(name = "company_detail") public interface CompanyDetail {

/**
 * id
 */
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
int id();

/**
 * address
 */
@Nullable
String address();

@Nullable
@IdView("company") //与数据库外键字段是否可空保持一致,如果是伪外键,则必须可空
Integer companyId();

@OnDissociate(DissociateAction.DELETE)
@OneToOne
@JoinColumn(name = "company_id", foreignKeyType = ForeignKeyType.FAKE)
@Nullable //与数据库外键字段是否可空保持一致,如果是伪外键,则必须可空
Company company();

}

@Entity @Table(name = "user") public interface User {

/**
 * id
 */
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
int id();

/**
 * name
 */
@Nullable
String name();

/**
 * balance
 */
@Nullable
Integer balance();

/**
 * version
 */
@Nullable
Integer version();

/**
 * create_time
 */
@Column(name = "create_time")
@Nullable
LocalDateTime createTime();

/**
 * update_time
 */
@Column(name = "update_time")
@Nullable
LocalDateTime updateTime();

/**
 * enabled
 */
@Nullable
Integer enabled();

/**
 * deleted
 */
@Nullable
Integer deleted();

/**
 * company_id
 */
@IdView("company") //根据数据库中是否可为空,为空用Integer,不为空用int,关联的ManyToOne对象也保持一致
Integer companyId();

@OnDissociate(DissociateAction.SET_NULL)
@Nullable // 与companyId保持一致
@ManyToOne // inputNotNull = true 必须用于可空的对象,且对应数据库外键字段必须不能为空。
@JoinColumn(name = "company_id", foreignKeyType = ForeignKeyType.REAL)
Company company();

@OneToOne(mappedBy = "user")
@Nullable //一对一镜像端必须可空
UserDetail userDetail();

@ManyToMany //多对多必须不为空
@JoinTable(
    name = "user_role",
    joinColumnName = "user_id",
    inverseJoinColumnName = "role_id"
)
List<Role> roles();

}

3. rest接口
```java
@GetMapping("/test/jimmer/company")
public Result testJimmerCompany() {
    CompanyTable table = Tables.COMPANY_TABLE;
    Integer affectedRowCount = companyRepository.sql()
        .createDelete(table)
        .where(table.id().eq(3))
        .execute();

    return Result.buildSuccess(affectedRowCount);
}
  1. 相关SQL
    
    -- auto-generated definition
    create table company
    (
    id        int auto_increment
        primary key,
    name      varchar(255)         null,
    parent_id int                  null,
    deleted   tinyint(1) default 0 not null
    );

-- auto-generated definition create table company_detail ( id int auto_increment primary key, address varchar(255) null, company_id int null );

create index idx_cid on company_detail (company_id);

-- auto-generated definition create table user ( id int auto_increment primary key, name varchar(255) null, balance int null, version int null, create_time datetime null, update_time datetime null, enabled tinyint(1) null, deleted tinyint(1) null, company_id int null, constraint user_company_id_fk foreign key (company_id) references company (id) );



5. 最后结果
生成的查询语句会带上逻辑删除标志,但是最后company表删除的时候执行的是物理删除
babyfish-ct commented 1 month ago

Try 0.8.187