簡體   English   中英

如何只發送id而不是requestbody中的對象?

[英]how to send only id instead of object in requestbody?

我有兩個實體。 與CustomerDepartment有一對多關系的Customer CustomerDepartment表具有用於存儲客戶ID的列。

我想以客戶對象存儲客戶部門列表的方式映射它們,而客戶部門存儲其所屬客戶的ID。

正在運行的代碼迫使我在創建或更新客戶部門時發送所有客戶詳細信息。

有沒有辦法我只能發送客戶ID並映射自己?

我嘗試從-

@JsonBackReference
@ManyToOne
@JoinColumn(name = "customer_no", nullable = false)
private Customer customer;

為此-

@JsonBackReference
@ManyToOne(targetEntity = Customer.class)
@JoinColumn(name = "customer_no", nullable = false)
private Integer customer;

這給了我我想要的請求主體,但由於出現以下錯誤而無法正常工作-

2019-08-03 04:59:08 ERROR CustomerController:72 - org.springframework.orm.jpa.JpaSystemException: Error accessing field [private java.lang.Integer com.enquero.pulse.entity.Customer.customerNo] by reflection for persistent property [com.enquero.pulse.entity.Customer#customerNo] : 1; nested exception is org.hibernate.property.access.spi.PropertyAccessException: Error accessing field [private java.lang.Integer com.enquero.pulse.entity.Customer.customerNo] by reflection for persistent property [com.enquero.pulse.entity.Customer#customerNo] : 1

工作代碼:

顧客:-

@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@DynamicUpdate
@Entity
@Table(name = "customer")
public class Customer extends Auditable<Integer>{

@Id
@Column(name = "customer_no")
private Integer customerNo;

@NotBlank
@Column(name = "customer_name")
private String customerName;

@Column(name = "industry")
private String industry;

@Column(name = "country")
private String country;

@Column(name = "state")
private String state;

@Column(name = "city")
private String city;

@Column(name = "postal_code")
private String postalCode;

@Column(name = "address_line1")
private String addressLine1;

@Column(name = "address_line2")
private String addressLine2;

@Column(name = "address_line3")
private String addressLine3;

@Column(name = "payment_term")
private String paymentTerm;

@Column(name = "customer_segment")
private String customerSegment;

@JsonFormat(pattern="dd-MMM-yyyy")
@Column(name = "engagement_start_on")
private Date engagementStartOn;

@JsonManagedReference
@OneToMany(fetch = FetchType.LAZY, mappedBy = "customer")
private List<CustomerDepartment> customerDepartments;

}

客戶部:

@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@DynamicUpdate
@Entity
@Table(name = "customer_department")
public class CustomerDepartment extends Auditable<Integer>{

@Id
@Column(name = "dept_id", updatable = false, nullable = false)
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer deptId;

@Column(name = "dept_name")
private String deptName;

@Column(name = "primary_contact")
private String primaryContact;

@JsonBackReference
@ManyToOne
@JoinColumn(name = "customer_no", nullable = false)
private Customer customer;

}

當前的請求正文:

{
  "createdBy": 0,
  "creationDate": "2019-08-02T23:05:33.993Z",
  "customer": {
    "addressLine1": "string",
    "addressLine2": "string",
    "addressLine3": "string",
    "city": "string",
    "country": "string",
    "createdBy": 0,
    "creationDate": "2019-08-02T23:05:33.993Z",
    "customerDepartments": [
      null
    ],
    "customerName": "string",
    "customerNo": 0,
    "customerSegment": "string",
    "engagementStartOn": "string",
    "industry": "string",
    "lastUpdateDate": "2019-08-02T23:05:33.993Z",
    "lastUpdatedBy": 0,
    "paymentTerm": "string",
    "postalCode": "string",
    "state": "string"
  },
  "deptId": 0,
  "deptName": "string",
  "lastUpdateDate": "2019-08-02T23:05:33.994Z",
  "lastUpdatedBy": 0,
  "primaryContact": "string"
}

預期的請求人:-

{
  "createdBy": 0,
  "creationDate": "2019-08-02T23:05:33.993Z",
  "customer": 1, //id instead of json
  "deptId": 0,
  "deptName": "string",
  "lastUpdateDate": "2019-08-02T23:05:33.994Z",
  "lastUpdatedBy": 0,
  "primaryContact": "string"
}

您是否考慮過單向@OneToManyhttps : @OneToMany

例如關於CustomerDeparment更改

@JsonBackReference
@ManyToOne
@JoinColumn(name = "customer_no", nullable = false)
private Customer customer;

}

  @JsonBackReference
    @ManyToOne
    @Column(name = "customer_no")
    private int customer;

...以及客戶變更

@JsonManagedReference
@OneToMany(fetch = FetchType.LAZY, mappedBy = "customer")
private List<CustomerDepartment> customerDepartments;

}

 @JsonManagedReference
@OneToMany(cascade = CascadeType.ALL)
private List<CustomerDepartment> customerDepartments;

}

順便說一句,老實說,我發現休眠關系有時更是一種障礙而不是幫助。 或者,您可能希望考慮使用“常規”列( @Column(name="customer_no") private int customer' )並僅在您的repo類中編寫查詢(例如, findByCustomerNo(int customNumber) )可以滿足您的要求。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM