FindByUUID() using Spring Data's JPA Repositor

2019-07-23 07:04发布

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!

4条回答
时光不老,我们不散
2楼-- · 2019-07-23 07:37

Had the same problem, specifically it works in H2 but not in MySQL.

In addition I got PRIMARY key constraint failures attempting to update the record because Hibernate (under JPA) was querying to see if the record exists and it did not find it.

Using @Column(length=16) also solves this problem neatly, assuming MySQL is using a BINARY column... otherwise the matching will fail due to the column having extra data in the DB (I think it defaults to BINARY[32]).

查看更多
Deceive 欺骗
3楼-- · 2019-07-23 07:44

I tackled the same question recently, and if someone stumbles up here, the solution for me was to annotate the column with

@Column(name = "id", columnDefinition = "BINARY(16)")
private UUID id;

which solved the problem for me.

查看更多
聊天终结者
4楼-- · 2019-07-23 07:50

Change binnary column to String. Default is binnary you must add this addnotation

@Type(type="org.hibernate.type.UUIDCharType")
查看更多
聊天终结者
5楼-- · 2019-07-23 07:51

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.

查看更多
登录 后发表回答