我正在使用spring-data-rest
。春季数据休息:在一对多关系调用后,外键更新为空
update
和daily_update
是2个表,它们有一对多的关系。使用spring引导运行此应用程序。
当我使用post请求添加数据时,条目被添加到两个表中而没有任何错误,但在子表(daily_update)列“update_id”(更新表的外键)即将到来null
。
我使用Lombok
作为setter和getter。
你能帮我解决吗?
UpdateEntity类:
@Data
@Entity
@Table(name = "update")
public class UpdateEntity {
@Id
@Column(name = "id")
@GeneratedValue(generator = "UUID")
@GenericGenerator(name = "UUID", strategy = "org.hibernate.id.UUIDGenerator")
private String id;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@Column(name = "start_time")
private Date startTime;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@Column(name = "end_time")
private Date endTime;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@Column(name = "date_created")
private Date dateCreated;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@Column(name = "date_modified")
private Date dateModified;
@OneToMany(mappedBy = "updateEntity", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
private Set<DailyUpdateEntity> dailyUpdateEntities = new HashSet<>();
}
DailyUpdateEntity类:
@Data
@Entity
@Table(name = "daily_update")
public class DailyUpdateEntity {
@Id
@Column(name = "id")
@GeneratedValue(generator = "UUID")
@GenericGenerator(name = "UUID", strategy = "org.hibernate.id.UUIDGenerator")
private String id;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "update_id")
private UpdateEntity updateEntity;
@Column(name = "dimension_id")
private String dimensionId;
@Column(name = "hour")
private Integer hour;
@Column(name = "updated_type_id")
private String updatedTypeId;
}
UpdateRepository:
@RepositoryRestResource(collectionResourceRel = "update", path = "update")
public interface UpdateRepository extends CrudRepository<UpdateEntity, String> {
}
POST
要求从邮递员http://localhost:8080/update
{
"startTime" : "2016-08-18 10:34:26",
"endTime" : "2016-08-19 10:34:26",
"dateCreated" : "2016-06-18 10:34:26",
"dateModified" : "2016-06-18 10:34:26",
"dailyUpdateEntities" :
[
{
"dimensionId" : "6ea91f60-2b1d-11e7-93ae-92361f002671",
"hour" : "01",
"updatedTypeId" : "6ea9273a-2b1d-11e7-93ae-92361f002671"
},
{
"dimensionId" : "6ea92636-2b1d-11e7-93ae-92361f002671",
"hour" : "02",
"updatedTypeId" : "6ea92816-2b1d-11e7-93ae-92361f002671"
}
]
}
击中并运行从春天启动
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
请同时发布持久数据的代码。 –
@AbdullahWasi:我坚持使用邮递员来电。同样的问题已经提到。并通过弹簧启动运行此应用程序 –