FindByUUID() 使用 Spring Data 的 JPA 存储库

FindByUUID() using Spring Data#39;s JPA Repository(FindByUUID() 使用 Spring Data 的 JPA 存储库)
本文介绍了FindByUUID() 使用 Spring Data 的 JPA 存储库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出于某种原因,我无法为此找到合适的答案.我有以下简单实体:

for some reason I have not being able to find a suitable answer for this. I have the following simple entity:

@Entity
@Table(name = "simple_entity")
@Access(AccessType.FIELD)
public class SimpleEntity {

  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  protected Long id;

  @Column(unique = true, updatable = false)
  protected UUID uuid;

  @PrePersist
  protected void onCreateAbstractBaseEntity() {
      this.uuid = UUID.randomUUID();
  }

  public Long getId() {
      return this.id;
  }

  public UUID getUuid() {
      return this.uuid;
  }
}

带有 Hibernate 的 Spring Data JPA 在我的 MySQL 数据库中正确创建了所有内容.但是,当我尝试使用我的 JPARepository 实现来搜索使用其 uuid 的项目时,它永远找不到任何东西,即使它在数据库上执行查找查询(我可以在调试器中看到).这是我的 JPARepository 实现:

Spring Data JPA with Hibernate creates everything correctly in my MySQL database. However, when I try to use my JPARepository implementation to search for an item using its uuid, it never finds anything, even though it executes the find query on the DB (which I can see in my debugger). Here is my JPARepository implementation:

public interface SimpleEntityRepository extends JpaRepository<SimpleEntity, Long> {
      SimpleEntity findOneByUuid(UUID uuid);
}

这是调用此方法的控制器.

Here is the controller that calls this method.

@Controller
@RequestMapping("/simple_entity")
public class SimpleEntityController {

@Autowired
private SimpleEntityRepository repository;

@RequestMapping(method = RequestMethod.GET, value = "/{simpleEntityId}", produces =        MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<FileDatastore> getSimpleEntity(@PathVariable UUID simpleEntityId)     {
    SimpleEntity record = this.repository.findOneByUuid(simpleEntityId);
    HttpHeaders headers = new HttpHeaders();

    HttpStatus status = (record != null) ? HttpStatus.OK : HttpStatus.NOT_FOUND;

    return new ResponseEntity<>(record, headers, status);
}

我错过了什么吗?

感谢您的帮助!

推荐答案

尝试使用 @org.hibernate.annotations.Type(type="org.hibernate.type) 注释您的 UUID 属性.UUIDCharType")

@org.hibernate.annotations.Type(type="org.hibernate.type.UUIDBinaryType")

Try annotate your UUID property with @org.hibernate.annotations.Type(type="org.hibernate.type.UUIDCharType")
or
@org.hibernate.annotations.Type(type="org.hibernate.type.UUIDBinaryType")

我在使用 UUID 查询数据库时遇到了类似的问题,因为 MSB/LSB 与二进制格式的 UUID 交换;我们解决了将数据视为字符串并在转换前进行必要转换的问题.

I faced a problem similar while query database with UUID due to MSB/LSB swap with UUID in binary format; we solved the problem treating data as String and do necessary conversion before conversion.

这篇关于FindByUUID() 使用 Spring Data 的 JPA 存储库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Hibernate reactive No Vert.x context active in aws rds(AWS RDS中的休眠反应性非Vert.x上下文处于活动状态)
Bulk insert with mysql2 and NodeJs throws 500(使用mysql2和NodeJS的大容量插入抛出500)
Flask + PyMySQL giving error no attribute #39;settimeout#39;(FlASK+PyMySQL给出错误,没有属性#39;setTimeout#39;)
auto_increment column for a group of rows?(一组行的AUTO_INCREMENT列?)
Sort by ID DESC(按ID代码排序)
SQL/MySQL: split a quantity value into multiple rows by date(SQL/MySQL:按日期将数量值拆分为多行)