Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
179 views
in Technique[技术] by (71.8m points)

java - JPA error object references an unsaved transient instance when not send json parameter

Have API created with springboot, JPA have this situation, send JSON to controller to save data :

@PostMapping(value = "/eform/save", produces = {"application/json"})
    public ResponseEntity<?> saveEform(@Valid @RequestBody EformDto req) throws URISyntaxException {
        // TODO Auto-generated method stub


        logger.info("
********************* POST /eform/save");
        Eform model = eformMapper.toModel(req);
        Optional<Eform> opt = Optional.ofNullable(eformRepository.save(model));
        ResponseEntity<?> r = opt.isPresent()
                ? Util.getResponseEntity(RcDescription._RC_00, RcDescription._DESC_SUCCESS, opt.get().getId() )
                : Util.getResponseEntity(RcDescription._RC_01, RcDescription._DESC_DATA_NOT_FOUND, null);

        logger.info("Response : 
"+r+"
*********************
");
        return r;
    }

EformDto to collect the json :

@Data
@AllArgsConstructor
@NoArgsConstructor
public class EformDto {
    private Long id;
    private Long document;
}

the model Eform :

@Setter
@Getter
@Entity
@Table(name = "eform")
@Data
public class Eform implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @SequenceGenerator(name = "eform_id_seq", sequenceName = "eform_id_seq", allocationSize = 1)
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "eform_id_seq")
    @Column(name = "id", nullable = false)
    private Long id;

    @ManyToOne
    @JoinColumn(name="document", nullable = true)
    private Document documentBean;
}

the EformMapper :

@Mapping(source = "e.documentBean.id", target = "document")
EformDto toDto(Eform e);
@Mapping(source = "eDto.document", target = "documentBean.id")
Eform toModel(EformDto eDto);

Document model :

@Setter
@Getter
@Entity
@Table(name = "documents")
public class Document {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name="id")
    private Long id;

    //bi-directional many-to-one association to UserDevice
    @OneToMany(mappedBy="documentBean", cascade = CascadeType.ALL)
    private List<Eform> eformdoc;
 }

and the json try send to controller : { "id":1, "document":2 }

that's work, but sometimes "document" is not available that means fill with "null" and not send "document" parameter : { "id":1 }

but got this error :

org.hibernate.TransientPropertyValueException: object references an unsaved transient instance - save the transient instance before flushing : id.digisign.dbapi.model.Eform.documentBean -> id.digisign.dbapi.model.Document; nested exception is java.lang.IllegalStateException: org.hibernate.TransientPropertyValueException: object references an unsaved transient instance - save the transient instance before flushing : id.digisign.dbapi.model.Eform.documentBean -> id.digisign.dbapi.model.Document

have try with Cascade, and nullable but not working.

question from:https://stackoverflow.com/questions/65951348/jpa-error-object-references-an-unsaved-transient-instance-when-not-send-json-par

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...