我正在為我工??作的公司構建 API,這是我第一次使用 Spring Boot,所以我遇到了一些問題。
我有 2 個班級,Compania(家長)和 Office(兒童)。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...
我想在我的父物件(具有 OneToMany 關系的物件)中保存或編輯資料為了實作這一點,我想到了 2 種方法,但我對這兩種方法都有問題。
第一個將保存 Compania (Parent) 類中的所有內容。像這樣,我將 Compania 類的 id 和 JSON 物件作為 Office 物件傳遞:
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);
With this method, my problem is that is like I'm not being able to save my entities, when I make a GET to obtain my Companias I get the Office as an empty string. like this:
{
"id": 1,
"name": "Test Compania",
"dominio": "domain",
"altas": "OU=nn",
"bajas": "none",
"office": []
}
The other method I thought was this one, saving the Office with the Office repository, but that returned an infinite recursion error when I tried to fetch my Compania (as I'm saving the compania in office)...
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);
So I'm stuck here because I don't know how to do a PUT or a POST for my child objects. I don't know if something is wrong with my code or what. Maybe it has an easy solution, but I really can't find it. If someone could help giving me an example of how to do a POST and a PUT I would be very grateful...
Thanks
uj5u.com熱心網友回復:
鑒于您有雙向關系,我認為您缺少Compania在新的Office. 沿著以下幾行的內容:
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);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/359614.html
標籤:java json spring-boot hibernate jpa
上一篇:SQL或JPQL查詢以獲取單向關系One-To-Many
下一篇:嘗試決議JSON
