创建实体时添加静态属性数据

发布于 2024-10-03 08:22:14 字数 1420 浏览 1 评论 0原文

假设我有两种实体类型:用户和国家/地区。

国家/地区实体永远不会被创建,也不可变。它们由 ISO alpha3 国家/地区代码键入,例如“USA”。它们驻留在具有 PK 列 ID 的表 COUNTRY 中。

用户与国家/地区有多对一映射,且级联 = 无。用户驻留在 USER 表中,该表的 FK 列 COUNTRY_ID 引用了 COUNTRY(ID)。

创建新用户时,我可能会执行以下操作:

User user = new User();
user.setCountry(em.find(Country.class, "USA"));
em.persist(user);

不过,我认为这很浪费,因为它需要首先针对 COUNTRY 进行查询。所以我注意到我也可以这样做:

Country usa = new Country();
usa.setId("USA");
User user = new User();
user.setCountry(usa);
em.persist(user);

由于cascade =“none”,这不需要查询COUNTRY表;它应该直接将“USA”作为 COUNTRY_ID 插入到 USER 中。这是正确的吗?

现在假设有一些代码只创建 COUNTRY_ID =“USA”的用户。在这种情况下,我想到存储 ID =“USA”的 Country 静态实例并将其用于每个新用户?例如:

public class UsaUserFactory implements Factory<User> {

    private static final Country USA = new Country();
    static { USA.setId("USA"); }

    public User newInstance() {
        User user = new User();
        user.setCountry(USA);
        return user;
    }
}

public SomeOtherClass {

    public void persistUser(EntityManager em, Factory<User> uf, ...) {
        User user = uf.newInstance();
        // set some other properties
        em.persist(user);
    }
}

假设从多个线程和多个持久性上下文中同时调用 persistUser()。

我的问题:

  1. 持久的用户实体是否会以任何方式改变我的单例“美国”国家/地区实例?

  2. 由于其他原因,这完全不可取吗?

上面两个类只是为了说明问题;我实际上并没有做那么愚蠢的事情。

Suppose I have two entity types: User and Country.

Country entities are never created and aren't mutable. They're keyed by ISO alpha3 country codes, e.g. "USA". They reside in the table COUNTRY with PK column ID.

Users have a many-to-one mapping to Country w/ cascade = none. Users reside in the USER table with a FK column COUNTRY_ID that references COUNTRY(ID).

When creating a new User I might do something like:

User user = new User();
user.setCountry(em.find(Country.class, "USA"));
em.persist(user);

I gather that's wasteful, though, since it requires a query against COUNTRY first. So I noticed I can also do this:

Country usa = new Country();
usa.setId("USA");
User user = new User();
user.setCountry(usa);
em.persist(user);

Since cascade = "none" this shouldn't need to query the COUNTRY table; it should just insert "USA" directly into USER as COUNTRY_ID. Is that correct?

Now suppose there's some code that only ever creates Users with COUNTRY_ID = "USA". In that case, it occurred to me to store a static instance of Country with ID = "USA" and use it for every new user? For example:

public class UsaUserFactory implements Factory<User> {

    private static final Country USA = new Country();
    static { USA.setId("USA"); }

    public User newInstance() {
        User user = new User();
        user.setCountry(USA);
        return user;
    }
}

public SomeOtherClass {

    public void persistUser(EntityManager em, Factory<User> uf, ...) {
        User user = uf.newInstance();
        // set some other properties
        em.persist(user);
    }
}

Assume persistUser() is called concurrently from multiple threads and from within multiple persistence contexts.

My questions:

  1. Will persisting User entities mutate my singleton "USA" Country instance in any way?

  2. Is this inadvisable for some other reasons entirely?

The two classes above are just to illustrate the question; I'm not actually doing anything quite that silly.

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

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

发布评论

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

评论(1

少年亿悲伤 2024-10-10 08:22:14

我更倾向于尝试使用只读数据的缓存来减少实际的数据库调用。请参阅此处了解帮助设置可能有帮助的缓存。

I would be more inclined to try using a cache for the read-only data to reduce the actual database calls. See here for help setting up a cache that might help.

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