简体   繁体   English

休眠检索父母/孩子

[英]Hibernate retrieving parent/children

So when I have a one-to-many bidirectional relationship, how can I go about making sure I get them from the database as needed? 因此,当我具有一对多的双向关系时,如何确保根据需要从数据库中获取它们?

For example: 例如:

In parent class: 在家长班:

@OneToMany(mappedBy="parent")
List<Child> children;

In child class: 在儿童班:

@ManyToOne
@JoinColumn(name="Parent_Id")
Parent parent;

I'm just not sure how to make sure that when I have the parent, I can get a list of its children or vice versa. 我只是不确定如何确定当我有父母时,我可以获得其孩子的清单,反之亦然。 Not sure how Hibernate handles that. 不确定Hibernate是如何处理的。

If getting children, my first instinct would be: 如果要生孩子,我的第一个直觉是:

String hql="FROM Parent WHERE id=:id";
Query query = session.createQuery(hql);
query.setInteger("id", id);
Parent p = (Parent)query.uniqueResult();
List<Child>=p.getChildren();

and similarly for the opposite: 与此相反:

//insert retrieve child code
Parent p = child.getParent();

My quandary here is that I'm not sure if Hibernate is actually filling in the objects with their parents/children upon creation or not, and if so I'm not sure the most efficient way to retrieve them. 我的困惑是,我不确定Hibernate在创建时是否真的在与父母/孩子一起填充对象,如果这样,我不确定是否最有效的方法来检索它们。

Each side of the relationship can be configured as either eager or lazy. 关系的每一边都可以配置为渴望或懒惰。 If it is eager, Hibernate will load the object immediately when the containing object is loaded. 如果渴望的话,Hibernate将在加载包含对象的对象后立即加载该对象。 If it is lazy, Hibernate will put a placeholder proxy object in, and that proxy object will automatically load the real one the first time you access it (provided that the session/transaction is still open, otherwise it throws a LazyInitializationException ). 如果它是惰性的,则Hibernate将放入一个占位符代理对象,并且该代理对象将在您首次访问它时自动加载真实对象(前提是会话/事务仍处于打开状态,否则将抛出LazyInitializationException )。 Either way, the syntax to access the relationship is the same - p.getChildren() or child.getParent() will work in both cases. 无论哪种方式,访问该关系的语法都是相同的p.getChildren()child.getParent()在两种情况下都可以工作。

Which option is most efficient depends on the details of your individual use case. 哪个选项最有效取决于您个人用例的细节。 In particular, how often you need the relationship relative to how often you load the containing object (lazy lets you skip unnecessary loads), possibly how often a single object is referred to by multiple relationships (depends on implementation, eager might check the database table even when the object's already loaded and in memory), and whether you need to access the relationship after closing the session. 特别是,相对于加载包含对象的频率,您需要多久一次的关系(惰性使您跳过不必要的加载),多个关系可能会引用一个对象的频率(取决于实现,急于检查数据库表) (即使对象已经加载并在内存中),以及在关闭会话后是否需要访问该关系。

All things depend upon your configuration.If you will apply lazy loading then hibernate internally fetch the record from query at the time of getting the record except its identity.
You can see the 
Fetching Strategies
There are four fetching strategies

1. fetch-“join” = Disable the lazy loading, always load all the collections and entities.
2. fetch-“select” (default) = Lazy load all the collections and entities.
3. batch-size=”N” = Fetching up to ‘N’ collections or entities, *Not record*.
4. fetch-“subselect” = Group its collection into a sub select statement.


If you want to surety that what is happening internally in hibernate then Please enable
<!--hibernate.cfg.xml -->
<property name="show_sql">true</property>

after enable you can see all queries in console.  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM