FindByUUID() using Spring Data's JPA Repository

spring boot jpa uuid mysql
spring boot jpa uuid generator
spring data jdbc uuid
jpa uniqueidentifier
spring data jpa primary key string
spring data mongodb uuid as id
jpql cast uuid to string
jpa id generator

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

@Table(name = "simple_entity")
public class SimpleEntity {

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

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

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

  public Long getId() {

  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.

public class SimpleEntityController {

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.

This is useful for example to model an update operation: create an enwtity with a known id and updated values, then call save() on such entity. In this tutorial, we will learn how to use save(), findById(), findAll(), and deleteById() methods of JpaRepository (Spring data JPA) with Spring Boot. As we know that Spring is a popular Java application framework. Spring Boot is an effort to create stand-alone, production-grade Spring-based applications with minimal effort.

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.

isNew() so that Spring Data repositories know whether to call EntityManager.​persist() or ….merge() . 3, Declare a method using JPA entity callbacks so that the  Let’s see an example of Spring Data JPA CrudRepository findById() Example where we will use save() method for creating the entity and findById() to get a single record. Open eclipse and create maven project, Don’t forget to check ‘Create a simple project (skip)’ click on next.

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


Spring was creating the field in the database as binary(255) The correct one is binary(16) So, for get the query working I had to use this annotation on the entity​  spring.main.banner-mode=off spring.datasource.platform=h2 spring.jpa.hibernate.ddl-auto=none The is Spring Boot's main configuration file. The Spring Boot banner is turned off with the spring.main.banner-mode property. The spring.datasource.platform sets the vendor name of the database. It is used in the initialization

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]).

The default way in JPA for primary keys is to use the @GeneratedValue final long serialVersionUID = 1L; @Id private String id; public AbstractBaseEntity()  Spring Data’s mission is to provide a familiar and consistent, Spring-based programming model for data access while still retaining the special traits of the underlying data store. It makes it easy to use data access technologies, relational and non-relational databases, map-reduce frameworks, and cloud-based data services.

Postgres supports a variety of data types that allow data architects to System.​out.println("There are " + Students.size() + " rows in the table");. spring-data-examples / src / main / java / com / kubrynski / data / repository / generic / Find file Copy path jkubrynski Custom generic repository 7a0a1a2 Oct 22, 2013

toString(); } @Override public int hashCode() { return id. If we want to serialize this data as JSON via a Spring REST Controller, then most we  You can also use curl or any other REST client to issue PUT, PATCH, and DELETE requests. It also is important to note that Spring Data REST automatically follows the principles of HATEOAS. HATEOAS is one of the constraints of the REST architecture style, and it means that hypertext should be used to find your way through the API.

Contribute to jkubrynski/spring-data-examples development by creating an account on GitHub. import​JpaRepository;. jkubrynski / spring-data-examples. Watch 0 Star 10 Fork 11 Code. Issues 0. Pull requests 0. Actions Projects 0. public T findByUuid (String uuid)

  • Show us the implementation and the relevant code where you are calling the method.
  • Do you mean annotate thefield: protected UUID uuid; with that?