休眠级联 +复合ID问题

发布于 2024-12-10 08:09:21 字数 798 浏览 2 评论 0原文

我目前正在学习Hibernate,我偶然发现了这个问题: 我定义了 3 个实体:用户、模块、权限。 user和module都与Permission存在一对多的关系,因此Permission的复合id由idUser和idModule组成。用户类有一个属性,它是一组 Permission 的属性,并且用 @OneToMany、cascade=CascadeType.ALL 等进行了适当的注释。

现在,我使用 MyEclipse 的逆向工程功能生成了这些类。权限 id 被创建为具有 idUser 和 idModule 属性的单独类。我以为可以创建一个User,给它添加一些新的权限,这样保存用户就会级联操作,权限会自动保存。除了操作导致异常之外,情况都是如此。我运行以下代码:

Permission p = new Permission();
p.setId(new PermissionId(null, module.getId());
user.getPermissions().add(p);
session.save(user);

我遇到的问题是,即使正确生成了 SQL(首先保存用户,然后保存权限),我还是从数据库驱动程序 (Firebird) 收到错误,指出它无法插入idUser 的 null 值,这是正确的,但是 hibernate 不应该将新创建的用户 id 传递给第二个查询吗?

这种特殊的场景对我来说非常违反直觉,因为我倾向于将 null id 传递给 Permission 对象,因为它是新的并且我希望创建它,但另一方面,我必须设置 idModule 属性,因为模块已经存在,所以我不太明白这样的操作应该如何工作。

有谁知道我做错了什么?谢谢

I'm currently learning Hibernate, and I've stumbled into this issue:
I have defined 3 entities: User, Module, Permission. Both user and module have a one-to-many relationship with Permission, so that Permission's composite id consists of idUser and idModule. The user class has a property that is a set of Permission's and it is appropriately annotated with @OneToMany, cascade=CascadeType.ALL, etc.

Now, I generated the classes with MyEclipse's reverse engineering feature. The id of permission was created as a separate class that has an idUser and idModule property. I thought I could create a User, add some new permissions to it, and thus saving the user would cascade the operation, and permissions would be saved automatically. This is true except that the operation causes an exception. I run the following code:

Permission p = new Permission();
p.setId(new PermissionId(null, module.getId());
user.getPermissions().add(p);
session.save(user);

The problem I have is that, even though the SQL is being generated correctly (first saves User, then Permission), I get an error from the database driver (Firebird) which states that it can't insert a null value for idUser, which is true, but shouldn't hibernate be passing the newly created user id to the second query?

This particular scenario feels very counter-intuitive to me since I'm inclined to pass a null id to the Permission object since it is new and I want it to be created, but on other hand, I have to set the idModule property since the module already exists, so I don't really understand how an operation like this is supposed to work.

Does anyone know what I'm doing wrong? Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

芯好空 2024-12-17 08:09:21

您需要指定

顺便说一句,您可能需要考虑对 Permission 对象使用不同的 ID 策略,例如生成的 ID 值 - 如何将 permission 行的主键数据库包含空值?

You need to specify a cascade action for Hibernate to perform when you save a User with an attached transient (meaning not-yet-saved) Permission.

By the way, you might want to consider using a different ID strategy for the Permission object, such as a generated ID value - how can the primary key of the permission row in the database contain a null value?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文