2

I have an entity (Car) which is mapped to a table in DB (car). The id of the POJO Car is of UUID and column type of car table is of uniqueidentifier.

The id field looks like this:

  @Id
  @Type(type = "uuid-char")
  @GeneratedValue(generator = "uuid2")
  @GenericGenerator(name = "uuid2", strategy = "uuid2")
  @Column(columnDefinition = "uniqueidentifier", name = "id")
  private UUID id;


  @NotNull
  @Type(type = "uuid-char")
  @ApiModelProperty(required = true, example = "00000000-0000-0000-0000-000000000000")
  @Column(columnDefinition = "uniqueidentifier", name = "make_id")
  private UUID makeId;

The id should be generated and save to DB. But the id was saved wrong. Same problem here. Different representation of UUID in Java Hibernate and SQL Server

I took the @Type(type = "uuid-char") solution and it solved the problem indeed. The id was saved to the db correctly.

But when doing a getById. It seems like hibernate can't find the record by id.

I am using spring boot repository. Using native query cannot find by id.

 @Query(value = QUERY_JOIN + " WHERE lv.car_makeId = ?1 ", nativeQuery = true)
  public List<Car> getCar(UUID makeId);

However, the following is fine:

public List<Car> findByMakeId(UUID makeId);

I am sure the native query is fine. Maybe hibernate is doing some converting/formatting to the UUID?

like: @Type(type = "uuid-char") tells hibernate. "don't do anything with the id. just store it as it is'. But when doing a get using the correct id. It's not able to find it even it's right there in the DB. So, I am guessing that before it uses the id to do a lookup. it's still doing something with it (converting/ formatting) so that it's wrong again. so that it can't find anything any ideas what am I doing wrong in here?

Al-Mothafar
  • 7,949
  • 7
  • 68
  • 102
raccoon
  • 33
  • 5

0 Answers0