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

问题描述

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

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

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

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

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

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

public UUID getUuid(){
return this.uuid; Spring数据JPA和Hibernate在我的MySQL中创建了一切正确的数据数据库。但是,当我尝试使用我的JPARepository实现来使用它的uuid搜索一个项目时,即使它在数据库上执行find查询(我可以在调试器中看到它),它也找不到任何东西。这是我的JPARepository实现:
$ b $ pre $ public interface SimpleEntityRepository扩展JpaRepository< SimpleEntity,Long> {
SimpleEntity findOneByUuid(UUID uuid);

$ / code>

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

  @Controller 
@RequestMapping(/ simple_entity)
公共类SimpleEntityController {

@Autowired
私人SimpleEntityRepository存储库;

@RequestMapping(method = RequestMethod.GET,value =/ {simpleEntityId},产生= 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;

返回新的ResponseEntity<>(记录,标题,状态);
}

我缺少什么?



感谢您的帮助!

解决方案

尝试注释您的 UUID @ org.hibernate.annotations.Type(type =org.hibernate.type.UUIDCharType)

或$ @ org.hibernate.annotations.Type(type =org.hibernate.type.UUIDBinaryType)



I由于MSB / LSB以二进制格式与 UUID 交换,所以在查询带有 UUID 的数据库时遇到类似的问题。我们解决了将数据作为字符串处理的问题,并在转换之前进行了必要的转换。

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;
  }
}

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);
}

Am I missing something?

Thanks for your help!

解决方案

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")

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存储库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 23:21