hibernate:父级的子id检索列表
技术: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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在这样做之前请仔细考虑。使用 ORM 的全部意义在于,DB 中通过外键相互链接的行可以表示为通过常规 java 引用和集合链接的对象。通过使用您概述的方案,您将失去使用 Hibernate 的大部分优势。
推荐的方法是使用延迟加载。
恕我直言,您获得的内存(如果有的话)不值得程序员所承受的痛苦。
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.
IMHO, the memory you gain, if any, is not worth the programmer pain it causes.