Gondy
Gondy

Reputation: 5305

Hibernate is getting wrong ID values when using UUID as primary key

My entity:

@Entity
@Table(name = "eh_portal")
public class PortalEntity {

    @Id
    @Column(name = "id", columnDefinition = "CHAR(36)")
    private UUID id; //java.util.UUID;

    @Column(name = "name")
    private String name;

    @Column(name = "url")
    private String url;

    // -- Constructor for Hibernate --
    protected PortalEntity() {
    }

    // -- Constructor for new entity in service code --
    public PortalEntity(final UUID id) {
        this.id = id;
    }
    .... getters and setters ommited
}

Respository is Spring DATA JPA:

public interface PortalRepository extends CrudRepository<PortalEntity, UUID> {

}

MYSQL 5 Database table definition:

CREATE TABLE `eh_portal` (
  `id` char(36) NOT NULL COMMENT 'UUID',
  `name` varchar(255) NOT NULL,
  `url` varchar(255) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `url_UNIQUE` (`url`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8

The problem is, Hibernate is returning obviously wrong data - see screenshots below Mysql workbench:

enter image description here

Actual web page where I get entities thru Spring Data JPA: enter image description here

You can see that UUIDs are obviously differrent, while other columns are correct. What is wrong here? (Spring 4, Hibernate 4, Spring DATA JPA, Mysql 5)

Upvotes: 3

Views: 3442

Answers (1)

Sainik Kumar Singhal
Sainik Kumar Singhal

Reputation: 811

Try using @Type(type="uuid-char").

Upvotes: 17

Related Questions