[英]convert UUID as blob to proper string id hibernate
I am using UUID is my id (@Id) in entity. 我在实体中使用UUID是我的ID(@Id)。 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: 现在发生的事情是,当我调用addressBookDao.save(addressBook)或每当我将数据保存在数据库中时,它都将存储为:
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. 因此,在id列中,我将这个blob写入要插入的每一行。 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. 我以为UUID会生成一个随机ID,而不是它在该ID列中将UUID的整个json对象存储为Blob。
How can i fix this problem. 我该如何解决这个问题。 I want string/random value to be present instead of blob/object? 我想显示字符串/随机值而不是blob / object?
You need to setup proper Hibernate Generator like 您需要设置适当的Hibernate Generator,例如
@Id
@GeneratedValue(generator = “UUID”)
@GenericGenerator(
name = “UUID”,
strategy = “org.hibernate.id.UUIDGenerator”,
)
private UUID id;
Do you really mean BLOB? 你真的是说BLOB吗? A UUID is 128 bits, certainly does not qualify for a LOB column. 一个UUID是128位,当然不符合LOB列的条件。 For example you could use a Binary(16) column in MySQL. 例如,您可以在MySQL中使用Binary(16)列。
Now to answer your question, you need to tell hibernate how to generate the ID value. 现在回答您的问题,您需要告诉hibernate如何生成ID值。
@Id
@GeneratedValue(generator = "hibernate-uuid")
@GenericGenerator(name = "hibernate-uuid", strategy = "uuid2")
@Column(name = "id", columnDefinition = "BINARY(16)")
protected UUID id;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.