我正在尝试在两个实体之间实现一对多Map。在这里,名称有一个位置,而位置有多个名称。一旦我添加了名称,它就Map得很好,但它会更新位置表,除了id之外为空。
这里我使用的是Microsoft SQL服务器。应用程序属性
#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
主标识
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;
}
位置管理器
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
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;
}
}
名称的控制器类别
@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);
}
我将数据传递到后端,如下所示
{
"id": "DM1003",
"designationName": "Driver",
"location": {
"locationId": "LM1001"
}
}
它更新了数据库,但将location表中的其他值更改为null,并获得如下请求
{
"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
}
与数据库中相同-位置表x1c 0d1x
- 名称表
我尝试了很多方法,但没有一种对我有效。有没有人能让我的一天过得更好?
1条答案
按热度按时间0yg35tkg1#
修改服务类中的createDesignation(DesignationMaster desData)后,其工作如下
在这里,我从它的存储库中获取位置数据,并将该对象设置为目标对象,这样它就可以正常工作。