[英]Hibernate: Cannot find by UUID
Im trying to use UUID's as ids for my database but I simply do not get it working.我试图将 UUID 用作我的数据库的 id,但我根本无法让它工作。
First attempt was:第一次尝试是:
@Id
@GeneratedValue(generator = "hibernate-uuid")
@GenericGenerator(name = "uuid", strategy = "uuid4")
private UUID id;
but this is generating some ugly byte code.但这会产生一些丑陋的字节码。 So I added a type annotation:
所以我添加了一个类型注释:
@Id
@GeneratedValue(generator = "hibernate-uuid")
@GenericGenerator(name = "uuid", strategy = "uuid4")
@Type(type="org.hibernate.type.UUIDCharType")
private UUID id;
with this I get a character representation in my mysql database but querying the database using my repository:有了这个,我在我的 mysql 数据库中得到了一个字符表示,但使用我的存储库查询数据库:
public interface CommentsRepository extends CrudRepository<Comment, UUID> {
Comment findById(final UUID imageId);
}
wont find any result - even if an entry with the given UUID exists it wont return the result.不会找到任何结果 - 即使具有给定 UUID 的条目存在,它也不会返回结果。 Even if I use plain SQL directly on my database it wont find any result.
即使我直接在我的数据库上使用普通的 SQL 也不会找到任何结果。
Is there something else I need to do to get UUID's working?我还需要做些什么才能让 UUID 正常工作吗?
EDIT编辑
Trying this:试试这个:
@Data
@Entity
public class Comment implements Serializable {
@Id
@GeneratedValue(generator = "uuid2")
@GenericGenerator(name = "uuid2", strategy = "uuid2")
@Type(type = "uuid-char")
private UUID id;
}
and adding some default values:并添加一些默认值:
@Component
@Slf4j
public class CommentsSampleData implements CommandLineRunner {
private final CommentsRepository repository;
@Autowired
public CommentsSampleData(final CommentsRepository repository) {
this.repository = repository;
}
@Override
public void run(String... args) {
repository.save(new Comment());
repository.save(new Comment());
repository.save(new Comment());
repository.save(new Comment());
}
}
Results in the following table:结果如下表:
performing:执行:
SELECT * FROM comment WHERE id = 'b076a9f7-7e9e-4f5a-91f8-e66c7d076fac'
results in:结果是:
which means no result but there should be one.这意味着没有结果,但应该有一个。 Using jpa also does not return anything.
使用 jpa 也不会返回任何内容。
Have you tried this uuid annotation maybe:您是否尝试过此 uuid 注释:
@Id
GeneratedValue(generator = "uuid2")
@GenericGenerator(name = "uuid2", strategy = "uuid2")
@Column(columnDefinition = "BINARY(16)")
private UUID id;
This sample should work with java.util.UUID
.此示例应与
java.util.UUID
一起使用。
EDIT: I've read that you could run into problems with having a binary type set, so you could also try with explicitly setting it to a char
uuid with:编辑:我读过您可能会遇到设置二进制类型的问题,因此您也可以尝试将其显式设置为
char
uuid:
@Type(type="uuid-char")
It works with the following:它适用于以下内容:
@Id
@GeneratedValue(generator = "uuid4")
@GenericGenerator(name = "UUID", strategy = "uuid4")
@Type(type = "org.hibernate.type.UUIDCharType")
@Column(columnDefinition = "CHAR(36)")
private UUID id;
When I was using the 2.1.212 version of H2 in memory database, I faced the same problem on my project.当我在 memory 数据库中使用 2.1.212 版本的 H2 时,我在项目中遇到了同样的问题。 I converted H2 version from 2.1.212 to 1.4.200 and it solved
我将 H2 版本从 2.1.212 转换为 1.4.200 并解决了
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.