Skip to content

解决save的时候不产生select语句,直接insert,提高批量操作是提升insert的效率 #31

@zhangzhenhuajack

Description

@zhangzhenhuajack

第一步实体 implements Persistable, Serializable

第二步重写 isNew方法即可;

参考如下实体:

@Builder
@Getter
@Setter
@ToString
@Entity
@NoArgsConstructor
@AllArgsConstructor
@Table(catalog = "user_gold", name = "user_goldjours_shard", indexes = {@Index(unique = true, columnList = "business_code_hash", name = "ux_on_business_code_hash"), @Index(columnList = "uuid,context_type,context_id", name = "idx_on_uuid_and_context_type_and_context_id"), @Index(columnList = "uuid,reason", name = "idx_on_uuid_and_reason")})
@org.hibernate.annotations.Table(appliesTo = "user_goldjours_shard", comment = "用户金贝流水拆分表")//为了给表添加注释
@EntityListeners(AuditingEntityListener.class)
public class UserGoldJoursShard implements Persistable<Long>, Serializable {

    private static final long serialVersionUID = 2225926240419540529L;

    /**
     * 利用hibernate 的 table generate id策略生成id,防止表与表之间ID冲突
     */
    @Column(nullable = false)
    @Id
    private Long id;
    @Column(columnDefinition = "varchar(255) DEFAULT NULL COMMENT '用户的 UUID'")
    private String uuid;
    @Column(columnDefinition = "int(11) DEFAULT NULL COMMENT '变动前数量'")
    private Long currentAmount;
 
    @Version
    private Long version; //必须包含version字段,解决detached entity passed to persist异常
    @Transient
    boolean defaultNew = false;

    @Override
    public boolean isNew() {
        return null == getId() || defaultNew;
    }

    public void setDefaultNew(boolean defaultNew) {
        this.defaultNew = defaultNew;
    }
}

当新增UserGoldJoursShard对象的时候设置defaultNew=true,调用实例如下:

    @Transient
    public UserGoldJoursShard initUserGoldShell() {
        UserGoldJoursShard userGoldShell = new UserGoldJoursShard();
        BeanUtils.copyProperties(this, userGoldShell);
        userGoldShell.setId(this.getId());
        //解决insert之前生成select查询的问题
        userGoldShell.setDefaultNew(true);
        //保留老表ID,防止异常情况需要更新回来
        userGoldShell.setOldTableId(getId());
        return userGoldShell;
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions