Unable to add child entity - JPA OneToMany relationship

久未见 提交于 2020-08-26 08:30:13

问题


I have been trying to add an entity to database that entity have OneToMany relationship to another entity but when I insert that entity it is saying unable to insert id because it is null.

I have mapped parent child class like this:

@OneToMany(mappedBy = "directory", cascade = CascadeType.ALL)

private List<DirectoryRemarksEntity> remarks;

Child Entity :

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "DIRECTORY_ID")
@JsonIgnore
private DirectoryTableEntity directory;

My json looks like this:

 {
  "state": {
    "id": 2,
    "nameEn": "Dubai",
    "code": "DXB",
    "nameAr": "دبي"
  },
  "segment": {
    "id": 5,
    "name": "Government",
    "alias": "Gov"
  },
  "contactType": {
    "id": 5,
    "name": "FAX"
  },
  "remarks": [
    {
      "remarkValueAR": "EN Remarks",
      "remarkValueEN": "AR RemarksEN ",
      "remarkType": {
        "id": 4,
        "name": "legacy_modifieddate"
      }
    }
  ],
  "isTelSecret": 1,
  "isPublishElectroniceMedia": 0,
  "isTelPublishDa": 1,
  "isPrintARA": 0,
  "isPrintENG": 0,
  "establishmentNameEn": "Establishment Name",
  "establishmentNameAr": "",
  "establishmentSubNameEn": "Subname",
  "establishmentSubNameAr": "Arabicname",
  "contactCountryCode": "92",
  "contactNumber": "3042501110",
  "streetAR": "",
  "streetEN": "",
  "houseNumber": "",
  "flatNo": "",
  "buildingAR": "",
  "buildingEN": "",
  "proffesionAR": "",
  "proffesionEN": "",
  "latitude": "",
  "longitude": "",
  "accountProductCode": "00",
  "subSegment": "",
  "companyNameAr": "",
  "companyNameEn": "",
  "email": "",
  "url": "",
  "ISUrl": "",
  "ticketsFeesEN": "",
  "ticketsFeesAR": "",
  "timingsAR": "",
  "timingsEN": "",
  "streetEn": "SZR"
}

This is the error hibernate is throwing:

Error Msg = ORA-01400: cannot insert NULL into ("DQISAPPS"."DIRECTORY_REMARKS"."DIRECTORY_ID")

at oracle.jdbc.driver.T4CTTIoer11.processError(T4CTTIoer11.java:498)

Here is my rest Controller :

@PostMapping("/")
public ResponseEntity<Response<DirectoryTableEntity>> save(@RequestBody DirectoryTableEntity directory){
    return new ResponseEntity<>(
            new Response<>(directoryService.save(directory), new Meta(SUCCESS_MESSAGE, HttpStatus.OK.value())),
            HttpStatus.OK);
    }

Here is my service :

    @Override
public DirectoryTableEntity save(DirectoryTableEntity directory) {
    // TODO Auto-generated method stub
    String user  = authService.getUsername();
    directory.setStatusId(1);
    directory.setCreatedBy(user);
    directory.setLastModifiedBy(user);
    return directoryTableRepository.save(directory);
}

Can anybody tell what I am missing here?


回答1:


Relationship between DirectoryRemarksEntity and DirectoryTableEntity is bi-directional. In JPA/Hibernate you have to explicitly set both sides.

When the DirectoryTableEntity is created from json, directory.getRemarks() will have DirectoryRemarksEntity but the remark.getDirectory() will be null.

So update your service method to include directory.getRemarks().forEach(remark -> remark.setDirectory(directory));

@Override
public DirectoryTableEntity save(DirectoryTableEntity directory) {
    // TODO Auto-generated method stub
    String user  = authService.getUsername();
    directory.setStatusId(1);
    directory.setCreatedBy(user);
    directory.setLastModifiedBy(user);
    directory.getRemarks().forEach(remark -> remark.setDirectory(directory));
    return directoryTableRepository.save(directory);
}


来源:https://stackoverflow.com/questions/62858120/unable-to-add-child-entity-jpa-onetomany-relationship

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!