Answer the question
In order to leave comments, you need to log in
How to save a collection of objects using JPA?
Good day to all! My problem is this: JSON comes from the client:
{"name":"Строительство","subCategories":[{"name":"электроинструменты"},{"name":"грузовые машины"}]}
@Entity
@Table(name = "high_category")
public class HighCategory extends BaseModel {
@Column(name = "NAME")
@NotEmpty
private String name;
@JsonIgnore
@OneToMany(mappedBy = "highCategory",fetch = FetchType.LAZY,cascade = CascadeType.ALL)
private List<SubCategory> subCategories;
public HighCategory(){}
public HighCategory(HighCategoryDeserialize highCategoryDeserialize){
this.name=highCategoryDeserialize.getName();
this.subCategories=highCategoryDeserialize.getSubCategories();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<SubCategory> getSubCategories() {
return subCategories;
}
public void setSubCategories(List<SubCategory> subCategories) {
this.subCategories = subCategories;
}
@Entity
@Table(name = "middle_category")
public class SubCategory extends BaseModel {
@Column(name = "NAME")
private String name;
@ManyToOne(fetch = FetchType.LAZY, cascade=CascadeType.ALL)
@JoinColumn(name = "HIGH_CATEGORY_ID")
private HighCategory highCategory;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public HighCategory getHighCategory() {
return highCategory;
}
public void setHighCategory(HighCategory highCategory) {
this.highCategory = highCategory;
}
@RequestMapping(value = "/addMiddleCategory", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
public String addHighCategory(@RequestBody HighCategoryDeserialize highCategoryDeserialize, BindingResult bindingResult) {
String resultOfOperation;
List<SubCategory> subCategoryList=highCategoryDeserialize.getSubCategories();
HighCategory highcategory=new HighCategory(highCategoryDeserialize);
for(SubCategory subCategory:subCategoryList){
subCategory.setHighCategory(highcategory);
}
return subCategoryService.addSubCategory(subCategoryList);
}
@Override
public String addSubCategory(List<SubCategory> subCategories) {
String resultOfOperation;
for (SubCategory subCategory : subCategories) {
System.out.println(subCategory);
entityManager.persist(subCategory);
}
return resultOfOperation="Operation complete!";
}
List<SubCategory> subCategoryList=highCategoryDeserialize.getSubCategories();
Answer the question
In order to leave comments, you need to log in
In general, working solutions for the total 2. If someone can offer something more "worthy" I will be very grateful!
Our JSON from the client:
+ We also accept a Top Category ID with : @PathVariable(value = "highCategoryId")
Deserialize with a Wrapper class :
public class SubCategoryWrapper {
private List<SubCategory> subCategories;
/**
* @return the subCategories
*/
public List<SubCategory> getSubCategories() {
return subCategories;
}
/**
* @param subCategories the persons to set
*/
public void setSubCategories(List<SubCategory> subCategories) {
this.subCategories = subCategories;
}
}
@Entity
@Table(name = "sub_category")
public class SubCategory extends BaseModel {
@Column(name = "NAME")
private String name;
@ManyToOne(fetch = FetchType.LAZY, cascade=CascadeType.ALL)
@JoinColumn(name = "HIGH_CATEGORY_ID",referencedColumnName = "ID")
private HighCategory highCategory;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public HighCategory getHighCategory() {
return highCategory;
}
public void setHighCategory(HighCategory highCategory) {
this.highCategory = highCategory;
}
@Override
public String toString() {
return "Имя Подкатегории: "+getName();
}
}
HighCategory highCategory=entityManager.find(HighCategory.class,highCategoryId);
highCategory.setSubCategories(subCategories.getSubCategories());
public void setSubCategories(List<SubCategory> subCategories) {
this.subCategories = subCategories;
for(SubCategory subCategory: subCategories){
subCategory.setHighCategory(this);
}
for (SubCategory subCategory : subCategories.getSubCategories()) {
entityManager.createNativeQuery("INSERT INTO sub_category(NAME,HIGH_CATEGORY_ID) VALUES(?,?)")
.setParameter(1,subCategory.getName())
.setParameter(2,highCategoryId).executeUpdate();
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question