0

I am developing a Spring boot application where one of the entities have UUID as ID. When I try to delete the entity, I get the below error with 400. Any input is highly appreciated. I am a new learner, so please be gentle :) Thanks.

Field error in object 'UUID' on field 'mostSigBits': rejected value [null]; codes [typeMismatch.UUID.mostSigBits,typeMismatch.mostSigBits,typeMismatch.long,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [UUID.mostSigBits,mostSigBits]; arguments []; default message [mostSigBits]]; default message [Failed to convert value of type 'null' to required type 'long'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [null] to type [long] for value 'null'; nested exception is java.lang.IllegalArgumentException: A null value cannot be assigned to a primitive type]
  • I think your JPA provider does not understand `UUID` type so tries to save it as entity with `mostSigBits` etc fields. What database do you use? What is JPA provider? – Alexander Pavlov Jul 06 '20 at 03:31
  • For example, here is how you make Hibernate aware of UUID https://stackoverflow.com/questions/25611285/hibernate-uuid-as-uuid-type – Alexander Pavlov Jul 06 '20 at 03:33

1 Answers1

0

You can see an example of it in the following code snippet.

 @Entity
public class Book {

    @Id
    @GeneratedValue(generator = “UUID”)
    @GenericGenerator(
        name = “UUID”,
        strategy = “org.hibernate.id.UUIDGenerator”,
    )
    @Column(name = “id”, updatable = false, nullable = false)
    private UUID id;
    
    …
}

you can read more from here

Mahamudul Hasan
  • 2,745
  • 2
  • 17
  • 26
  • I am autogenerating the UUID by using the below annotations ``` @GeneratedValue(generator = "u") @GenericGenerator(name = "u", strategy = "org.hibernate.id.UUIDGenerator") ``` – vey sammeta Jul 06 '20 at 02:04
  • I added the 'Column' annotation but still getting the same issue. I have a 'OneToMany' mapping to another Entity BookCategory which also have UUID as id. But for the book entity I am trying to delete, there is no BookCategory attached. I am not sure why I am getting the error :( – vey sammeta Jul 06 '20 at 03:51
  • if you have OneToMany relationship then must you have to delete reference record first – Mahamudul Hasan Jul 06 '20 at 04:15
  • but the reference record doesn't exist. It's just an empty list as I didn't add any book categories to the book – vey sammeta Jul 06 '20 at 04:38