通过REST调用使用外键将项添加到集合中 [英] add item to the collection with foreign key via REST call
问题描述
我有2个具有双向关联的jpa实体。
I have 2 jpa entities with bidirectional associasion.
包含项目集合的实体容器
(oneToMany)
Ommiting getter / setters
Entity Container
that holds collection of items (oneToMany)
Ommiting getter/setters
@javax.persistence.Entity
@Table(name = "CONTAINER")
public class Container implements Serializable {
private static final long serialVersionUID = -3288335692695653843L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "ID")
private Long id;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "container", cascade = CascadeType.ALL)
private List<Item> items;
}
实体项目
包含返回容器(ManyToOne)的引用,带有属性值和日期。
Ommiting setter / getters
Entity Item
contains reference back to the container (ManyToOne), with attributes value and date.
Ommiting setter/getters
@javax.persistence.Entity
@Table(name = "ITEM")
public class Record implements Serializable {
private static final long serialVersionUID = -758343957629274274L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "ID")
private Long id;
@Basic
private Long value;
@Basic
private Date date;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "CONTAINER_ID")
private Container container;
}
我也使用spring-data存储库来公开数据。
Also i am using spring-data repositories to expose data.
My Interface存储库只是扩展 CrudRepository< Container,Long>
和 CrudRepository< Item,Long>
My Interface repositories just extends CrudRepository<Container, Long>
and CrudRepository<Item, Long>
@RepositoryRestResource
public interface ItemRepository extends CrudRepository<Item, Long> {
}
@RepositoryRestResource
public interface ContainerRepository extends CrudRepository<Container, Long> {
}
我正在尝试通过REST调用创建项目。
I am trying to create items via REST calls.
首先我在物品储存库上试过这个 rest / items
First i tried this on items repository rest/items
POST { "value" : 666, "date" : "2016-01-31T23:00:00.000+0000", "container": {"id":"1"}}
但它只是在容器上创建带有空引用的项目。
But it just creates item with null reference on container.
当我尝试通过容器存储库添加 rest / containers / 1 / items
When i try to add via container repository rest/containers/1/items
POST { "value" : 666, "date" : "2016-01-31T23:00:00.000+0000", "container": {"id":"1"}}
我得到 HTTP / 1.1 204无内容
和<响应正文为空>
。没有创建实例。
I get HTTP/1.1 204 No Content
and <Response body is empty>
. No instance is created.
我的问题是如何通过引用容器的REST调用添加项目。
My question is how i can add item via REST call that has reference to the container.
编辑:要指定我的问题,我想为现有容器添加新项目。我不确定如何在通过rest(json)创建Item的实例时处理外来ID键
To specify my question, i want to add new item for the existing container. I am not sure how to deal with with foreign ID key when creating instance of Item via rest(json)
推荐答案
我解决了这个问题通过使用链接到json内的容器来解决问题。
I resolved this issue by using link to the container inside json.
POST {value:666,date:2016-01-31T23: 00:00.000 + 0000,容器:http:// localhost:8080 / container / 1}
我不是确定它是否可以在没有 spring-data-rest
I am not sure if it works without spring-data-rest
编辑:我应该指出,该链接资源必须是 @RepositoryRestResource
,应该是聚合根。
I should point out, that linked resource has to be @RepositoryRestResource
and should be aggregate root.
这篇关于通过REST调用使用外键将项添加到集合中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!