gpt4 book ai didi

java - Spring 数据休息 : Foreign key is update with null after post call in one to many relationship

转载 作者:搜寻专家 更新时间:2023-11-01 02:20:55 25 4
gpt4 key购买 nike

我正在使用 spring-data-rest

updatedaily_update 是一对多关系的 2 个表。使用 spring boot 运行此应用程序。

当我使用 post 请求添加数据时,条目被添加到两个表中而没有任何错误,但在子表 (daily_update) 列“update_id”(更新表的外键)中即将 null

我正在使用 Lombok 作为 setter 和 getter。

你能帮我解决这个问题吗?

更新实体类:

@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 请求来自 postman 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 运行这个应用程序

@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}

最佳答案

我遇到了确切的问题,问题源于表关系的声明。从 UpdateEntityDailyUpdateEntity,您以以下形式声明关系;

@OneToMany(mappedBy = "updateEntity", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
private Set<DailyUpdateEntity> dailyUpdateEntities = new HashSet<>();

通过分离依赖实体的插入和向其添加外键来导致问题。所以第一次创建操作总会缺少外键。通过下面的声明,这个问题就解决了,创建的时候会包含一个外键;

@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name = "update_id", nullable = false, updatable = false)
private Set<DailyUpdateEntity> dailyUpdateEntities = new HashSet<>();

关于java - Spring 数据休息 : Foreign key is update with null after post call in one to many relationship,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43806173/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com