【发布时间】:2019-10-07 07:54:38
【问题描述】:
我有一个使用 Spring Data for Couchbase 的 springBoot 2.1.9.RELEASE 应用程序
我有这个东西
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Hostel<T> {
@NotNull
@JsonProperty("_location")
private T location;
}
还有这个
@Document
@Data
@AllArgsConstructor
@NoArgsConstructor
@EqualsAndHashCode(of = { "id" })
@Builder
public class HTMDoc {
@Id
private String id;
@NotNull
@Field
private Hostel hostel;
}
关于服务
public HTMDoc create(@Valid HTMDoc doc) {
return repository.save(doc);
}
在测试中
service.create(new HTMDoc());
但是当我保存时,我得到了这个错误,而不是 hostel 字段中的验证 NotNull
org.springframework.data.mapping.MappingException: An ID property is needed, but not found/could not be generated on this entity.
【问题讨论】:
-
id字段没有_@NotNull,为什么要验证呢?我会把它放在那里,我也会考虑应用 _@GeneratedValue (如果在 create 调用中没有提供 id ......)(嗯,如何在此处编写 java 注释以避免stackoverflow引擎将其识别为用户提及? )
-
但是宿舍区可以
-
是的,这意味着它将检查 HTMDoc.hostel 是否为空。但是您没有向验证引擎告知其他字段。
-
您使用的是哪个 @NotNull 注释?显示完整的包装。
标签: java spring spring-boot spring-data-couchbase