English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The database structure is as follows
The 'strategy' table has foreign keys 'member_id' (associated with the 'member' table) and 'strategy_category' (associated with the 'category' table), while the 'member' table has a foreign key 'position_id' (associated with the 'positions' table)
If the front-end page directly queries the content of the stategy table, our HQL statement would be written like this
Stringhql="FromStrategywhereid=:id";
The console will report a nosession error because hibernate defaults to lazy loading, which will only load associated objects when needed. In our front-end, when we need to take the properties of the associated objects, the session has already been closed, and such an error will be reported.
So how to solve it
It is recommended to use the leftjoinfetch method to load objects instead of changing the lazy loading by default in the annotation, as the efficiency will be very low
The statement is as follows
Strategystrategy=(Strategy)sessionFactoryUtil.getSession() .createQuery("FromStrategysleftjoinfetchs.strategyCategoryleftjoinfetchs.memberleftjoinfetchs.member.positionswheres.id=:id") .setInteger("id",id).uniqueResult();
It should be noted here that because the member table associated with the member table also associates with the positions table, it is necessary to load the other table together. Another thing to note is that the s.id must be written like this because the primary key name of each table is id, and if it is not specified, the system cannot identify it.
Summary
That's all about the content of this article on the discussion of the urgent loading problem of hibernate (multiple foreign key associations). I hope it will be helpful to everyone. Those who are interested can continue to read other related topics on this site. If there are any shortcomings, please leave a message to point them out. Thank you for the support of friends to this site!
Declaration: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been edited by humans, and does not assume any relevant legal responsibility. If you find any content suspected of copyright infringement, please send an email to: notice#w3Please replace '#' with '@' when sending an email to report violations, and provide relevant evidence. Once verified, this site will immediately delete the content suspected of infringement.