一对多关系如何在spring boot中保存数据

Posted

技术标签:

【中文标题】一对多关系如何在spring boot中保存数据【英文标题】:one to many relationship how to save data in spring boot 【发布时间】:2020-09-02 08:29:22 【问题描述】:

下面是我的商店实体

@Entity
@Table(name="stores")
public class Stores 
    @Id
    @GeneratedValue
    private Long id;

    @Column(name ="incharge_id")
    private Integer inchargeId;

    @Column(name = "store_name")
    private String storeName;

    @OneToMany(mappedBy = "stores",
            fetch = FetchType.LAZY,
            cascade = CascadeType.ALL)
    private Set<Items> items;

    public Set<Items> getItems() 
        return items;
    

    public void setItems(Set<Items> items) 
        this.items = items;
        for (Items item : items) 
            item.setStores(this);
        
    

    public Long getId() 
        return id;
    

    public void setId(Long id) 
        this.id = id;
    

    public Integer getInchargeId() 
        return inchargeId;
    

    public void setInchargeId(Integer inchargeId) 
        this.inchargeId = inchargeId;
    

    public String getStoreName() 
        return storeName;
    

    public void setStoreName(String storeName) 
        this.storeName = storeName;
    

下面是我的物品实体

package bt.gov.dit.inventoryservice.model;

import javax.persistence.*;
import java.util.Date;

@Entity
@Table(name = "items")
public class Items 

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long Id;

    @Column(name="item_name")
    private String itemName;

    @ManyToOne
    private Categories categories;

    @ManyToOne(fetch = FetchType.LAZY)
    //@JoinColumn(name = "book_category_id", referencedColumnName = "id")
    @JoinColumn(name = "stores_id", nullable = false,referencedColumnName = "id")
    private Stores stores;

    @Column(name="insert_date")
    private Date insertDate;

    @Column(name="update_date")
    private Date updateDate;

    public Long getId() 
        return Id;
    

    public void setId(Long id) 
        Id = id;
    

    public String getItemName() 
        return itemName;
    

    public void setItemName(String itemName) 
        this.itemName = itemName;
    

    public Categories getCategories() 
        return categories;
    

    public void setCategories(Categories categories) 
        this.categories = categories;
    

    public Stores getStores() 
        return stores;
    

    public void setStores(Stores stores) 
        this.stores = stores;
        stores.getItems().add(this);
    

    public Date getInsertDate() 
        return insertDate;
    

    public void setInsertDate(Date insertDate) 
        this.insertDate = insertDate;
    

    public Date getUpdateDate() 
        return updateDate;
    

    public void setUpdateDate(Date updateDate) 
        this.updateDate = updateDate;
    


他们之间是一对多的关系。一家商店可以有许多商品。但我不知道如何使用 stores 插入项目。我已经尝试过 Jpa respoistory 的默认保存,但是代替 stores_id (这是外键)它保存 null 。谁能告诉我如何实现该服务?

【问题讨论】:

grokonez.com/spring-framework/spring-boot/… 按照这个链接,它用代码显示了我想要的。 【参考方案1】:

如下所示。

Stores stores = new Stores();
stores.setStoreName("store name");
// Set other fields of store entity

Item item1 = new Item();
item1.setItemName("item name 1");
// Set other fields of item entity

Item item2 = new Item();
item2.setItemName("item name 2");
// Set other fields of item entity

// Call setItems

// Call getItems in a Set class object like Set<Item> items;

stores.setItems(items);
storesService.save(stores); // it will save all items with foreign key.

【讨论】:

以上是关于一对多关系如何在spring boot中保存数据的主要内容,如果未能解决你的问题,请参考以下文章

无法让spring boot hibernate rest api返回一对多关系

外键在一对多关系中始终为空 - Spring Boot Data with JPA

尝试使用 mysql 学习带有外键和一对多关系关系的 Spring Boot 但是

使用spring boot和spring data jpa时一对多关系的奇怪行为

Spring Boot项目中的MongoDB一对多和多对一关系

如何使用 laravel 8 同时保存一对多关系数据