I am trying to implement ONE-to-MANY mapping among two entities. In here designations have a location and a location have many designations. Once I added the designation it mapped fine, but it update the location table with null except id.
Here I’m using the Microsoft SQL server. Application Properties
#for ms server spring.datasource.url = jdbc:sqlserver://localhost\sqlexpress;databaseName=nifs_database;encrypt=true;trustServerCertificate=true spring.datasource.username = viranga spring.datasource.password = ******** spring.jpa.hibernate.ddl-auto=update spring.jpa.properties.hibernate.format_sql = true spring.jpa.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect spring.jpa.properties.hibernate.globally_quoted_identifiers=true #spring.main.allow-circular-references=true spring.jackson.serialization.fail-on-empty-beans=false
DesignationMaster
package com.nifs.backend.Admin.EmployeeDesignation; import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.nifs.backend.Admin.Locations.Locations; import jakarta.persistence.*; import lombok.*; import java.util.Date; @Entity @Getter @Setter @AllArgsConstructor @NoArgsConstructor @Table(name = "designation_master") public class DesignationMaster { @Id @Column(name = "id", nullable = false, length = 10) private String id; @Column(name = "designation_name", length = 255) private String designationName; @ManyToOne(fetch = FetchType.LAZY, cascade = {CascadeType.MERGE}) @JoinColumn(name = "location_id", referencedColumnName = "location_id", nullable = false) @JsonIgnoreProperties("designations") private Locations location; @Temporal(TemporalType.TIMESTAMP) @Column(name="date_created") private Date dateCreated; @Temporal(TemporalType.TIMESTAMP) @Column(name="date_updated") private Date dateUpdated; }
LocationMaster
package com.nifs.backend.Admin.Locations; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.nifs.backend.Admin.EmployeeDesignation.DesignationMaster; import com.nifs.backend.Admin.OtherData.District; import jakarta.persistence.*; import lombok.AllArgsConstructor; import lombok.Getter; import lombok.NoArgsConstructor; import lombok.Setter; import java.util.Date; import java.util.List; import java.util.Set; @Entity @Setter @Getter @NoArgsConstructor @AllArgsConstructor @Table(name = "locations_master") public class Locations { @Id @Column(name = "location_id", nullable = false, length = 10) private String locationId; @Column(name = "location_name", length = 100) private String locationName; @Column(name = "address", length = 255) private String address; @Column(name = "tel_no", length = 15) private String telNo; @Column(name = "fax_no", length = 15) private String faxNo; @Temporal(TemporalType.TIMESTAMP) @Column(name="date_created") private Date dateCreated; @Temporal(TemporalType.TIMESTAMP) @Column(name="date_updated") private Date dateUpdated; // relationships @OneToMany(mappedBy = "location", cascade = {CascadeType.MERGE}, fetch = FetchType.LAZY) @JsonIgnoreProperties("location") private List<DesignationMaster> designations; }
Service class of Designations
@Service public class DesignationService { @Autowired private DesignationRepostory desRepo; @Autowired private LocationRepository locRepo; // get all designations public List<DesignationMaster> getAllDesignations() { return desRepo.findAll(); } public Boolean createDesignation(DesignationMaster desData) { if(desRepo.returnDesignation(desData.getId()) == null){ Date d = new Date(); desData.setDateCreated(d); desRepo.save(desData); return true; } return false; } }
Controller class of Designation
@RestController @RequestMapping("admin/designation") public class DesignationController { @Autowired private DesignationService desService; // get all designations @GetMapping private List<DesignationMaster> getAllDesignations(){ return desService.getAllDesignations(); } @GetMapping("/newid") private String returnNewId(){ return desService.returnNewId(); } // create designation @PostMapping private Boolean createDesignation(@RequestBody DesignationMaster desData){ return desService.createDesignation(desData); }
I pass data to backend as this
{ "id": "DM1003", "designationName": "Driver", "location": { "locationId": "LM1001" } }
And it updated the database, but it change others values to null in location table, and get request like this
{ "id": "DM1001", "designationName": "Lecturer", "location": { "locationId": "LM1001", "locationName": null, "address": null, "telNo": null, "faxNo": null, "dateCreated": null, "dateUpdated": null, "hibernateLazyInitializer": {} }, "dateCreated": "2022-12-21T13:56:32.429+00:00", "dateUpdated": null }
As in the database -Location table
I went through many methods but nothing works for me. Is there anyone who can make my day better?
Advertisement
Answer
Its work after change the createDesignation(DesignationMaster desData) in service class as follows
public Boolean createDivision(DivisionMaster diviMasterData) { if (diviMasterRepo.returnDivision(diviMasterData.getDivisionId()) == null) { Date d = new Date(); diviMasterData.setCreatedDate(d); Locations l = locRepo.getLocation(diviMasterData.getLocation().getLocationId()); diviMasterData.setLocation(l); diviMasterRepo.save(diviMasterData); return true; } else { return false; } }
where I fetch the location data from its repository and set that object to the destination object. So that its work fine.