I am using spring data rest, I have following entities exposed via spring data rest
DonationRequest
@Data
@Entity
@Table(name="donation_request",schema="public")
public class DonationRequest {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="donation_request_id")
Integer donationRequestId;
@Column(name="expiry_datetime")
Date expiryDatetime;
@Column(name="blood_group")
String bloodGroup;
@Column(name="no_of_bottles")
String noOfBottles;
@OneToOne
@JoinColumn(name="hospital_id")
Hospital hospital;
@OneToOne
@JoinColumn(name="user_data_id")
UserData requester;
@Column(name="active")
Boolean active;
}
Hospital
@Data
@Entity
@Table(name="hospital",schema="public")
public class Hospital {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="hospital_id")
Integer hospitalId;
@Column(name="name")
String name;
@Column(name="address")
String address;
@Column(name="loc",columnDefinition = "geometry")
Point loc;
}
Now I have an android client which has the same class definitions as stated above. Hospitals are cached at startup on android client. Now I want to create a donationRequest entity on server. I can do that easily by posting json of donationRequest object to /api/donationRequests. this json contains hospital object also. But the newly created donationRequest and hospital are not linked together.
Following type of json in postman does not create link:
{
"bloodGroup":"AB+",
"hospital":{
"hospitalId":1
}
}
I know that following json does create link:
{
"bloodGroup":"AB+",
"hospital":"/api/hospitals/1"
}
My question is how can I create link using first type of json as that is the natural way to serialize dontaionRequest object from android client? Also I want hospitals to be exposed via /api/hospitals, so removing that rest resource is not an option.