While saving json object containing multiple jsons, the json object is being saved as a single array instead of multiple rows.
sample json:
[
{
"id" : 1, -- this is not a primary key and not unique but cannot be null
"name" : "John Doe",
"phone" : [
{ "type" : "home", "ref" : "111-111-1234"},
{ "type" : "work", "ref" : "222-222-2222"}
]
},
{
"id" : 2, -- this is not a primary key and not unique but cannot be null
"name" : "Jane Doe",
"phone" : [
{ "type" : "home", "ref" : "111-111-1234"},
{ "type" : "work", "ref" : "222-222-2222"}
]
}
]
This is what i need after saving in the database
id name phone
1 John Doe { "type" : "home", "ref" : "111-111-1234"}
1 John Doe { "type" : "work", "ref" : "222-222-2222"}
2 Jane Doe { "type" : "home", "ref" : "111-111-1234"}
2 Jane Doe { "type" : "work", "ref" : "222-222-2222"}
This is what I am getting
id name phone
1 John Doe [{ "type" : "home", "ref" : "111-111-1234"},{ "type" : "work", "ref" : "222-222-2222"}]
2 Jane Doe [{ "type" : "home", "ref" : "111-111-1234"},{ "type" : "work", "ref" : "222-222-2222"}]
here is how i am parsing the json object to pojo and saving to db
@Entity
@Table(name="person")
public class person{
private Integer id;
private String name;
private String phone;
@Transient
JsonNode phoneJson;
private static OhjectMapper mapper = new ObjectMapper();
getter/setter
@Transient
public JsonNode getPhoneJson(){
return phoneJson;
}
public void setPhoneJson(JsonNode phoneJson){
this.phoneJson = phoneJson;
}
@JsonIgnore
@Column(name="phone")
public String getPhone() throws Exception{
return mapper.writeValueAsString(phoneJson);
}
public void setPhone(String phone) throws Exception{
this.phone = mapper.readTree(phone);
}
}
dao- save
personRepository.save(person)
any help would be appreciated.
UPDATE
Multiple jSON Column
[
{
"id" : 1, -- this primary key and not unique but cannot be null
"name" : { --this element can be empty/null
"first" : "John",
"last" : "Doe"
},
"phone" : [
{ "type" : "home", "ref" : 1111111234},
{ "type" : "work", "ref" : 2222222222}
]
},
{
"id" : 2, -- this primary key and not unique but cannot be null
"name" : {
"first" : "Jane",
"last" : "Doe"
},
"phone" : [
{ "type" : "home", "ref" : 1111111234},
{ "type" : "work", "ref" : 2222222222}
]
}
]
how do i get result as below
id name phone
1 [{John},{Doe}] { "type" : "home", "ref" : "111-111-1234"}
1 [{John},{Doe}] { "type" : "work", "ref" : "222-222-2222"}
2 [{Jane},{Doe}] { "type" : "home", "ref" : "111-111-1234"}
2 [{Jane},{Doe}] { "type" : "work", "ref" : "222-222-2222"}