日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

hibernate 懒加载_Hibernate懒/急加载示例

發布時間:2023/12/3 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 hibernate 懒加载_Hibernate懒/急加载示例 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

hibernate 懶加載

這篇文章將重點討論為什么以及如何在應用程序中使用稱為LAZY和EAGER加載的概念,以及如何使用Spring的Hibernate模板以EAGER方式加載LAZY實體。

當然,正如標題本身所暗示的那樣,我們將通過一個示例來說明這一點。 場景就是這樣;

您是一個有很多玩具的孩子的父母。 但是當前的問題是,只要您打電話給他(我們假設您有一個男孩),他也會帶著所有玩具來找您。 現在這是一個問題,因為您不希望他一直都隨身攜帶玩具。

因此,作為理論上的父母,您會繼續前進,并將孩子的玩具定義為LAZY。 現在,每當您打電話給他時,他都會帶著他的玩具來到您身邊。

但是您面臨另一個問題。 當需要進行家庭旅行時,您希望他帶上他的玩具,因為否則孩子會厭倦這次旅行。 但是由于您對孩子的玩具嚴格執行LAZY,因此您無法要求他隨身攜帶玩具。 這是EAGER提取起作用的地方。 首先讓我們看看我們的領域類。

package com.fetchsample.example.domain;import java.util.HashSet; import java.util.Set;import javax.persistence.CascadeType; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.NamedQuery; import javax.persistence.OneToMany; import javax.persistence.Table;/*** Holds information about the child* * @author dinuka.arseculeratne* */ @Entity @Table(name = 'CHILD') @NamedQuery(name = 'findChildByName', query = 'select DISTINCT(chd) from Child chd left join fetch chd.toyList where chd.childName=:chdName') public class Child {public static interface Constants {public static final String FIND_CHILD_BY_NAME_QUERY = 'findChildByName';public static final String CHILD_NAME_PARAM = 'chdName';}@Id@GeneratedValue(strategy = GenerationType.AUTO)/*** The primary key of the CHILD table*/private Long childId;@Column(name = 'CHILD_NAME')/*** The name of the child*/private String childName;@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)/*** The toys the child has. We do not want the child to have the same toy more than* once, so we have used a set here.*/private Set<Toy> toyList = new HashSet<Toy>();public Long getChildId() {return childId;}public void setChildId(Long childId) {this.childId = childId;}public String getChildName() {return childName;}public void setChildName(String childName) {this.childName = childName;}public Set<Toy> getToyList() {return toyList;}public void addToy(Toy toy) {toyList.add(toy);}}package com.fetchsample.example.domain;import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table;/*** Hols data related to the toys a child possess* * @author dinuka.arseculeratne* */ @Entity @Table(name = 'TOYS') public class Toy {@Id@GeneratedValue(strategy = GenerationType.AUTO)@Column(name = 'TOY_ID')/*** The primary key of the TOYS table*/private Long toyId;@Column(name = 'TOY_NAME')/*** The name of the toy*/private String toyName;public Long getToyId() {return toyId;}public void setToyId(Long toyId) {this.toyId = toyId;}public String getToyName() {return toyName;}public void setToyName(String toyName) {this.toyName = toyName;}@Overridepublic int hashCode() {final int prime = 31;int result = 1;result = prime * result + ((toyName == null) ? 0 : toyName.hashCode());return result;}@Override/*** Overriden because within the child class we use a Set to* hold all unique toys*/public boolean equals(Object obj) {if (this == obj)return true;if (obj == null)return false;if (getClass() != obj.getClass())return false;Toy other = (Toy) obj;if (toyName == null) {if (other.toyName != null)return false;} else if (!toyName.equals(other.toyName))return false;return true;}@Overridepublic String toString() {return 'Toy [toyId=' + toyId + ', toyName=' + toyName + ']';}}

如您所見,我們有兩個簡單的實體分別代表孩子和玩具。 這個孩子與玩具有一對多的關系,這意味著一個孩子可以擁有很多玩具(哦,我多么想念童年的日子)。 之后,我們需要與數據進行交互,因此讓我們繼續定義DAO(數據訪問對象)接口和實現。

