The database structure is as follows
There is a foreign key member_id (associated member table) in strategy, a foreign key strategy_category (associated category table) and a foreign key position_id (associated positons table) in the member table
If the front desk page directly query the contents of the stategy table, our hql statement will be written like this
Stringhql="FromStrategywhereid=:id";
The console will report a nosesession error. This is because hibernate is lazy to load by default. Only when we need to load the associated object will the associated object be loaded. Here, when the session has been closed, the session will be reported.
So how to solve it
Here it is recommended to use leftjoinfetch to load objects instead of changing the default lazy load in the annotation to urgent loading, which will be very efficient.
The statement is as follows
Strategystrategy=(Strategy)sessionFactoryUtil.getSession().createQuery("FromStrategysleftjoinfetchs.strategyCategoryleftjoinfetchs.memberleftjoinfetchs.memberleftjoinfetchs.member.positionswheres.id=:id").setInteger("id",id).uniqueResult();What you need to note here is that because the associated member table is associated with the positions table, it needs to be loaded together. Another thing you need to note is that the s.id here must be written like this because the primary key name of each table here is id. If it is not specified, the system cannot be identified.
Summarize
The above is all about this article discussing the urgent loading problem of hibernate (multiple foreign key association). I hope it will be helpful to everyone. Interested friends can continue to refer to other related topics on this site. If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!