无法将实体保存在休眠状态

时间:2018-11-06 13:04:10

标签: java hibernate jpa orm hibernate-mapping

我已经创建了简单的CRUD服务。有4个实体:客户,提供者,产品,交易。

CustomerProvider实体组成的ID AppId具有以下结构:

@Getter
@Setter
@Embeddable
@NoArgsConstructor
public class AppId implements Serializable {
    private String app;
    private String id;
    //...
}

这是我想要的业务逻辑: 提供者实体级联并创建产品实体。 当客户与提供者打交道时,我需要创建实体Deal,该实体不会层叠任何其他实体。 它只包含引用交易的提供者,客户和产品的字段。

我创建了一些提供商和客户。 然后,我尝试建立交易,但得到了客户和提供者null字段。

Imgur Imgur

这是我的实体定义:


Provider

@Entity
@Getter
@Setter
@ToString
@NoArgsConstructor
@Table(name = "provider")
public class Provider implements Serializable {

    @EmbeddedId
    @Column(name = "appid")
    private AppId appId;

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

    @Column(name = "firstname")
    private String firstName;

    @Column(name = "lastname")
    private String lastName;

    @Column(name = "latitude")
    private float latitude;

    @Column(name = "longitude")
    private float longitude;

    @Column(name = "work_date")
    private Date workDate;

    @ManyToMany(cascade = CascadeType.ALL)
    @JoinTable(name = "provider_product"
            , joinColumns = {
                @JoinColumn(name = "provider_app"),
                @JoinColumn(name = "provider_id")
            }
            , inverseJoinColumns = @JoinColumn(name="product_id"))
    private Set<Product> products;

    @OneToMany(cascade = CascadeType.ALL)
    @JoinColumns({
            @JoinColumn(name = "app", referencedColumnName = "app", updatable = false, insertable = false),
            @JoinColumn(name = "id", referencedColumnName = "id", updatable = false, insertable = false)
    })
    private List<Deal> dealList = new ArrayList<>();
}


Customer

@Entity
@Getter
@Setter
@ToString
@NoArgsConstructor
@Table(name = "customer")
public class Customer implements Serializable {

    @EmbeddedId
    @Column(name = "appid")
    private AppId appId;

    @Column(name = "firstname")
    private String firstName;

    @Column(name = "lastname")
    private String lastName;

    public Customer(AppId appId, String firstName, String lastName) {
        this.appId = appId;
        this.firstName = firstName;
        this.lastName = lastName;
    }
}


Product

@Entity
@Getter
@Setter
@ToString
@NoArgsConstructor
@Table(name = "product")
public class Product implements Serializable {

    @Id
    @GeneratedValue
    private long id;

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

    @Column(name = "cost")
    private long cost;
}


Deal

@Entity
@Getter
@Setter
@ToString
@NoArgsConstructor
@Table(name = "deal")
public class Deal implements Serializable {

    @Id
    @GeneratedValue
    private long id;

    @ManyToOne
    @JoinColumns({
            @JoinColumn(name = "provider_app", referencedColumnName = "app", insertable = false, updatable = false),
            @JoinColumn(name = "provider_id", referencedColumnName = "id", insertable = false, updatable = false)
    })
    private Provider provider;

    @ManyToOne
    @JoinColumns({
            @JoinColumn(name = "customer_app", insertable = false, updatable = false),
            @JoinColumn(name = "customer_id", insertable = false, updatable = false)
    })
    private Customer customer;

    @ManyToMany
    @JoinTable(name = "deal_product"
            , joinColumns = @JoinColumn(name="deal_id", insertable = false, updatable = false)
            , inverseJoinColumns = @JoinColumn(name="product_id", insertable = false, updatable = false))
    private Set<Product> product;

    // deal is complete when provider entered deal id
    @Column(name = "closed")
    private boolean closed = false;
}

1 个答案:

答案 0 :(得分:1)

通过删除insertable = false实体中customerprovider字段的Deal,一切正常。

    {
    "id": 5,
    "provider": {
        "appId": {
            "app": "vk",
            "id": "123"
        },
        "name": null,
        "firstName": null,
        "lastName": null,
        "latitude": 0,
        "longitude": 0,
        "workDate": null,
        "products": null,
        "dealList": []
    },
    "customer": {
        "appId": {
            "app": "vk",
            "id": "123"
        },
        "firstName": null,
        "lastName": null
    },
    "product": [
        {
            "id": 2,
            "name": "Temp",
            "cost": 100
        }
    ],
    "closed": false
}

我可以得到以下答复。

insertable = false表示在保存实体时,您不会保存该字段的值,而是会将该字段显式设置在某个位置。

insertable = true并不意味着您将创建一个新的CustomerProvider,由CascadeType

处理