Spring Data Rest:在一对多关系中后调用后,外键更新为空 [英] Spring Data Rest : Foreign key is update with null after post call in one to many relationship
问题描述
我正在使用 spring-data-rest
.
update
和 daily_update
是 2 个具有一对多关系的表.使用 spring boot 运行此应用程序.
update
and daily_update
are 2 table which is having one to many relationship. Running this application with spring boot.
当我使用 post 请求添加数据时,条目被添加到两个表中而没有任何错误,但在子表 (daily_update) 列update_id"(更新表的外键) 中即将 null
.
When i am adding data using post request, entries being added into both table without any error but in child table (daily_update) column "update_id" (foreign key to update table) is coming null
.
我使用 Lombok
作为 setter 和 getter.
I am using Lombok
for setter and getter.
你能帮我解决这个问题吗?
Can you please help me with this?
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;
}
更新存储库:
@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"
}
]
}
并从 Spring Boot 运行此应用程序
and running this application from spring boot
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
推荐答案
我遇到了确切的问题,问题源于表关系的声明.从 UpdateEntity
到 DailyUpdateEntity
,你用下面的形式声明关系;
I have had the exact problem, the issue stems from the declaration of table relationship. From UpdateEntity
to DailyUpdateEntity
, you are declaring the relationship in the following form;
@OneToMany(mappedBy = "updateEntity", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
private Set<DailyUpdateEntity> dailyUpdateEntities = new HashSet<>();
通过分离依赖实体的插入和向其添加外键而导致问题.所以第一次创建操作总是缺少外键.通过以下声明,此问题已解决,并且创建将包含外键;
which causes the issue, by separating the insertion of dependent entity, and addition of foreign key to it. So first create operation will always lack a foreign key. With the following declaration, this issue is resolved, and creation will contain a foreign key;
@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name = "update_id", nullable = false, updatable = false)
private Set<DailyUpdateEntity> dailyUpdateEntities = new HashSet<>();
这篇关于Spring Data Rest:在一对多关系中后调用后,外键更新为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!