本文介绍了字符串ID生成器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在jpa中实现字符串ID的最简单方法是什么?到目前为止,我所拥有的是
what's the easiest way to implement a string id in jpa ?So far what I have is
@Id
@GeneratedValue
private int id;
我想拥有的是类似的东西
and what I'd like to have is something like
@Id
@GeneratedValue
private String id;
但是如果我这样使用它,我会得到此id生成器生成长,整数,短".
but if I use it like this, I get 'this id generator generates long, integer, short'.
推荐答案
您可以像这样从Java创建UUID:
You can create the UUID from Java like this:
UUID.randomUUID().toString();
或者,如果您的JPA支持它(如Hibernate一样),则可以使用:
Or if your JPA supports it, like Hibernate does, you can use:
@Id @GeneratedValue(generator="system-uuid")
@GenericGenerator(name="system-uuid", strategy = "uuid")
private String myId;
如果您用Google搜索"JPA UUID",则有很多选择.
If you google for "JPA UUID" there are many alternatives.
这篇关于字符串ID生成器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!