使用JPA和REST添加具有OneToOne Relation的实体

时间:2015-08-24 09:53:20

标签: spring hibernate jpa

我正在使用Spring JPA Restful,我不了解如何使用外键插入实体。

活动实体:

@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;

是否可以简单地插入Activity?有了像JSON这样的东西,其中ActivityType的id" 1"存在:

createActivity:     {"标签":" LABEL""类型":1}

使用此代码,我必须这样做:

createActivity:     {"标签":" LABEL""类型" {" ID":1}}

返回值为:

{
 "uuid": "a54b27aa-8d49-41fd-8976-70c019c40e3b",
 "type": {
   "id": 1,
   "code": null,
   "label": null
 },
 "label": "LABEL",
 "details": null
}

0 个答案:

没有答案