0

I have below entity configuration

@Entity
@RestResource
public class Employee  {
    @Id
    @GeneratedValue
    @Type(type = "pg-uuid")
    private UUID id;

    @OneToMany(mappedBy = "employee")
    @RestResource(path = "projects", rel = "projects")
    private List<EmployeeToProject> projects;
}

@Entity
@RestResource
public class Project {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @OneToMany(mappedBy = "project", cascade = CascadeType.ALL, orphanRemoval = true)
    @RestResource(path = "employees", rel = "employees")
    private List<EmployeeToProject> employees;
}

@Entity
@RestResource
public class EmployeeToProject {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @ManyToOne
    @RestResource(path = "employee", rel = "employee")
    private Employee employee;

    @ManyToOne
    @RestResource(path = "project", rel = "project")
    private Project project;
}

And repository for EmployeeToProject with @RepositoryRestResource to make crud apis available

@RepositoryRestResource
@CrossOrigin("*")
public interface EmployeeToProjectRepository extends PagingAndSortingRepository<EmployeeToProjectRepository, Long> {
    
}

I am trying to create a record in employee_to_project by using the data rest api provided using the paths enter image description here

this is giving me a numberformatexception as it is expecting a number for employee ID but I have configured employeed ID as UUID

"message": "JSON parse error: Failed to convert from type [java.lang.String] to type [java.lang.Long] for value 'e21d6b85-371f-485d-ba5b-b807e2c79093'; nested exception is java.lang.NumberFormatException: For input string: \"e21d6b85-371f-485d-ba5b-b807e2c79093\"; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Failed to convert from type [java.lang.String] to type [java.lang.Long] for value 'e21d6b85-371f-485d-ba5b-b807e2c79093'; nested exception is java.lang.NumberFormatException: For input string: \"e21d6b85-371f-485d-ba5b-b807e2c79093\"

This api works if I change Emplyee ID from UUID to Long.

How I still can use UUID and utilize the data rest apis provided by Spring

user09
  • 920
  • 2
  • 12
  • 38
  • In your query param take it as String(uuidStr for example) And where you invoke repository method use UUID.fromString(uuidStr). Might solve your issue. Hopefully it will. – dauthDaert06 Aug 03 '23 at 15:44
  • @dauthDaert06 I do not have custom controller api. API is generated by Spring and I want to depend on it if possible, so do not need to create apis manually. Also this works default for Long ids. So issue is with UUID as it by default expect IDs be Long – user09 Aug 03 '23 at 16:03
  • The error you have has nothing to do with your entity, or postgres. It looks like an error from your POST where you're sending a String and the ObjectMapper cannot convert the String you're sending into a Long that you've defined the field as. – lane.maxwell Aug 03 '23 at 16:04
  • As i mentioned in the post, if I change the UUID to Long and send a number `employees/1` it works. So data rest generated apis works only for Long and not working for UUID. – user09 Aug 03 '23 at 16:52
  • I made a mistake in when declaring `public interface EmployeeToProjectRepository extends PagingAndSortingRepository { }` by using Long instead of UUID. that is the reason it is expecting Long instead of UUID – user09 Aug 08 '23 at 18:15

0 Answers0