| Hibernate中outer-join與lazy關(guān)鍵字的使用 | |
<property name="total" type="double" update="true" insert="true" access="property" column="Total" </hibernate-mapping> <hibernate-mapping> <property name="description" type="string" update="true" insert="true" access="property" column="Description" not-null="false" unique="false" /> <property name="lineItemPrice" type="double" update="true" insert="true" access="property" </hibernate-mapping>
近來(lái)一直困惑與outer-join關(guān)鍵字,經(jīng)過(guò)多次測試之后,現總結如下:
select * from OrderLineItem o left join Order p on o.OrderId=p.OrderId where o.OrderLineItem_Id=? false:表示不使用外連接抓取關(guān)聯(lián)的內容,當load(OrderLineItem.class,“id“)時(shí),Hibernate生成兩條SQL語(yǔ)句,一條查詢(xún)OrderLineItem表,另一條查詢(xún)Order表。這樣的好處是可以設置延遲加載,此處要將Order類(lèi)設置為lazy=true。 select * from OrderLineItem o where o.OrderLineItem_Id=? auto:具體是ture還是false看hibernate.cfg.xml中的配置
由于集合可以設置lazy=“true“,所以lazy與outer-join不能同時(shí)為true,當lazy=“true“時(shí),outer-join將一直是false,如果lazy=“false“,則outer-join用法與1同 3、HQL語(yǔ)句會(huì )將POJO配置文件中的關(guān)聯(lián)一并查詢(xún),即使在HQL語(yǔ)句中沒(méi)有明確join。 4、In HQL, the “fetch join” clause can be used for per-query specific outer join fetching. One important thing many people miss there, is that HQL queries will ignore the outer-join attribute you specified in your mapping. This makes it possible to configure the default loading behaviour of session.load() and session.get() and of objects loaded by navigating relationship. So if you specify and then do you will still have an additional query and no outer-join. So you must explicily request the outer-join fetching: MyObject obj = session.createQuery( Another important thing to know is that you can only fetch one collection reference in a query. That means you can just use one fetch join. You can however fetch “one” references in addition, as this sample from the Hibernate Docs demonstrates: from eg.Cat as cat We have once considered lifting this limitation, but then decided against it, because using more than one fetch-join would be a bad idea generally: The generated ResultSet becomes huge and is a major performance loss. So alltogether the “fetch join” clause is an important instrument Hibernate users should learn how to leverage, as it allows tuning the fetch behaviour of a certain use case. 5、join fetch 與 join 的區別 如果HQL使用了連接,但是沒(méi)有使用fetch關(guān)鍵字,則生成的SQL語(yǔ)句雖然有連接,但是并沒(méi)有取連接表的數據,還是需要單獨的sql取數據,也就是 select a,b,d...中沒(méi)有連接表的字段 6、如果集合被聲明為lazy=true,在HQL中如果顯式的使用 join fetch 則延遲加載失效。 Powered by: |
聯(lián)系客服