【发布时间】:2020-08-05 05:42:44
【问题描述】:
我有以下实体:
@Data
@Entity
public class Comment implements Serializable {
@Id
@GeneratedValue(generator = "uuid4")
@GenericGenerator(name = "UUID", strategy = "uuid4")
@Column(columnDefinition = "BINARY(16)")
private UUID id;
@Column(columnDefinition = "BINARY(16)")
private UUID imageId;
private Instant creationTime;
private String text;
}
还有一个 CRUD 回购:
public interface CommentsRepository extends CrudRepository<Comment, UUID> {
List<Comment> findAllByImageId(final UUID imageId);
}
我添加一些示例数据:
@Component
@Slf4j
public class CommentsSampleData implements CommandLineRunner {
private final CommentsRepository repository;
@Autowired
public CommentsSampleData(final CommentsRepository repository) {
this.repository = repository;
}
@Override
public void run(String... args) {
createComment("617220ff-1642-4490-b589-869e7978c5e0", Instant.now(), "comment1");
createComment("617220ff-1642-4490-b589-869e7978c5e0", Instant.now(), "comment2");
createComment("617220ff-1642-4490-b589-869e7978c5e0", Instant.now(), "comment3");
createComment("e3a8aa57-6937-4f9e-b117-78bafe61b718", Instant.now(), "comment1");
}
private void createComment(
final String imageId,
final Instant creationTime,
final String text) {
final Comment comment = new Comment();
comment.setImageId(UUID.fromString(imageId));
comment.setCreationTime(creationTime);
comment.setText(text);
log.info("save comment: {}", comment);
repository.save(comment);
}
}
所以我表中的数据如下所示:
那么现在通过这些二进制 UUID 选择的最佳方法是什么? 我将从前端获取字符串 UUID 所以我想我需要以某种方式将这些字符串转换为二进制文件。这样做的最佳方法是什么,以便它也适用于 id 和主键。
示例端点:
@Slf4j
@RestController
public class CommentsController {
private final CommentsService service;
public CommentsController(final CommentsService service) {
this.service = service;
}
@GetMapping(value = "/comments", produces = MediaType.APPLICATION_JSON_VALUE)
public List<Comment> getComments(@RequestParam("imageId") final UUID imageId) {
log.info("get comments by imageId: {}", imageId);
String existingIds = service.findAll().stream()
.map(Comment::getImageId)
.map(UUID::toString)
.collect(Collectors.joining(","));
log.info("Image Id Passed: {}", imageId);
log.info("Existing image ids: {}", existingIds);
String resultIds = service.findAllByImageId(imageId).stream()
.map(Comment::getImageId)
.map(UUID::toString)
.collect(Collectors.joining(","));
log.info("Result image ids: {}", resultIds);
return service.findAllByImageId(imageId);
}
}
当我现在提出请求时:
localhost:8080/comments?imageId=617220ff-1642-4490-b589-869e7978c5e0
即使 UUID 存在但不是字符串,我也没有得到任何结果,它在数据库中以二进制 (16) 的形式存在:
d.f.a.c.service.CommentsController : Image Id Passed: 617220ff-1642-4490-b589-869e7978c5e0
d.f.a.c.service.CommentsController : Existing image ids: 617220ff-1642-4490-b589-869e7978c5e0,617220ff-1642-4490-b589-869e7978c5e0,617220ff-1642-4490-b589-869e7978c5e0,e3a8aa57-6937-4f9e-b117-78bafe61b718
d.f.a.c.service.CommentsController : Result image ids:
【问题讨论】:
-
如果你只是用 UUID 保存你的实体,现在会发生什么?不会自动转换为二进制并存储吗?
-
确实如此。 UUID 被存储为二进制 16,如屏幕截图所示。问题是我无法查询它们。我使用示例端点和 GET 更新了我的帖子。
-
你为什么首先将它们存储为二进制文件?它基本上序列化了 UUID 类。现在,如果事情发生变化,这将不再用于反序列化。为什么不简单地将其存储为 UUID(如果您的数据库支持 or)或 char(36)?
-
因为将其存储为二进制 16 更有效:devforth.io/blog/why-your-software-should-use-uuids