使用 JPA2 进行实体关系设计

发布于 2024-12-06 12:20:35 字数 408 浏览 0 评论 0原文

我有两个实体,用户和用户设置。这两者之间的明显关系将用户作为包含一组/列表的用户设置的一流实体,因此当加载用户时,设置也会被加载。例如,用户 1-->* UserSetting

问题是这不是我想要的。目前,用户只有一些设置,但情况并非总是如此,当用户在系统中处于活动状态时,他们通常只需要访问所有设置的一小部分。我想要的是按需加载个人用户设置。显而易见的选择是使 UserSetting 列表延迟加载,但这不起作用,因为我想在分离状态下使用用户。

我当前的“解决方案”是将 User 包含在 UserSetting 对象中,但这感觉不对,因为它使关系 UserSetting *-->1 User 感觉像 UserSetting 是主导实体。这是最好的解决方案吗?

假设我当前的解决方案是我能得到的最好的解决方案,删除用户仍然可以正确级联吗?

I have two entities, User and UserSetting. The obvious relationship between these two has User as the first rate entity which contains a set / list of UserSettings so when a User is loaded the settings get loaded too. E.g User 1-->* UserSetting

Trouble is that's not what I want. At the moment users only have a few settings but that won't always be the case and when a user is active in the system they typically only need access to a small subset of all their settings. What I want is to load individual user settings on demand. The obvious choice is to make the UserSetting list lazy load but that won't work as I want to use the User in a detached state.

My current "solution" is to include the User in the UserSetting object but that feels wrong as it makes the relationship UserSetting *-->1 User which feels like the UserSetting is the dominant entity. Is this the best solution?

Assuming my current solution is the best I can get will a delete of the User still cascade correctly?

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

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

发布评论

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

评论(1

空心↖ 2024-12-13 12:20:35

这里有两点

  1. 首先,如果您的 User 实体与 UserSettings 有关联,并且该关联可以包含很多并不总是需要的成员,那么正确的做法是将其默认配置为lazyLoaded(通过 JPA2映射配置),然后仅在需要时强制对其进行急切获取(即在您提到的需要分离用户实体上的关联值的情况下)。看看“join fetch”看看如何做到这一点,它应该是这样的:

    SELECT u FROM User u JOIN FETCH u.userSettings

  2. 如果只有经常需要的 UserSettings 的子集,您可以进行两个关联在 User 实体中,例如 userSettingsMain 和 userSettingsExtra,默认情况下都是延迟加载的,只需在某个分离的 User 实体上 join 获取您需要的实体。或者,作为更高级的事情,您可以在用户设置上进行映射关联,并为重要的 UserSetting 和额外的设置使用不同的键(例如 i1,i2,... e1,e2 等),然后急切地仅获取所需的键集,但这仅适用于(至少目前)EclipseLink JPA2,Hibernate 的实现只会在 Map 关联上引发一个大异常(请参阅此处的问题:Hibernate JPQL - 在地图关联中查询 KEY() 错误如果

There's 2 points here

  1. First, if your User entity has an association towards UserSettings and that association can contain a lot of members which are not needed all the time, the right thing to do is configure it as lazyLoaded by default (via JPA2 mapping config) and then force an eager fetch on it only when you need to (i.e. in those situations you mentioned where you need the values of that assocation on a detached User Entity). Look at "join fetch" to see how to do this, it should be something like:

    SELECT u FROM User u JOIN FETCH u.userSettings

  2. If there's only a subset of those UserSettings that are needed often, you could make two associations in the User entity, such as userSettingsMain and userSettingsExtra, both lazy loaded by default, and just join fetch the one you needed on a certain detached User entity. Or, as a more advanced thing, you can make a Map association on the user settings, and have different keys for the important UserSetting, and the extra ones (such as i1, i2,... e1, e2, etc) and then eagerly fetch only those sets of keys that are needed, but this only works (at least at the moment) with EclipseLink JPA2, Hibernate's implementation just throws a big exception on Map associations (see my question here: Hibernate JPQL - querying for KEY() in Map association error on this)

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