JPA属性继承-查看文章

JPA属性继承

发表于:2022-05-13 11:27:00 分类:开发杂记 阅读:41次

项目中有时候需要在JPA的entity中继承父类,如果只是使用extent的话,在数据保存的时候继承的属性是不会写库的。

解决方法:

在父类上打注解

@MappedSuperclass

例如:

package com.bes.gs.gat.process;

import com.bes.gs.boot.core.exception.BusinessException;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;

import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.MappedSuperclass;
import javax.persistence.Transient;
import java.util.ArrayList;
import java.util.List;

/**
 * @author ERSREDMA
 * @date Created in 17:17 2022/5/12
 */
@MappedSuperclass
public abstract class BaseBesSimProcessBean implements BesSimProcessBean{
    private final ObjectMapper mapper = new ObjectMapper();
    @JsonIgnore
    protected String approveHistory;
    protected String handlerTarget;
    protected String processState;
    protected String dataOwner;

    @Override
    public void setApproveResult(List<ApproveBean> list) {
        try {
            this.approveHistory = mapper.writeValueAsString(list);
        } catch (JsonProcessingException e) {
            throw new BusinessException(8002,"格式化处理记录失败");
        }
    }

    @Override
    @Transient
    public List<ApproveBean> getApproveResult() {
        JavaType javaType = mapper.getTypeFactory().constructParametricType(ArrayList.class, ApproveBean.class);
        try {
            return mapper.readValue(getApproveHistory(), javaType);
        } catch (JsonProcessingException e) {
            throw new BusinessException(8003,"解析处理记录失败");
        }
    }

    @Override
    @Basic
    @Column(name = "approve_history")
    public String getApproveHistory() {
        return approveHistory;
    }

    @Override
    public void setApproveHistory(String approveHistory) {
        this.approveHistory = approveHistory;
    }

    @Override
    @Basic
    @Column(name = "handler_target")
    public String getHandlerTarget() {
        return handlerTarget;
    }

    @Override
    public void setHandlerTarget(String handlerTarget) {
        this.handlerTarget = handlerTarget;
    }

    @Override
    @Basic
    @Column(name = "process_state")
    public String getProcessState() {
        return processState;
    }

    @Override
    public void setProcessState(String processState) {
        this.processState = processState;
    }

    @Override
    @Basic
    @Column(name = "data_owner")
    public String getDataOwner() {
        return dataOwner;
    }

    @Override
    public void setDataOwner(String dataOwner) {
        this.dataOwner = dataOwner;
    }
}


关键词:jpa,继承


验证码: