У меня есть следующие JSON:
{
"name": "String",
"time": int,
"serve": int,
"type": "String",
"about": "String",
"userId": int,
"food": [{
"main_id": long
"name_Id": int,
"size": int,
"measure": "String",
"foodImgId": int
},
{
"main_id": long
"name_Id": int,
"size": int,
"measure": "String",
"foodImgId": int
}, ... ],
"steps": [{
"main_id": long
"step_id": int,
"step": "String",
"stepImgId": int
},
{
"main_id": long
"step_id": int,
"step": "String",
"img": int
}, ... ],
"img": [{
"main_id": long
"foodImgId": int,
"stepImgId": int,
"imgLink": "String"
},
{
"main_id": long
"foodImgId": int,
"stepImgId": int,
"imgLink": "String"
}, ... ],
}
И следующие модели для этого JSON:
@Entity
@Table(name = "MAIN")
public class Main implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@Column(name = "NAME", nullable = false)
private String name;
@Column(name = "TIME", nullable = false)
private int time;
@Column(name = "SERVE", nullable = false)
private int serve;
@Column(name = "TYPE", nullable = false)
private String type;
@Column(name = "ABOUT", nullable = false)
private String about;
@Column(name = "USER_ID", nullable = false)
private int userId;
@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, mappedBy = "main_id", orphanRemoval = true)
private Set<Food> food;
@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, mappedBy = "main_id", orphanRemoval = true)
private Set<Steps> steps;
@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, mappedBy = "main_id", orphanRemoval = true)
private Set<Image> img;
// setter & getter
}
@Entity
@Table(name = "STEPS")
public class Steps implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@ManyToOne(fetch = FetchType.EAGER, cascade = {CascadeType.PERSIST, CascadeType.MERGE})
@JoinColumn(name = "MAIN_ID", updatable = false)
public Main main_id;
@Column(name = "STEP_ID", nullable = false)
public int step_id;
@Column(name = "STEP", nullable = false)
public String step;
@OneToOne(optional = false)
@JoinColumn(name = "stepImgId", nullable = true)
public Image stepImgId;
// setter & getter
}
@Entity
@Table(name = "IMAGE")
public class Image implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@ManyToOne(fetch = FetchType.EAGER, cascade = {CascadeType.MERGE, CascadeType.PERSIST})
@JoinColumn(name = "MAIN_ID", nullable = false)
private Main main_id;
@OneToOne(optional = false, mappedBy="foodImgId")
@Column(name = "foodImgId", nullable = true)
private Food food;
@OneToOne(optional = false, mappedBy="cookStepId")
@Column(name = "stepImgId", nullable = true)
private CookStep cookStepId;
@Column(name = "ImgLink", nullable = false)
private String imgLink;
// setter & getter
}
@Entity
@Table(name = "FOOD")
public class Food implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@ManyToOne(fetch = FetchType.EAGER, cascade = {CascadeType.PERSIST, CascadeType.MERGE})
@JoinColumn(name = "MAIN_ID", nullable = false)
private Main main_idain_;
@Column(name = "NAME_ID", nullable = false)
private int nameId;
@Column(name = "SIZE", nullable = false)
private int size;
@Column(name = "MEASURE", nullable = false)
private String measure;
@Column(name = "foodImgId", nullable = true)
private int foodImgId;
// setter & getter
}
Мой вопрос. Как я могу сохранить это JSON в БД? Main_id в каждой модели должен иметь идентификатор из класса Main. У меня есть пустые репозитории, потому что я пытался сохранить данные по умолчанию методом repo.save(My_JSON)
, но я не могу получить идентификатор из основного класса. Мне нужны какие-либо идеи, потому что у меня недостаточно опыта работы с пружинными ботинками.