[英]How to save multiple entity in one action related in Many to Many Relationship [Spring Boot 2, JPA, Hibernate, PostgreSQL]
[英]One to Many relationship, how to save or update data in spring boot
我正在為我工作的公司構建一個 API,這是我第一次使用 Spring Boot,所以我遇到了一些問題。
我有 2 個班級,Compania(父母)和辦公室(孩子)。 class Compania 與 Office 具有 OneToMany 關系,而 Office 具有 ManyToOne,我正在努力尋找如何添加一些孩子或編輯其中一個。 現在我的課程如下:
公司(父)
@Entity(name = "Compania")
@Table(
name = "compania"
)
public class Compania {
@Id
@SequenceGenerator(
name = "compania_sequence",
sequenceName = "compania_sequence",
allocationSize = 1
)
@GeneratedValue(
strategy = GenerationType.SEQUENCE,
generator = "compania_sequence"
)
@Column(
nullable = false
)
private Long id;
@Column(
name = "name",
nullable = false,
unique = true
)
private String name;
@Column(
name = "dominio",
nullable = true
)
private String dominio;
@Column(
name = "altas"
)
private String altas;
@Column(
name = "bajas"
)
private String bajas;
@OneToMany(
mappedBy = "compania",
cascade = CascadeType.ALL,
fetch = FetchType.LAZY,
orphanRemoval = true
)
private List<Office> office;
...Constructors...
... Getters and Setters...
辦公室(兒童)
@Entity()
@Table()
public class Office {
@Id
@SequenceGenerator(
name = "office_sequence",
sequenceName = "office_sequence",
allocationSize = 1
)
@GeneratedValue(
strategy = GenerationType.SEQUENCE,
generator = "office_sequence"
)
@Column(
nullable = false
)
@JsonIgnore
private Long id;
@Column(
name = "idRef",
nullable = false
)
private int idRef;
@Column(
name = "title",
nullable = false
)
private String title;
@Column(
name = "name"
)
private String name;
@Column(
name = "path"
)
private String path;
@ManyToOne(
fetch = FetchType.LAZY
)
@JoinColumn(
name = "compania_id",
referencedColumnName = "id"
)
private Compania compania;
...Constructors...
... Getters and Setters...
我想在我的父 object(具有 OneToMany 關系的那個)中保存或編輯數據為了實現這一點,我想到了 2 種方法,但我都遇到了問題。
第一個是將所有內容保存在 Compania (父)class 中。 像這樣,我將 Compania class 和 JSON object 的 ID 作為辦公室 ZA8CFDE6331194EB26ZC96F8
public void addOfficeTest(Long companiaId, Office office) {
// OPTION 1 - Add directly the office to the office list of the Compania Object (parent), then save it using Compania repository
if (office.getId() != null) {
office.setId(null);
}
Compania companiaFound = companiaRepository.findById(companiaId).get();
List<Office> listaOffice = companiaFound.getOffice();
listaOffice.add(office);
companiaFound.setOffice(listaOffice);
// method to set references id for user use...
setIdReferencia(companiaEnc, true, false);
companiaRepository.save(companiaEnc);
使用這種方法,我的問題是我無法保存我的實體,當我通過 GET 獲取我的 Companias 時,我將 Office 作為一個空字符串。 像這樣:
{
"id": 1,
"name": "Test Compania",
"dominio": "domain",
"altas": "OU=nn",
"bajas": "none",
"office": []
}
我認為的另一種方法是使用 Office 存儲庫保存 Office,但是當我嘗試獲取我的 Compania 時返回了無限遞歸錯誤(因為我正在將公司保存在辦公室中)......
public void addOfficeTest(Long companiaId, Office office) {
// OPTION 2 - Save Office with the repository office
if (office.getId() != null) {
office.setId(null);
}
Compania companiaEnc = companiaRepository.findById(companiaId).get();
office.setCompania(companiaEnc); // This causes recursion
officeRepository.save(office);
所以我被困在這里,因為我不知道如何為我的子對象做 PUT 或 POST。 我不知道我的代碼是否有問題或什么。 也許它有一個簡單的解決方案,但我真的找不到。 如果有人可以幫助給我一個如何進行 POST 和 PUT 的示例,我將非常感激......
謝謝
鑒於您具有雙向關系,我認為您缺少在新Office
中設置Compania
object 。 大致如下:
public void addOfficeTest(Long companiaId, Office office) {
// OPTION 1 - Add directly the office to the office list of the Compania Object (parent), then save it using Compania repository
if (office.getId() != null) {
office.setId(null);
}
Compania companiaFound = companiaRepository.findById(companiaId).get();
office.setCompania(companiaFound); // The new line
List<Office> listaOffice = companiaFound.getOffice();
listaOffice.add(office);
companiaFound.setOffice(listaOffice); // You actually don't need this because you've changed the List already
// method to set references id for user use...
setIdReferencia(companiaEnc, true, false);
companiaRepository.save(companiaEnc);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.