简体   繁体   中英

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.

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.

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. 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 ). Either way, the syntax to access the relationship is the same - p.getChildren() or child.getParent() will work in both cases.

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.  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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