package com.fetchsample.example.dao;import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional;import com.fetchsample.example.domain.Child;/*** The basic contract for dealing with the {@link Child} entity* * @author dinuka.arseculeratne* */ @Transactional(propagation = Propagation.REQUIRED, readOnly = false) public interface ChildDAO {/*** This method will create a new instance of a child in the child table* * @param child* the entity to be persisted*/public void persistChild(Child child);/*** Retrieves a child without his/her toys* * @param childId* the primary key of the child table* @return the child with the ID passed in if found*/public Child getChildByIdWithoutToys(Long childId);/*** Retrieves the child with his/her toys* * @param childId* the primary key of the child table* @return the child with the ID passed in if found*/public Child getChildByIdWithToys(Long childId);/*** Retrieves the child by the name and with his/her toys* * @param childName* the name of the child* @return the child entity that matches the name passed in*/public Child getChildByNameWithToys(String childName);}package com.fetchsample.example.dao.hibernate;import org.springframework.orm.hibernate3.support.HibernateDaoSupport;import com.fetchsample.example.dao.ChildDAO; import com.fetchsample.example.domain.Child;/*** The hibernate implementation of our {@link ChildDAO} interface* * @author dinuka.arseculeratne* */ public class ChildDAOHibernateImpl extends HibernateDaoSupport implementsChildDAO {/*** {@inheritDoc}*/public void persistChild(Child child) {getHibernateTemplate().persist(child);}/*** {@inheritDoc}*/public Child getChildByIdWithoutToys(Long childId) {return getHibernateTemplate().get(Child.class, childId);}/*** {@inheritDoc}*/public Child getChildByIdWithToys(Long childId) {Child child = getChildByIdWithoutToys(childId);/*** Since by default the toys are not loaded, we call the hibernate* template's initialize method to populate the toys list of that* respective child.*/getHibernateTemplate().initialize(child.getToyList());return child;}/*** {@inheritDoc}*/public Child getChildByNameWithToys(String childName) {return (Child) getHibernateTemplate().findByNamedQueryAndNamedParam(Child.Constants.FIND_CHILD_BY_NAME_QUERY,Child.Constants.CHILD_NAME_PARAM, childName).get(0);}}

一個簡單的合同。 我有四種主要方法。 當然,第一個只是將子實體持久化到數據庫中。

第二種方法通過傳入的主鍵來檢索Child,但不會獲取玩具。

第三種方法首先獲取Child,然后使用Hibernate模板的initialize()方法檢索Child的玩具。 請注意,當您調用initialize()方法時,hibernate會將您LAZY定義的集合獲取到您檢索到的Child代理中。

最終方法還可以檢索“兒童”的玩具,但這一次使用命名查詢。 如果返回到Child實體的Named查詢,您會看到我們使用了“ left join fetch ”。 當返回有資格的Child實體時,實際上是關鍵字fetch也會初始化玩具集合。 最后,我有我的主班來測試我們的功能;

