In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly shows you "what is the use of Fetch in Hibernate". It is easy to understand and clear. I hope it can help you solve your doubts. Let me lead you to study and learn this article "what is the use of Fetch in Hibernate?"
Now more and more found that it is not easy to master Hibernate Fetch, Spring is actually much easier to use, but it really takes a certain amount of time to accumulate when using Hibernate, for a project team, if you use Hibernate***, there is a person who is relatively clear about Hibernate, otherwise it will become a risk to the project.
What I want to tell you is that mastering Hibernate Fetch may be much more difficult than you expected. When you easily tell me that Hibernate Fetch is very simple, you should reflect on yourself more. (with one exception, you are a great man.)
Well, there is so much nonsense in one introduction, in fact, today I just want to talk about the function of Hibernate Fetch.
As we all know, the concept of lazy is introduced for the sake of performance in Hibernate. Here we use Parent and Child as models to illustrate.
Public class Parent implements Serializable {/ * identifier field * / private Long id; / * * persistent field * / private List childs; / / skip all getter/setter method} public class Child implements Serializable {/ * * identifier field * / private Long id; / * * persistent field * / private net.foxlog.model.Parent parent; / / skip all getter/setter method}
When we query Parent objects, by default there are only Parent contents and no childs information. If lazy= "false" is set in Parent.hbm.xml, all associated childs contents will be retrieved at the same time.
The question is, what if I want both the default performance of Hibernate and temporary flexibility? This is what Fetch does. We can compare the relationship between fetch and lazy= "true" to programmatic transactions and declarative transactions in transactions, which is not very accurate, but that's what it probably means.
Total value, fetch is to give you an active capture opportunity at the code level.
Parent parent = (Parent) hibernateTemplate.execute (new HibernateCallback () {public Object doInHibernate (Session session) throws HibernateException) SQLException {Query Q = session.createQuery ("from Parent as parent" + "left outer join fetch parent.childs" + "where parent.id =: id") Q.setParameter ("id", new Long (15)); return (Parent) q.uniqueResult ();}}); Assert.assertTrue (parent.getChilds (). Size () > 0)
You can remove the Fetch in the case of lazy= "true" and an exception will be reported. Of course, if lazy= "false" does not need fetch, there is a problem that duplicate records will occur when using Fetch. We can understand that Fetch is actually not for Parent, but for Child. Therefore, directly fetching Parent will have the problem of mismatch.
Refer to the following article, Hibernate collection initialization
Update: some of the above conclusions are wrong. In fact, they are tested under the Hibernate3.2.1 version without duplicate records.
Public void testNPlusOne () throws Exception {List list = (List) hibernateTemplate.execute (new HibernateCallback () {public Object doInHibernate (Session session) throws HibernateException, SQLException {Query Q = session.createQuery ("select distinct p from net.foxlog.model.Parent p inner join fetch p.childs"); return q.list () }}); / / (Parent) (list.get (0)). GetChilds (); System.out.println ("list size =" + list.size ()); for (int item0)
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.