在 Spring Boot 3.2.4 (Hibernate 6.4.4) 中为具有嵌入 ID 的实体生成 Null Id

问题描述 投票:0回答:1

我在 Spring Boot 3.2.4(包括 Hibernate 6.4.4)中使用嵌入式 ID 保存实体时遇到了一个特殊问题。 Spring Boot 3.1.1 不会出现此问题(大概是 Hibernate 6.2.x)。

Caused by: org.springframework.orm.jpa.JpaSystemException: Null id generated for entity ABC  [...] Caused by: org.hibernate.id.IdentifierGenerationException: Null id generated for entity ABC [...] 

关键观察:

该错误似乎是由较新的 Hibernate 版本 (6.4.4) 专门触发的。降级到 6.2.5 成功避免了该问题。

潜在原因:

我的代码在实体类上使用 @PrePersist 注释。我可以确认使用 Hibernate 6.4.4 时不会调用此注释。相比之下,它在 Hibernate 6.2.5 中(或使用 Spring Boot 3.1.1 时)按预期运行。这可能与新版本中的内部 Hibernate 逻辑更改有关。 有人遇到过类似的问题或有解决此问题的建议吗?

提前谢谢您!

如果您想复制此示例,请创建一个具有复合主键的新实体。您还需要创建一个引用此复合主键实体的新实体。

@Entity public class LocalizedText { @EmbeddedId private CompositeKey contentId; @OneToMany() private LocalizedContent localizedContent; @Embeddable public static class CompositeKey implements Serializable { private String id; private Char language; } @PrePersist public void preBeforePersist() { if (contentId == null) { contentId = new CompositeKey(localizedContent.getId(), language); } } }

使用的依赖项:

<dependency> <groupId>org.hibernate.orm</groupId> <artifactId>hibernate-core</artifactId> <version>6.2.5.Final</version> </dependency>

我已经彻底搜索了现有问题,但没有找到直接匹配的问题。

将 JPA 和 Hibernate Core 升级到最新版本并不能解决该错误。

清理和重建项目也被证明是无效的。

hibernate
1个回答
-1
投票

© www.soinside.com 2019 - 2024. All rights reserved.