package com.fetchsample.example;import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;import com.fetchsample.example.dao.ChildDAO; import com.fetchsample.example.domain.Child; import com.fetchsample.example.domain.Toy;/*** A test class* * @author dinuka.arseculeratne* */ public class ChildTest {public static void main(String[] args) {ApplicationContext context = new ClassPathXmlApplicationContext('com/fetchsample/example/spring-context.xml');/*** First we initialize a child*/Child child = new Child();/*** A cool ben 10 action figure*/Toy ben10 = new Toy();ben10.setToyName('Ben 10 figure');/*** A even more cooler spider man action figure*/Toy spiderMan = new Toy();spiderMan.setToyName('Spider man figure');child.setChildName('John');/*** Add the toys to the collection*/child.addToy(ben10);child.addToy(spiderMan);ChildDAO childDAO = (ChildDAO) context.getBean('childDAO');childDAO.persistChild(child);Child childWithoutToys = childDAO.getChildByIdWithoutToys(1L);// The following line will throw a lazy initialization error since we have// defined fetch type as LAZY in the Child domain class.// System.out.println(childWithToys.getToyList().size());Child childWithToys = childDAO.getChildByIdWithToys(1L);System.out.println(childWithToys.getToyList().size());Child childByNameWithToys = childDAO.getChildByNameWithToys('John');System.out.println(childByNameWithToys.getToyList().size());} }

將基礎實體定義為LAZY是一種好習慣,因為在很多情況下,您可能不希望實體中的集合,而只想與基礎實體中的數據進行交互。 但是,如果需要集合的數據,則可以使用前面提到的任何一種方法。

今天就是這樣。 對于任何想嘗試該示例的人,我都在這里上傳了該示例。

參考: “ 我的旅程” IT博客中的JCG合作伙伴 Dinuka Arseculeratne 舉例說明了使用Hibernate模式進行延遲/延遲加載 。


翻譯自: https://www.javacodegeeks.com/2012/08/hibernate-lazyeager-loading-example.html

hibernate 懶加載

總結

以上是生活随笔為你收集整理的hibernate 懒加载_Hibernate懒/急加载示例的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 日本波多野结衣在线 | 色婷婷在线播放 | 亚洲精品91在线 | 日韩精品一区二区三区视频 | av丝袜天堂 | 高清欧美性猛交xxxx | 亚洲av色一区二区三区精品 | 国产成人免费网站 | 欧美xxx性 | 国产午夜精品一区二区 | 视频一区欧美 | 国产精品国产自产拍高清av | 亚洲成人激情小说 | 好吊视频一区二区三区四区 | 女性生殖扒开酷刑vk | 成人免费观看视频 | 熟女毛片 | 久九九| 裸体毛片 | 成人一区二区三区在线 | 久久精品a亚洲国产v高清不卡 | 无码人妻丰满熟妇区96 | 99久久久无码国产精品 | 久久久久免费看 | 瑟瑟视频在线观看 | 折磨小男生性器羞耻的故事 | 国产又粗又长又大 | 国产激情av | 一区二区三区欧美日韩 | 欧美另类tv | av网站免费观看 | 久久久网站 | 国产香蕉视频在线观看 | 午夜亚洲AV永久无码精品蜜芽 | jizz视频 | 视频一区二区三区在线观看 | a极黄色片| 手机看片日韩福利 | 国产三级自拍视频 | 五月丁香花 | 国产激情a | xxxx日本黄色 | 午夜国产小视频 | 奇米影视第四色777 波多野结衣一区二区三区免费视频 | 亚洲精品成人av | 黄瓜视频在线观看 | 九九自拍视频 | 精品亚洲乱码一区二区 | 大色av| 国产 日韩 欧美在线 | 国产精品天天操 | 日本少妇激情舌吻 | 中文字幕123区 | 女人17片毛片60分钟 | 国产第四页 | jzz在线观看 | 日韩wwww | 在线中文字日产幕 | 黑帮大佬和我的365日第二部 | 亚洲性猛交 | 中文字幕在线亚洲 | www久久| 麻豆www| 久在线观看视频 | 久久国产精品免费 | 免费看裸体网站视频 | 极品少妇xxx | 俺来也av | 久久精品视频网站 | 三级视频网站在线观看 | 青青草十七色 | 国产麻豆精品视频 | 亚洲三级小说 | 精品国产大片大片大片 | 国产a级精品 | 色偷偷网站 | 天天爽视频 | 精品久久91| 中文字幕一区二区人妻视频 | 国产精品自拍小视频 | 在线观看的av网站 | 成人视频在线观看 | 四虎色播 | 一区二区精品视频在线观看 | 最近免费中文字幕大全免费版视频 | 国产探花精品在线 | 青青久视频 | 婷婷深爱激情 | 国产又爽又黄又嫩又猛又粗 | 男女性杂交内射妇女bbwxz | 国产激情av在线 | 天堂成人在线视频 | 免费黄色小视频在线观看 | 国产精品日韩在线观看 | 97无码精品人妻 | 日本三级理论片 | 成人免费看片&#39; | 黄色大片毛片 | 久久久久亚洲av无码网站 |