Spring Data Rest:在一对多关系中调用post后,外键更新为null
我正在使用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 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 { }
从邮递员http://localhost:8080/update
发出POST
请求
{ "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); } }
我遇到了确切的问题,问题源于表关系的声明。 从UpdateEntity
到DailyUpdateEntity
,您将以下列forms声明关系;
@OneToMany(mappedBy = "updateEntity", fetch = FetchType.LAZY, cascade = CascadeType.ALL) private Set dailyUpdateEntities = new HashSet<>();
通过分离依赖实体的插入和向其添加外键导致问题。 所以首先创建操作总是缺少一个外键。 使用以下声明,此问题已解决,创建将包含外键;
@OneToMany(cascade = CascadeType.ALL) @JoinColumn(name = "update_id", nullable = false, updatable = false) private Set dailyUpdateEntities = new HashSet<>();
看来问题是你试图坚持错误的关系。
您正在发布一个UpdateEntity(针对UpdateRepository),您将Set
集合映射为mappedBy = "updateEntity"
,这意味着这只是双向关联的只读方,实体之间将没有连接。
如果要增强DailyUpdateEntity映射,则会遇到非空约束错误,如:
@ManyToOne(optional = false, fetch = FetchType.EAGER) @JoinColumn(name = "update_id") private UpdateEntity updateEntity;
也许您应该调整您的映射或考虑另一个存储策略/订单。
- 在FreeMarker模板中处理错误的不同方法有哪些?
- 哪个“user”来自convertAndSendToUser在SockJS + Spring Websocket中工作?
- JMenuBar SelectionModel ChangeListener仅触发一次
- OptaPlanner中的穷举搜索不适用于非常简单的示例
- Javamail:在搜索邮件时可以使用FetchProfile而不是仅仅获取邮件吗?
- 没有节点的数据类型:org.hibernate.hql.internal.ast.tree.IdentNode HQL
- 如何自定义Eclipse生成的hashCode()和equals()?
- Spring Webflow – 决策状态与动作状态
- 如何通过网络将String从Java发送到JavaScript?