无法在Springboot / Hibernate中使用OneToMany关系保存子对象

时间:2017-09-01 06:18:22

标签: java spring hibernate jpa spring-boot

我正在使用Spring BootHibernate annotationsRepositories中构建订单跟踪系统。我有一个Order类,可以有一个OrderItems列表。它们分别映射到ORDERORDER_ITEMS表。我代表两者的代码如下。

Order.java

package net.township.order;


import org.hibernate.annotations.Cascade;

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


@Entity
@Table(name = "orders")
public class Order {
    public Order() {
}

public Order(long merchantId, String firstDeliveryName, String 
lastDeliveryName, String deliveryAddress, String status, Date createDate, 
Date updateDate) {
    this.merchantId = merchantId;
    this.lastDeliveryName = lastDeliveryName;
    this.firstDeliveryName = firstDeliveryName;
    this.deliveryAddress = deliveryAddress;
    this.status = status;
    this.createDate = createDate;
    this.updateDate = updateDate;
}

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "order_id", unique = true)
private long orderId;

@Column(name = "merchant_id")
private long merchantId;

@Column(name = "first_delivery_name")
private String firstDeliveryName;

@Column(name = "last_delivery_name")
private String lastDeliveryName;

@Column(name = "delivery_address")
private String deliveryAddress;

@Column
private String status;


@OneToMany(mappedBy = "order", cascade = { 
CascadeType.ALL,CascadeType.PERSIST,CascadeType.MERGE })
private List<OrderItem> orderItems;

@Column(name = "create_date")
private Date createDate;


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


public void setOrderId(long orderId) {
    this.orderId = orderId;
}

public long getMerchantId() {
    return merchantId;
}

public void setMerchantId(long merchantId) {
    this.merchantId = merchantId;
}

public List<OrderItem> getOrderItems() {
    return orderItems;
}

public void setOrderItems(List<OrderItem> orderItems) {
    this.orderItems = orderItems;
}


public String getLastDeliveryName() {
    return lastDeliveryName;
}

public void setLastDeliveryName(String lastDeliveryName) {
    this.lastDeliveryName = lastDeliveryName;
}

public Date getUpdateDate() {
    return updateDate;
}

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

public Long getOrderId() {
    return orderId;
}

public void setOrderId(Long orderId) {
    this.orderId = orderId;
}

public String getFirstDeliveryName() {
    return firstDeliveryName;
}

public void setFirstDeliveryName(String firstDeliveryName) {
    this.firstDeliveryName = firstDeliveryName;
}

public String getDeliveryAddress() {
    return deliveryAddress;
}

public void setDeliveryAddress(String deliveryAddress) {
    this.deliveryAddress = deliveryAddress;
}

public String getStatus() {
    return status;
}

public void setStatus(String status) {
    this.status = status;
}

public Date getCreateDate() {
    return createDate;
}

public void setCreateDate(Date createDate) {
    this.createDate = createDate;
}
}

OrderItem.java

package net.township.order;

import com.fasterxml.jackson.annotation.JsonBackReference;
import org.hibernate.annotations.Cascade;

import javax.persistence.*;


@Entity
@Table(name = "order_items")
public class OrderItem {

@Id
@GeneratedValue
@Column(name = "id")
private Long id;

public Long getId() {
    return id;
}

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

public Order getOrder() {
    return order;
}

public void setOrder(Order order) {
    this.order = order;
}

@Column
private String name;

@Column
private String description;

@Column
private Long quantity;

@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn (name="ORDER_ID")
@JsonBackReference
@Cascade(value={org.hibernate.annotations.CascadeType.ALL})
private Order order;

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getDescription() {
    return description;
}

public void setDescription(String description) {
    this.description = description;
}

public Long getQuantity() {
    return quantity;
}

public void setQuantity(Long quantity) {
    this.quantity = quantity;
}
}

当我从前端POSTOrder时,它会正确映射到Order对象。 OrderItems中提供的所有JSON也作为List出现在对象中。但是,在使用我的OrderRepository的保存方法(它只是CrudRepository)将其保存到数据库之后,我的数据库包含一个具有正确字段的新Order对象,但没有任何内容曾在ORDER_ITEMS创建。

我在Hibernate和JPA OneToMany注释的文档中搜索过,我不知道我在哪里出错了。我还要补充一点,我没有创建手动模式,让SpringBoot处理H2中的所有内容。

2 个答案:

答案 0 :(得分:0)

这最终对我有用。

Order.java

package net.township.order;


import org.hibernate.annotations.Cascade;

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


    @Entity
    @Table(name = "orders")
    public class Order {
    public Order() {
    }

    public Order(long merchantId, String firstDeliveryName, String lastDeliveryName, String deliveryAddress, String status, Date createDate, Date updateDate) {
        this.merchantId = merchantId;
        this.lastDeliveryName = lastDeliveryName;
        this.firstDeliveryName = firstDeliveryName;
        this.deliveryAddress = deliveryAddress;
        this.status = status;
        this.createDate = createDate;
        this.updateDate = updateDate;
    }

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "order_id", unique = true)
    private long orderId;

    @Column(name = "merchant_id")
    private long merchantId;

    @Column(name = "first_delivery_name")
    private String firstDeliveryName;

    @Column(name = "last_delivery_name")
    private String lastDeliveryName;

    @Column(name = "delivery_address")
    private String deliveryAddress;

    @Column
    private String status;


    @OneToMany( cascade = CascadeType.ALL)
    @JoinColumn(name = "order_id", referencedColumnName = "order_id")
    private List<OrderItem> orderItems;

    @Column(name = "create_date")
    private Date createDate;


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


    public void setOrderId(long orderId) {
        this.orderId = orderId;
    }

    public long getMerchantId() {
        return merchantId;
    }

    public void setMerchantId(long merchantId) {
        this.merchantId = merchantId;
    }

    public List<OrderItem> getOrderItems() {
        return orderItems;
    }

    public void setOrderItems(List<OrderItem> orderItems) {
        this.orderItems = orderItems;
    }


    public String getLastDeliveryName() {
        return lastDeliveryName;
    }

    public void setLastDeliveryName(String lastDeliveryName) {
        this.lastDeliveryName = lastDeliveryName;
    }

    public Date getUpdateDate() {
        return updateDate;
    }

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

    public Long getOrderId() {
        return orderId;
    }

    public void setOrderId(Long orderId) {
        this.orderId = orderId;
    }

    public String getFirstDeliveryName() {
        return firstDeliveryName;
    }

    public void setFirstDeliveryName(String firstDeliveryName) {
        this.firstDeliveryName = firstDeliveryName;
    }

    public String getDeliveryAddress() {
        return deliveryAddress;
    }

    public void setDeliveryAddress(String deliveryAddress) {
        this.deliveryAddress = deliveryAddress;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public Date getCreateDate() {
        return createDate;
    }

    public void setCreateDate(Date createDate) {
        this.createDate = createDate;
    }
    }

OrderItem.java

package net.township.order;

import com.fasterxml.jackson.annotation.JsonBackReference;

import javax.persistence.*;

@Entity
@Table(name = "order_items")
public class OrderItem {

    @Id
    @GeneratedValue
    @Column(name = "id")
    private Long id;

    @Column(name = "order_id")
    private Long orderId;

    public Long getId() {
        return id;
    }

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

    @Column
    private String name;

    @Column
    private String description;

    @Column
    private Long quantity;



    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Long getQuantity() {
        return quantity;
    }

    public void setQuantity(Long quantity) {
        this.quantity = quantity;
    }
    }

答案 1 :(得分:-2)

CascadeType.ALL添加到您的地图中。