CRUDRepository无法保存修改的实体

时间:2019-09-23 14:14:06

标签: spring

我正在尝试从数据库中获取一些数据,使用其他实体更新字段并将其保存回数据库,当然,我已经确保第一个实体和将要​​成为实体的实体插入的数据会被检索并很好,只是将其引发保存功能调用。

这是例外

[err] org.springframework.dao.DataIntegrityViolationException: Attempt to persist detached object "repository.entities.RequestEntity-0".  If this is a new instance, make sure any version and/or auto-generated primary key fields are null/default when persisting.; nested exception is <openjpa-2.4.3-r422266:1833086 nonfatal store error> org.apache.openjpa.persistence.EntityExistsException: Attempt to persist detached object "repository.entities.RequestEntity-0".  If this is a new instance, make sure any version and/or auto-generated primary key fields are null/default when persisting.
FailedObject: repository.entities.RequestEntity-0

实体

@Entity
@Table(name="REQUEST")
public class RequestEntity implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @Column(name="REQUEST_ID")
    private long requestId;

some other fields .... 

    //bi-directional many-to-one association to MStatus
    @ManyToOne
    @JoinColumn(name="STATUS")
    private MStatus mStatus;

getters and setters here as well .. 

}

最后,这是代码

private void doStuff() throws Exception {
        List<RequestEntity> requestsList = requestRepo
                .findByMStatusStatusContaining("TEXT");
        RequestEntity requestItem;
        if (requestsList.size() > 1 || requestsList.isEmpty()) {
            throw new Exception("No requests found");
        } else {
            requestItem = requestsList.get(0);
        }
        requestItem.setMApprovalStatus(mapprovalStatus.findOne("TEXT_TWO"));
        requestRepo.save(requestItem);
    }

0 个答案:

没有答案