我正在使用spring-data-rest
。
update
和daily_update
是2个表,它具有一对多的关系。使用spring boot运行此应用程序。
当我使用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"
}
]
}
从spring boot
运行此应用程序@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
答案 0 :(得分:4)
我遇到了确切的问题,问题源于表关系的声明。从UpdateEntity
到DailyUpdateEntity
,您将以下列形式声明关系;
@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<>();
答案 1 :(得分:0)
似乎问题是你试图将关系保持在错误的一面。
您正在发布一个UpdateEntity(针对UpdateRepository),您将Set<DailyUpdateEntity>
集合映射为mappedBy = "updateEntity"
,这意味着这只是双向关联的只读方,并且之间没有连接实体。
如果您要增强DailyUpdateEntity映射,则会遇到非空约束错误:
@ManyToOne(optional = false, fetch = FetchType.EAGER)
@JoinColumn(name = "update_id")
private UpdateEntity updateEntity;
也许您应该调整您的映射或考虑另一个存储策略/订单。