hibernate:父级的子id检索列表

发布于 2024-10-07 19:46:42 字数 1083 浏览 3 评论 0原文

技术:Hibernate 3.0

假设我有

    @Entity
    @Table(name="tbl_companies")  
    public class Company
    {
            @Id
            @Column(name="id") 
            @GeneratedValue(strategy=GenerationType.IDENTITY)
            int id;

            @Column(name="name")
            String companyName;

            @OneToMany(mappedBy = "company")
            List<Employees> empList;

            @OneToMany(mappedBy = "company")
            List<Projects>  projectList;

            @OneToMany(mappedBy = "company")
            List<Department> deptList;

            @OneToMany(mappedBy = "company")
            List<Branch>     branchList;       
    }

实体公司中的实体类公司,它通过 hibernate 注释映射到数据库,包含与其相关的其他实体的列表。由于Branch、Project、Employee等实体的对象本身就是重对象,因此会使Company对象变得非常重,并且包含几乎全部数据库数据。避免这种情况的一种方法是使用延迟加载。另一种方法可以是使用 ListbranchIdList、ListprojectIdList,它们是对象 id 的列表。我的问题是哪种方法是标准做法并且在这种情况下更适合使用。更好的使用包括主要在内存方面的性能、程序员的灵活性等因素(第一个是对程序员来说灵活的,第二个是使用较少的内存)。另一个问题是,如果我使用第二种方法,注释将会发生什么变化。我怀疑 hibernate 是否支持 ids 列表或仅支持完整的对象。

谢谢

Technology: Hibernate 3.0

Suppose i have Entity class Company

    @Entity
    @Table(name="tbl_companies")  
    public class Company
    {
            @Id
            @Column(name="id") 
            @GeneratedValue(strategy=GenerationType.IDENTITY)
            int id;

            @Column(name="name")
            String companyName;

            @OneToMany(mappedBy = "company")
            List<Employees> empList;

            @OneToMany(mappedBy = "company")
            List<Projects>  projectList;

            @OneToMany(mappedBy = "company")
            List<Department> deptList;

            @OneToMany(mappedBy = "company")
            List<Branch>     branchList;       
    }

In Entity Company which is mapped to database by hibernate annotation contains list of of other entities related to it. Since objects of these Entities like Branch, Project, Employee itself are heavy objects, it will make Company object very heavy and contains almost whole of db data. One way to avoid this is to use lazy loading. One other approach can be to use List branchIdList, List projectIdList that is list of ids for objects. My question which approach is standard practice and better to use in such situation. Better to use includes factors like performance in term of memory mainly, flexibility for programmer(first one is one flexible for programmer and second one uses less memory). Another question is if i use second approach what will be change in annotation. I doubt if hibernate supports list of ids or support full fledged objects only.

Thanks

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

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

发布评论

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

评论(2

安穩 2024-10-14 19:46:42

另一种方法是使用列表
branchIdList、列出projectIdList即
是对象的 id 列表。

在这样做之前请仔细考虑。使用 ORM 的全部意义在于,DB 中通过外键相互链接的行可以表示为通过常规 java 引用和集合链接的对象。通过使用您概述的方案,您将失去使用 Hibernate 的大部分优势。

推荐的方法是使用延迟加载。

更好使用包括以下因素
主要表现在内存方面,
程序员的灵活性(第一个
对于程序员来说是一种灵活的方式
第二个使用较少的内存)。

恕我直言,您获得的内存(如果有的话)不值得程序员所承受的痛苦。

One other approach can be to use List
branchIdList, List projectIdList that
is list of ids for objects.

Please think very carefully before doing this. The whole point of using an ORM is so that rows in DB that are linked to each other via foreign keys can be represented as objects linked via regular java references and collections. By using the scheme you outlined, you will loose most of the advantages of using Hibernate.

The recommended approach is to using lazy loading.

Better to use includes factors like
performance in term of memory mainly,
flexibility for programmer(first one
is one flexible for programmer and
second one uses less memory).

IMHO, the memory you gain, if any, is not worth the programmer pain it causes.

Spring初心 2024-10-14 19:46:42

我认为,你必须先看看你的
检索公司的用例
目的。意味着例如有
员工所处的场景太多
使用 Company 对象检索。那里
项目较少的场景
使用 Company 对象检索。所以,
按照这些,您可以删除
公司对象中的项目列表和
从项目中使其成为多对一
对象(手动检索)。所以,
分析你所有的场景并做出
有些关系是多对一的。做其他
列出懒惰的列表。

I think, you have to first see your
use cases for retrieving Company
object. Means for example, there are
too many scenarios where Employees is
retrieved with Company object. There
are less scenarios where Projects is
retrieved with Company object. So,
following these, you can remove
Projects list from Company object and
make it many-to-one from Project
object (retrieve it manually). So,
analyse all your scenarios and make
some relations many-to-one. Make other
list lazy.

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