在 Spring-Hibernate 项目中初始化实体集合(POJO)的正确方法是什么?
我有一个 POJO 类,比如 Foo,它有一组其他实体实例,比如 bar。 此外,此类项目还有标准杂项类:Foo 和 Bar 的 service 和 dao。
我希望 BarService 获取与某些 Foo 关联的 Bar 实例集。现在我有以下代码,我认为它在概念上是不好的。
public class Foo {
Set<Bar> bars;
public Set<Bar> getBars() {
if (bars == null)
return ( bars = new HashSet() );
return bars;
}
}
public class BarServiceImpl {
public List<Bar> getListOfBars(Foo foo) {
return new ArrayList(foo.getBars());
}
}
3个问题: Foo的Set在哪里初始化比较好? 哪些特定的集合和列表更适合此类目的? 我当前的实施存在哪些概念性问题,以及如何做得更好?
提前致谢。
I have a POJO class, say Foo, which has a Set of other entity instances, say bars.
Also there are standart misc classes for such project: service and dao for both Foo and Bar.
I want BarService to get the Set of Bar instances associated with some Foo. Now I have the following code, wich I believe is conceptually bad.
public class Foo {
Set<Bar> bars;
public Set<Bar> getBars() {
if (bars == null)
return ( bars = new HashSet() );
return bars;
}
}
public class BarServiceImpl {
public List<Bar> getListOfBars(Foo foo) {
return new ArrayList(foo.getBars());
}
}
3 questions:
Where it is better to initialize Foo's Set?
What specific Sets and Lists are better for such purposes?
What conceptual issues has my current implementation, and how to do better?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
大多数时候,我在声明集合时初始化它,这是 Hibernate 所推荐的。引用文档:
如果将其保留为 null 是您业务的一部分,我的建议是在(通用)链接管理方法中对其进行初始化:
这一切都取决于您需要的语义。
Set
不允许重复,List
允许重复并引入位置索引。null
,则让它为null
。foo.getBars()
?Most of time, I initialize a collections when declaring it, which is what Hibernate recommends. Quoting the documentation:
If leaving it
null
is part of your business, my suggestion would be to initialize it in a (common) link management methods:It all depends on the semantics you need. A
Set
doesn't allow duplicates, aList
allows duplicates and introduces positional indexing.null
at that point, let it benull
.foo.getBars()
?您的实体可以是带有 getter 和 setter 的简单字段集。您需要注意的是如何关联对象以及填充对象所采取的方法。
ORM API 提供了使用对象而不是 SQL 的自由。您应该小心选择何时初始化对象的字段。例如,如果您有一个对象人员,其中包含姓名、年龄、联系人集合和访问过的城市。如果您对人员的姓名和年龄感兴趣,而不是联系方式和城市,则应仅加载姓名和年龄。这意味着联系人和城市应该延迟加载。
当对联系人感兴趣时,您仅加载联系人,而不加载整个人员对象或通过人员对象。您可能希望仅使用 Dao/Service 并显式定义方法来加载联系人集来加载对象的特定方面(使用反向关联)。
一些最佳的休眠实践可以在 最佳实践。
更新:
1) 实体不会自行填充。一种流行的方法是让 DAO 来完成这项工作。您的实体可以简单地是
2)您可以在另一个层(也称为服务层)中管理事务。
Your entity could be simple set of fields with getters and setters. What you need to take care of is how you relate your objects and approach you take to populate objects.
ORM APIs gives liberty of using Objects rather than SQL. You should be careful as to when you should choose to initialize the fields of an Object. For example if you have an object person, which comprises of name, age, collection of contacts and cities visited. In a situation where you are interested in name and age of the person and not contact and cities, you should load name and age only. Which implies that contacts and cities should be lazy loaded.
When interested in contact you load only contacts and not the entire person object or through person object. You would want to load Set of contacts only using Dao/Service and explicitly defining methods to load specific aspect of the object (use of reverse association).
Some best hibernate practices can be found at Best Practices.
Updated:
1) Entity does not populate on its own. One of the popular approach is to have DAO to do this Job. Your Entity could simple be
2) You can have transaction managed in another layer also referred as Service layer.
我倾向于在服务层中初始化集合,并在其中保留事务处理。因此,我可以在 BaseDAO 中有一个方法,它允许我使用反射来初始化项目中任何实体的任何集合,方法是将集合名称传递到要急切获取(初始化)的方法中:
然后您可以从使用此方法的服务层:
更详细的示例:
http://objecthunter.congrace.de/tinybo/blog/articles/69
i tend to initialize the collections in the service layer where i keep the transaction handling as well. So i can have a method in my BaseDAO which lets me initialize any collection of any Entity in my projects using reflection, by passing the collection names into the method which are to be fetched eagerly (initialized):
then you can initialize any collection from the service layer using this method:
A more detailed example:
http://objecthunter.congrace.de/tinybo/blog/articles/69