微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Spring Boot:将子实体保存到父实体

如何解决Spring Boot:将子实体保存到父实体

我正在开发一个Spring Boot Web App,该应用程序可跟踪公司进行的硬件交易。用户能够为交易创建“新记录”。在创建newRecord的页面上,他们可以在记录中添加“硬件项”。 hwItem是父实体newRecord的子实体。这是两个仅显示相关方法的实体:

@Entity
@Table(name="Records")
public class NewRecord extends Auditable<String>{

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name="recordId")
    private Long recordId;
    @Temporal(TemporalType.TIMESTAMP)
    @DateTimeFormat(pattern="yyyy/MM/dd hh:mm:ss")
    @Column(name="dateCreated")
    private Date dateCreated;
    @CreatedBy
    @Size(max=30)
    @Column(name="createdBy")
    private String createdBy; //Auditable.java and other Auditable data all required for "createdBy"

    @Size(min=0,max=50,message="{newRecord.rfqCustomerNumber.size}")
    .
    .
    .

    @OnetoMany(mappedBy="record",cascade = {CascadeType.ALL})
    private Set<HardwareItems> items;

    public void addtochildren(HardwareItems item) {
        item.setParent(this);
        this.items.add(item);
    }

    public Set<HardwareItems> getItems() {
        return items;
    }

    public void setItems(Set<HardwareItems> items) {
        this.items = items;
    }
...
}

@Entity
@Table(name="HardwareItems")
public class HardwareItems {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name="id")
    private long id;
    
    @ManyToOne(fetch = FetchType.LAZY,cascade = CascadeType.ALL)
    @JoinColumn(name="record_id",nullable=false) 
    private NewRecord record;
    
    @Size(min=0,max=100,message="{newRecord.hwDescription.size}")
    @Column(name="hwDescription")
    private String hwDescription;
    @Size(min=0,message="{newRecord.hwSerialNumber.size}")
    @Column(name="hwSerialNumber")
    private String hwSerialNumber;
    @Size(min=0,message="{newRecord.hwModelNumber.size}")
    @Column(name="hwModelNumber")
    private String hwModelNumber;
    
    public void setParent(NewRecord newRecord) {
        // Todo Auto-generated method stub
        
    }
    .
    .
    .
}

除了在HardwareItems实体中的setParent()之外,我相信这里的一切都是我想要的设置,

接下来是我不确定该从哪里继续或者我是否正确开始的部分。首先是HardareItemsService:

package bcoreHW.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import bcoreHW.model.HardwareItems;
import bcoreHW.model.HardwareItemsRepo;

@Service
public class HardwareItemsService {
    
    @Autowired
    private HardwareItemsRepo hwItemsRepo;
    
    
    public void save(HardwareItems hwItem) {
        hwItemsRepo.save(hwItem);
    }
    

    public void delete(Long recordId) {
        
        hwItemsRepo.deleteById(recordId);;
        
    }
    
}

然后我有点迷路的地方是NewRecordController。相关方法在这里

    @RequestMapping(value="/hwItem",method=RequestMethod.POST)
    ModelAndView hwItem(ModelAndView modelAndView,@PathVariable Long recordId,@modelattribute(value="hwItem")@Valid @RequestBody HardwareItems hwItem,BindingResult result) {
        
        //modelAndView.setViewName("app.newRecord");
        
        if(!result.hasErrors()) {
            hwItemsService.save(hwItem);
            modelAndView.getModel().put("hwItem",new HardwareItems());
            modelAndView.setViewName("redirect:/newRecord");
        }
        
        return modelAndView;
    }
    
    @RequestMapping(value="/newRecord",method=RequestMethod.POST)
    ModelAndView newRecord(ModelAndView modelAndView,@modelattribute(value="newRecord")@Valid NewRecord newRecord,BindingResult result) {
        
        modelAndView.setViewName("app.newRecord");
        
        if(!result.hasErrors()) {
            
            newRecord.addtochildren(item);
            
            
            newRecordService.save(newRecord);
            modelAndView.getModel().put("newRecord",new NewRecord());
            modelAndView.setViewName("redirect:/home");
        }
        
        return modelAndView;
    }

因此很明显,我试图保存hwItem,然后将其传递并保存到newRecord,但是我不确定自己是否正确执行了操作。任何见识都会有所帮助。谢谢!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。