简体   繁体   中英

convert UUID as blob to proper string id hibernate

I am using UUID is my id (@Id) in entity. Something like this:

import java.util.UUID;
@Table(name="address_book")
@Entity  
public class AddressBook {  
  @Id
  private UUID id;
  private String name;
  .
  .
  .
 }

Now what is happening is, when i call addressBookDao.save(addressBook) or whenever i am saving data in database it is getting stored as:

id, name, isbn, date, username
{blob}, john, isbn-45888, 15-02-2019, david

So in id column i'm getting this blob written their in every row i'm inserting. I thought UUID will generate a random id, instead of that its storing an entire json object of UUID as blob in that id column.

How can i fix this problem. I want string/random value to be present instead of blob/object?

You need to setup proper Hibernate Generator like

@Id
@GeneratedValue(generator = “UUID”)
@GenericGenerator(
    name = “UUID”,
    strategy = “org.hibernate.id.UUIDGenerator”,
)
private UUID id;

Do you really mean BLOB? A UUID is 128 bits, certainly does not qualify for a LOB column. For example you could use a Binary(16) column in MySQL.

Now to answer your question, you need to tell hibernate how to generate the ID value.

    @Id
    @GeneratedValue(generator = "hibernate-uuid")
    @GenericGenerator(name = "hibernate-uuid", strategy = "uuid2")
    @Column(name = "id", columnDefinition = "BINARY(16)")
    protected UUID id;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM