级联不保存参考表数据

时间:2016-11-13 09:27:08

标签: spring spring-mvc spring-data entity

级联不保存详细信息

订单表

CREATE TABLE orders
(
  order_id numeric(10,0) NOT NULL,
  test_id numeric(5,0),
  CONSTRAINT orders_pkey PRIMARY KEY (order_id),
  CONSTRAINT fk_order_user_doneby FOREIGN KEY (done_by)
      REFERENCES user_mast (user_id) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION
}

参考表

CREATE TABLE refout_order_hdr
(
  ref_inst_id numeric(8,0),
  status character varying(1),
  ref_out_id numeric(10,0) NOT NULL,
  order_id numeric(10,0),
  CONSTRAINT "refout_order_pkey PRIMARY KEY (ref_out_id)" PRIMARY KEY (ref_out_id),
  CONSTRAINT "ref_out_fkey_order_id order_id)" FOREIGN KEY (order_id)
      REFERENCES orders (order_id) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION
)

订单和订单之间存在一对多关系。 Refrence(ORDER_ID)

参考的实体类

 /**
  * 
  */
 private static final long serialVersionUID = 1L;
 private long refOutId;
 private CdcInstitutes instMast;
 private String status;
 @JsonIgnore
 private Orders orders;


 //------other getters and setters are there -------------//

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name="order_id")
public Orders getOrders() {
    return orders;
}

public void setOrders(Orders orders) {
    this.orders = orders;

 //------other getters and setters are there -------------//

订单的实体类

private Long orderId;
private TestMast testMast;
private Set<RefoutOrder> refoutOrder; 


@OneToMany(mappedBy="orders", fetch=FetchType.LAZY, cascade = {CascadeType.MERGE, CascadeType.PERSIST } )
public Set<RefoutOrder> getRefoutOrder() {
    return refoutOrder;
}

public void setRefoutOrder(Set<RefoutOrder> refoutOrder) {
    this.refoutOrder = refoutOrder;
}

即使在保存订单实体时设置了级联,也不会保存引用表orderId。其他字段已设置。

//服务层

for( OrderedListDto orderedListDto : radioOrderList){
            if(orderedListDto != null && orderedListDto.getOrdersDto() != null){
                orderedListDto.getOrdersDto().setTestType(ServiceConstants.TEST_TYPE_RADIALOGY);
                orderedListDto.getOrdersDto().setVisitId(visitDto.getVisitId());
                neworderDtos.add(orderedListDto.getOrdersDto());
            }
        }
        List<OrdersDto> resultList = ordersService.saveOrdersList(new ArrayList<>(neworderDtos));

//将从此处调用Save方法

@Override
    @Transactional
    public List<OrdersDto> saveOrdersList(List<OrdersDto> ordersList) throws VisitMastException {
        if(ordersList == null){
            //need to throws exception as atleast one order to update or save order.
        }
         List<OrdersDto> resultList = new ArrayList<OrdersDto>();
        for(OrdersDto order: ordersList){
            try{
                order.setInstCode(InstitutionService.getInstCode());
                order.setCreatedDate(new Date());                           order = super.saveOneEntity(order);
                resultList.add(order);

            } catch (GenericException ge) {
                throw new VisitMastException(ge, ge.getRootCauseMessage());
            } catch (InstituteNotFoundException e) {
                throw new VisitMastException(VisitMastException.INTERNAL_SERVER_ERROR, "", e, 
                        messageSource.getMessage(CodeEnum.INST_CODE_NOT_FOUND.getValue(), null, Locale.ENGLISH));
            }

        }
        return resultList;
    }

保存逻辑将一次保存一个实体

0 个答案:

没有答案