1+n problems in Hibernate and how to solve them

Source: Internet
Author: User

1.1+n Problem description in Hibernate

In a many-to-one relationship, when we need to query the records of a table of more than one party, we can complete the operation with an SQL statement. However The default value of fetch for the @manytoone callout in a multi-party entity class is Fetchtype.eager, at which point, hibernate emits an n (multiparty record number) SQL statement in addition to the SQL statement that records the table of the one party that queries the many, which is 1+ N problem. such as: BBS's Plate (Category), subject (topic), Reply (msg). A plate has multiple themes, and a theme belongs to a plate, then category and topic belong to a one-to-many relationship, set @manytoone in topic. When all topics need to be removed, only a single statement of SELECT * from topic can be issued. However, hibernate queries the category corresponding to each topic, so 1+n SQL statements are issued.

2. Solutions to 1+n problems

① set the Fetch property value of @manytoone to Fetchtype.lazy, which is resolved after the following N SQL statements are sent on demand. However, one drawback is that cascading objects cannot be obtained if a cascading query is required.

② sets @batchsize (size=5), which is added to the class, and @entity in the same location, so that the issued SQL statements are reduced. This setting improves efficiency to a certain extent.

③ uses join fetch in the HQP statement, which is actually the method used in the criteria. This is also the most commonly used method;

  1. @Test
  2. //join Fetch
  3. publicvoid test1_n3() {
  4. Session session=sf.getcurrentsession ();
  5. Session.begintransaction ();
  6. //list<topic> topics= (list<topic>) Session.createcriteria (Topic.class). List ();//Only one query statement, The criteria method is this way
  7. List<topic> topics= (list<topic>) session.createquery ("from Topic T left join fetch t.category C"). List ( );
  8. For (Topic t:topics) {
  9. System.out.println (T.getid () +"----" +t.gettitle ());
  10. System.out.println (T.getcategory (). GetName ());
  11. }
  12. Session.gettransaction (). commit ();
  13. }

This is the 1+n problem in hibernate.

1+n problems in Hibernate and how to solve them

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.