Hibernate - 使用每个子类的表 - 如何将现有的超类对象链接到子类对象

发布于 2024-08-19 23:50:28 字数 244 浏览 3 评论 0原文

我有一个用户休眠类、职员类和消费者类。所有这些都映射到数据库中自己的表。用户 PK 也充当店员和消费者的 PK。

所以现在我的问题是,如果一个用户最初是一名职员,他在用户表和职员表中有一条记录。如果该用户想成为消费者,我想将该用户的记录链接到新消费者的记录。因此,即使我将 userId 传递给消费者的记录,它也会将其视为要持久保存的新 User 并给出重复键异常。我如何告诉 Hibernate 将同一个用户对象与这个新的 Consumer 对象链接起来?

I have a User hibernate class, Clerk class and Consumer class. All these maps to their own tables in database. The User PK also acts as Clerk's and Consumer's PK.

So now my problem is that if a user is initially a Clerk, he has a record in Users table and Clerks table. If that user wants to become a consumer, I want to link that User's record to the new Consumer's record. So even if I pass the userId to the consumer's record, it treats it as a new User to be persisted and gives a duplicate_key exception. How do I tell Hibernate to link the same user object with this new Consumer object?

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

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

发布评论

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

评论(3

星光不落少年眉 2024-08-26 23:50:28

不,你不能。您正在尝试实现多重继承。在不支持多重继承的语言中,用户不能同时是职员和消费者。如果这是一个有效的场景,那么您应该使用组合而不是继承进行映射,以便您的 User 类同时具有 Clerk 和 Consumer 子对象,其中一个或两个都可以为 null。

No, you can't. You are trying to implement multiple inheritance. A User cannot be both a Clerk and a Consumer in a language that does not support multiple inheritance. If this is a valid scenario, then you should map using composition rather than inheritance, so that your User class has both Clerk and Consumer child objects, either or both of which could be null.

酒绊 2024-08-26 23:50:28

根据您当前的实现,我认为您必须在复制所需的属性后删除实际的 Clerk 并保留新的 Consumer (这在我看来是有意义的)因为您不能将 Clerk 转换为 Consumer,反之亦然)。如果您为您的用户使用功能键(正如您应该的那样),这应该不是问题。

With your current implementation, I think that you'll have to delete the actual Clerk and to persist a new Consumer, after having copied the desired attributes (and this makes sense IMO as you can't cast a Clerk into a Consumer and vice versa). If you use a functional key for your User (as you should), this shouldn't be a problem.

南…巷孤猫 2024-08-26 23:50:28

根据我的经验,最好的方法是首先考虑对象和类,然后看看 hibernate 从中生成什么。

当用java来描述你时,你有多重继承(只对接口起作用一点)和变异类型,即你的用户首先是一个职员,后来是一个消费者。这样行不通。

请考虑以下模型:

用户具有角色类到角色(子类)的映射。店员和消费者是角色。当您让角色具有对用户的反向引用时,用户可以作为委托来提供每个用户拥有的属性和方法。

在代码中,这看起来类似于:
类用户{
地图、角色>角色;

<T extends Role> T as(<Class<T>> roleClass>){
    return (T) roles.get(roleClass);
}

应该漂亮、干净、灵活。并且很容易使用 hibernate 映射到数据库模式。

In my experience the best approach is to think of objects and classes first, and then see what hibernate generates out of that.

When stating your in terms of java you have multiple inheritance (which only works a little with interfaces) and mutating type, i.e. your User is first a clerk an later a consumer. Doesn't work this way.

Consider this model instead:

a User has a map of Role-classes to (subclasses of)Roles. Clerk and Consumer are roles. When you let a role have a backreference to the user, the user can work as a delegate to provide the attributes and methods every user has.

In code this would look similar to this:
class User {
Map,Role> roles;

<T extends Role> T as(<Class<T>> roleClass>){
    return (T) roles.get(roleClass);
}

Should be nice, clean and flexible. And pretty easy to map to a database schema with hibernate.

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