8,hibernate的集合
生活随笔
收集整理的這篇文章主要介紹了
8,hibernate的集合
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
list和set的使用方式差不多, set需要兩個屬性: private Set<Employee> emps; ? <set name="emps">
????????<key column="depart_id" />
????????<one-to-many class="Employee" />
</set> 而list需要增加一個list_index的屬性,標識其加入位置 private List<Employee> emps; <list name="emps">
????????<key column="depart_id" />
<!-- 這一列是Hibernate標識員工加入的位置 -->
????????<list-index column="order_column" />
????????<one-to-many class="Employee" />
</list> 此時,由于數據表中多增加了一列"order_column"來保存排列位置,可以使用bag進行優化. <bag name="emps">
????????<key column="depart_id" />?
????????<one-to-many class="Employee" />
</bag> 注意:bag只能和list類型對于 而對于Map類型,使用如下: private Map<String, Employee> emps; // key=Employee.name
<map name="emps">
????????<key column="depart_id" />
????????<map0key type="string" column="name" />
????????<one-to-many class="Employee" />
</map> 對于Map,需要指定名稱map的key. 使用Hibernate較多的時候,推薦使用Set類型. 如果使用List,當不需要排序時,則使用bag進行映射. 另外,入庫保存時定義的類型,與Hibernate查詢結果的類型不能完全一致. 如Set<Employee> emps = new HashSet<Employee>(); 如果查詢后,進行轉型;HashSet hs = (HashSet) depart.getEmployee(); 會拋出類型轉換的異常,Hibernate返回的類型是:org.hibernate.collection.PersistentSet 即,對于Hibernate,定義屬性是,其類型必須是接口. 如:private Set<Employee> emps; 而不能定義成:private HashSet<Employee> emps;
????????<key column="depart_id" />
????????<one-to-many class="Employee" />
</set> 而list需要增加一個list_index的屬性,標識其加入位置 private List<Employee> emps; <list name="emps">
????????<key column="depart_id" />
<!-- 這一列是Hibernate標識員工加入的位置 -->
????????<list-index column="order_column" />
????????<one-to-many class="Employee" />
</list> 此時,由于數據表中多增加了一列"order_column"來保存排列位置,可以使用bag進行優化. <bag name="emps">
????????<key column="depart_id" />?
????????<one-to-many class="Employee" />
</bag> 注意:bag只能和list類型對于 而對于Map類型,使用如下: private Map<String, Employee> emps; // key=Employee.name
<map name="emps">
????????<key column="depart_id" />
????????<map0key type="string" column="name" />
????????<one-to-many class="Employee" />
</map> 對于Map,需要指定名稱map的key. 使用Hibernate較多的時候,推薦使用Set類型. 如果使用List,當不需要排序時,則使用bag進行映射. 另外,入庫保存時定義的類型,與Hibernate查詢結果的類型不能完全一致. 如Set<Employee> emps = new HashSet<Employee>(); 如果查詢后,進行轉型;HashSet hs = (HashSet) depart.getEmployee(); 會拋出類型轉換的異常,Hibernate返回的類型是:org.hibernate.collection.PersistentSet 即,對于Hibernate,定義屬性是,其類型必須是接口. 如:private Set<Employee> emps; 而不能定義成:private HashSet<Employee> emps;
轉載于:https://blog.51cto.com/xiaosa/131650
總結
以上是生活随笔為你收集整理的8,hibernate的集合的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 关于编程学习及面试,推荐些适合的网站,希
- 下一篇: 万年自学党聊聊如何选择编程学习资源?