简体   繁体   中英

how to send only id instead of object in requestbody?

I have two entities. Customer which is mapped in one to many relation with the CustomerDepartment . CustomerDepartment table has a column to store customer Id .

I want to map them in such a way that Customer Object store a list of Customer Department, and the Customer Department stores the id of the customer it belongs to.

The code that is working compels me to send the all the customer details while creating or updating a customer Department.

Is there a way I can only send the id of the customer and it maps itself?

I have tried changing from -

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

to this -

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

which gives me the requestbody I want but it does not work giving the following error -

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

Working Code:

Customer:-

@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;

}

CustomerDepartment:-

@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;

}

Current RequestBody:-

{
  "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"
}

expected requestbody:-

{
  "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"
}

Have you considered a unidirectional @OneToMany : https://docs.jboss.org/hibernate/orm/5.2/userguide/html_single/Hibernate_User_Guide.html#associations ?

For example on CustomerDeparment change

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

}

to

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

...and on Customer change

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

}

to

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

}

As a bit of an aside, I honestly find Hibernate relationships to sometimes be more a hindrance than a help. As an alternative, you may wish to consider dropping the explicit relationship properties, using "regular" columns ( @Column(name="customer_no") private int customer' ) and just writing queries in your repo classes (ex. findByCustomerNo(int customNumber) ) to meet your requirements.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM