一对一和mapsId的Spring Data JPA错误

时间:2019-01-18 00:23:25

标签: hibernate spring-data-jpa spring-data one-to-one

我的父母有:

@Entity
@Table(name="engineering.drawingrevisions")
public class DrawingRevision implements DrawingRevisionInterface, Serializable {
    private int drawingRevisionID;
    private DrawingRevisionManufacturing manufacturing;

    @Id
    @Column(name="pk_drawingrevisionid")
    @GeneratedValue(strategy = GenerationType.IDENTITY)    
    @Override
    public int getDrawingRevisionID() {
        return drawingRevisionID;
    }

    public void setDrawingRevisionID(int drawingRevisionID) {
        this.drawingRevisionID = drawingRevisionID;
    }

    @OneToOne(mappedBy="drawingRevision", cascade=CascadeType.ALL, fetch=FetchType.LAZY)
    @JsonView({View.DrawingRevisionView.class,View.DrawingView.class,View.RFQueueView.class})
    public DrawingRevisionManufacturing getManufacturing() {
        return manufacturing;
    }

    public void setManufacturing(DrawingRevisionManufacturing manufacturing) {
        if (manufacturing==null) {
            if (this.manufacturing!=null) {
                this.manufacturing.setDrawingRevision(null);
            }
        } else {
            manufacturing.setDrawingRevision(this);
        }
        this.manufacturing = manufacturing;
    }
}

以及相应的孩子:

@Entity
@Table(name="engineering.drawingrevision_manufacturing")
public class DrawingRevisionManufacturing implements Serializable {
    private static final long serialVersionUID = 1L;

    private int drawingRevisionID;
    private DrawingRevision drawingRevision;

    public DrawingRevisionManufacturing() {
    }

    @Id
    @Column(name="fk_drawingrevisionid")
    public int getDrawingRevisionID() {
        return drawingRevisionID;
    }

    public void setDrawingRevisionID(int drawingRevisionID) {
        this.drawingRevisionID = drawingRevisionID;
    }

    @OneToOne
    @MapsId("drawingRevisionID")
    @JoinColumn(name="fk_drawingrevisionid")
    public DrawingRevision getDrawingRevision() {
        return drawingRevision;
    }

    public void setDrawingRevision(DrawingRevision drawingRevision) {
        this.drawingRevision = drawingRevision;
    }
}

当我有一个新的DrawingRevision且drawingRevisionID = 0时,问题浮出水面。我期望的是DrawingRevisionManufacturing将根据CascadeType.ALL正确更新其ID。

在我的服务层中,请确保存在关联:

if (rev.getManufacturing().getDrawingRevisionID()==0) {
    rev.getManufacturing().setDrawingRevision(rev);
}

使用此代码,然后我得到了一个外键约束违规,因为它试图在drawingRevision_manufacturing中插入一个带有drawingRevisionID为0的条目(这不好)。

如果我将setDrawingRevision()更改为

public void setDrawingRevision(DrawingRevision drawingRevision) {
    this.drawingRevision = drawingRevision;
    this.drawingRevisionID = drawingRevision.getDrawingRevisionID();
}

然后我收到一个休眠错误,实例的标识符从0更改为10322(或任何数字)。

我缺少什么可以使我成功保存新的DrawingRevision?

1 个答案:

答案 0 :(得分:0)

在DrawingRevisionManufacturing实体中添加以下代码,

@OneToOne
@MapsId("drawingRevisionID")
@JoinColumn(name="fk_drawingrevisionid" ,referencedColumnName ="pk_drawingrevisionid")
public DrawingRevision getDrawingRevision() {
    return drawingRevision;
}

如有任何问题,请告知