hibernate 高级映射 --张国亮总结第一季
生活随笔
收集整理的這篇文章主要介紹了
hibernate 高级映射 --张国亮总结第一季
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
在做持久化類的時候的規定:
1).有一個默認的構造方法;
2).所有的屬性都有setter和getter方法
3).有一個對象標識符Oid;
4).如果有集合屬性,則必須定義成接口類型:List、Set、Map。不能定義成類類型HashSet、hashMap、ArrayList等
1. Hibernate 高級映射
<set>元素:可以映射java.util.Set接口的屬性,元素沒有順序且不允許重復。
<list>元素:可以映射java.util.List接口的屬性,有順序,需要在集合屬性對應的表中用一個額外的索引保存每個元素的位置。
<bag> <idbag>元素:可以映射java.util.Collection接口的屬性,元素可重復,但不保存順序。
<map>元素:可以映射java.util.Map接口的屬性,元素以鍵/值對的形式保存,也是無序的。
<primitive-array> <array>:可以映射數組元素。
2. 這幾個高級映射的主要代碼
<set>元素private Set<String> hobbies;<set name=“hobbies” table=“student_hobby”><key column=“student_id”/><element type=“string” column=“hobby_name” not-null=“true”/></set>這里要注意的是:這里所說的set標簽不同于前面介紹過的關系映射中的set標簽,不知大家注意到沒有,這里多了一個element標簽但是沒有關系類型的標簽了;element標簽的屬性not-null=”true”是不允許字段為空的意思<list>元素private List<String> hobbies;<list name=“hobbies” table=“student_hobby”><key column=“student_id”/><list-index column=“posistion”/><element type=“string” column=“hobby_name” not-null=“true”/></list>這里要注意的是:list標簽與set的標簽的映射文件寫法看似一樣,其實不一樣,有一點不同的是,list標簽多了list-index標簽,這個標簽的作用是給表添加一個索引位置,為什么會有多一個這個標簽呢!?其實是由list與set的不同決定的,li。st是 有順序的所以要有一個索引位置來唯一標識,而set沒有。<bag>元素private Collection<String> hobbies;<bag name=“hobbies” table=“student_hobby”><key column=“student_id”/><element type=“string” column=“hobby_name” not-null=“true”/></bag>這里要注意的是:bag標簽的作用是達到與list標簽同樣的功能,但是不多余的產生索引位置的標識。bag標簽對應的持久化類中的屬性類型是Collection集合類型,即是set、list的父接口,固后者達到的功能前者都能達到。bag主要作用是:想使數據又順序,但是又不想要那個產生的標識位置的字段。<map>元素private Map<Long String> hobbies;<map name=“hobbies” table=“student_hobby”><key column=“student_id”><map-key column=“hobby_id” type=“long”/><element type=“string” column=“hobby_name” not-null=“true”/></map> 3. 案例分析以上是各個xxx.hbm.xml的部分代碼,也是核心代碼,下面列舉一下測試代碼。SetTest.Javapackage com.hbsi.set;import java.util.HashSet;import java.util.Set;import org.hibernate.Session;import org.junit.Test;import com.hbsi.utils.HibernateSessionFactory;public class SetTest {@Testpublic void add(){Session session = HibernateSessionFactory.getSession();session.beginTransaction();Person p = new Person();p.setName("keven");Set<String> hobbies = new HashSet<String>();hobbies.add("sing");hobbies.add("dance");p.setHobbies(hobbies);session.save(p);session.getTransaction().commit();HibernateSessionFactory.closeSession();}}ListTest.javapackage com.hbsi.list;import java.util.ArrayList;import java.util.List;import org.hibernate.Session;import org.junit.Test;import com.hbsi.utils.HibernateSessionFactory;public class ListTest {@Testpublic void add(){Session session = HibernateSessionFactory.getSession();session.beginTransaction();Person p = new Person();p.setName("join");List<String> hobbies = new ArrayList<String>();hobbies.add("backetball");hobbies.add("football");p.setHobbies(hobbies);session.save(p);session.getTransaction().commit();HibernateSessionFactory.closeSession();}}BagTest.javapackage com.hbsi.bag;import java.util.ArrayList;import java.util.List;import org.hibernate.Session;import org.junit.Test;import com.hbsi.utils.HibernateSessionFactory;public class BagTest {@Testpublic void add(){Session session = HibernateSessionFactory.getSession();session.beginTransaction();Person p = new Person();p.setName("keven");List<String> hobbies = new ArrayList<String>();hobbies.add("sing");hobbies.add("dance");p.setHobbies(hobbies);session.save(p);session.getTransaction().commit();HibernateSessionFactory.closeSession();}}MapTest.javapackage com.hbsi.map;import java.util.HashMap;import java.util.Map;import org.hibernate.Session;import org.junit.Test;import com.hbsi.utils.HibernateSessionFactory;public class MapTest {@Testpublic void add(){Session session = HibernateSessionFactory.getSession();session.beginTransaction();Person p = new Person();p.setName("keven");Map<Integer,String> hobbies = new HashMap<Integer,String>();hobbies.put(1,"sing");hobbies.put(2,"dance");p.setHobbies(hobbies);session.save(p);session.getTransaction().commit();HibernateSessionFactory.closeSession();}}轉載于:https://www.cnblogs.com/zgl521/archive/2012/12/24/3057675.html
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀總結
以上是生活随笔為你收集整理的hibernate 高级映射 --张国亮总结第一季的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: UIImage加阴影
- 下一篇: CakePHP 2.x CookBook