JPA:如何通过字符串 UUID 选择二进制(16)UUID?

问题描述 投票:0回答:2

我有以下实体:

@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:

spring jpa uuid
2个回答
1
投票
  • 它按预期工作,没有任何问题,并且在 UUID 和二进制之间自动转换。

  • 我建议尝试以下操作以确保 id 确实存在于数据库中。

    @GetMapping(value = "/comments", produces = MediaType.APPLICATION_JSON_VALUE)
    public Iterable<Comment> getComments(@RequestParam("imageId") 
                                          final UUID imageId) {
        log.info("get comments by imageId: {}", imageId);

        String existingIds = service.findAll()
                .map(Comment::getImageId)
                .map(UUID::toString)
                .collect(Collectors.joining(","));
        
        log.info("Image Id Passed : {}", imageId);
        log.info("Existing image ids : {}", existingIds);

        return service.findAllByImageId(imageId);
    }

0
投票

另一种方式是

@Query(value = "select BIN_TO_UUID(id), comment from comments", nativeQuery = true)
List<String> findIdAndComments();

BIN_TO_UUID 会将二进制(16)转换为 UUID。

© www.soinside.com 2019 - 2024. All rights reserved.