2
我正在使用Spring JPA Restful,并且我不明白如何使用外键插入实体。使用JPA和REST添加使用OneToOne关系的实体
活动实体:
@Entity
@Table(name= "Activity")
public class Activity implements Serializable{
@Id
@GeneratedValue(generator = "uuid")
@GenericGenerator(name="uuid", strategy = "uuid2")
@Column(name = "uuid", nullable = false, unique = true)
private UUID uuid;
@OneToOne(fetch = FetchType.EAGER, cascade=CascadeType.MERGE)
@JoinColumn(name="type", nullable = false)
private ActivityType type;
@Column(nullable = false)
private String label;
ActivityType实体:
@Entity
@Table(name= "ActivityType")
public class ActivityType implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@Column(nullable = false, unique = true)
private String code;
@Column(nullable = false
private String label;
是否可以简单地插入活动?像这样的东西JSON其中ActivityType的ID为 “1” 的存在:
createActivity: { “标签”: “标签”, “类型”:1}
有了这个代码,我必须做的:
createActivity: { “标记”: “LABEL”, “类型”:{ “ID”:1}}
其返回值为:
{
"uuid": "a54b27aa-8d49-41fd-8976-70c019c40e3b",
"type": {
"id": 1,
"code": null,
"label": null
},
"label": "LABEL",
"details": null
}