Hibernate optimization Crawl (select crawl strategy through link instant crawl)

Source: Internet
Author: User
Tags configuration settings
Hibernate optimization Crawl (select crawl strategy through link instant crawl) For example: Every time I need item, I need the item's seller (User object). If you can turn it into a statement, you should go to the mapping metadata to enable instant crawl of the seller association, and use the SQL join: <class name= "Item" table= "Item" > ... <many-to-one name= " Seller "class=" User "column=" seller_id "update=" false "fetch=" join "/> </class>  Hibernate now loads the item and its seller in a single SQL statement. For example: Item item = (item) session.get (Item.class,new Long (123));  This action triggers the following SQL Select:select i.*,u.* from Item I left O Uter join USERS u on i.seller_id = u.user_id where i.item_id =?  Obviously, SELLER is no longer loaded on demand, but immediately. Therefore, fetch= "join" disables deferred loading. If you only use lazy= "false" to enable instant crawl, you will see the second select immediately. Load seller in the same select by Fetch= "Join". In the left OUTER join SQL generated by the example above, if item has no vendor, all u.* columns are filled with null. This is why hibernate uses external joins, so it not only gets the item object that has the seller, but it can get all the item objects. If you enable <many-to-one not-null= "true"/>,hibernate an inner join rather than an outer join is performed. -----------can also set up an instant join crawl strategy on the collection: <class name= "Item" table= "Item" > ... <set name= "bids" true "inverse=" Join "> <key column=" item_id "/> <one-to-many class=" Bid "/> </Set> </class>  If you load many Item objects now, such as through Creatcriteria (item.class). List (), the resulting SQL statement looks like this: select I.*,b.* From ITEM I left outer join BID b on i.item_id = b.item_id ----------Finally, you must introduce a global maximum number of hibernat that can be used to control the connected entity associations (not collections) e configuration settings. Consider that you have set all the One-to-many and One-to-many associations mapped to fetch= "join" in the mapping metadata. Suppose the item has a SUCCESSFULBID association, bid has a bidder, and the user has a shippingaddress. If all of these associations are mapped by fetch= "join", how much data will be fetched when you load the item,  how many tables to join. In this case, the number of tables being joined depends on the global hibernate.max_fetch_depth configuration attribute. By default, no restrictions are set, so loading the item also gets bid,user and address in a single query. The reasonable setting is very small, usually between the 1--5. You can even disable join fetching for Many-to-many and one-to-one associations by setting this property to 0. (Note that some database dialects can preset this property, such as Mysqldialect to set it to 2.)

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.