首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 相關知識

Hibernate Map 與 SortedMap 映射全解析:從 XML 配置到自定義比較器

     編輯:Java 相關知識

前言

Hibernate 支持將 Java Map 映射為關係表,Map 鍵值對應映射表的索引列與外鍵。本文詳細講解 無序 Map(HashMap) 與 有序 SortedMap(TreeMap) 兩種映射模式,含 XML 配置、實體類、自定義比較器、CRUD 測試全流程。

一、領域模型

// Employee 一方
public class Employee {
    private Integer id;
    private String firstName, lastName;
    private Integer salary;
    private Map<String, Certificate> certificates; // 鍵:證書類型,值:證書對象
    // Getter/Setter
}

// Certificate 多方
public class Certificate {
    private Integer id;
    private String name; // 證書名稱
    // Getter/Setter
}

二、數據庫表結構

CREATE TABLE EMPLOYEE (
    id INT PRIMARY KEY AUTO_INCREMENT,
    first_name VARCHAR(20), last_name VARCHAR(20), salary INT
);

CREATE TABLE CERTIFICATE (
    id INT PRIMARY KEY AUTO_INCREMENT,
    certificate_type VARCHAR(40),  -- Map 鍵
    certificate_name VARCHAR(30),  -- Certificate.name
    employee_id INT,               -- 外鍵
    FOREIGN KEY (employee_id) REFERENCES EMPLOYEE(id)
);

三、無序 Map 映射(HashMap)

<class name="Employee" table="EMPLOYEE">
    <id name="id" column="id">
        <generator class="native"/>
    </id>
    <map name="certificates" cascade="all">
        <key column="employee_id"/>
        <index column="certificate_type" type="string"/>
        <one-to-many class="Certificate"/>
    </map>
    <property name="firstName" column="first_name"/>
    <property name="lastName" column="last_name"/>
    <property name="salary" column="salary"/>
</class>

<class name="Certificate" table="CERTIFICATE">
    <id name="id" column="id"><generator class="native"/></id>
    <property name="name" column="certificate_name"/>
</class>

關鍵點:

  • <map> 替代 <set>/<list>。
  • <index> 定義 Map 鍵對應的列(certificate_type)。
  • <key> 定義外鍵列(employee_id)。
  • <one-to-many> 聲明值類型。

四、有序 SortedMap 映射(TreeMap + 自定義比較器)

在 <map> 元素添加 sort 屬性:

<map name="certificates" cascade="all" sort="com.example.MyComparator">
    <key column="employee_id"/>
    <index column="certificate_type" type="string"/>
    <one-to-many class="Certificate"/>
</map>

sort 取值:

  • natural:鍵實現 Comparable,按自然序(String 字典序)。
  • 全限定類名:實現 Comparator<K> 的自定義比較器類。

自定義比較器:鍵降序

public class MyComparator implements Comparator<String> {
    @Override
    public int compare(String o1, String o2) {
        // 反轉自然序
        return o2.compareTo(o1);
    }
}

五、實體類調整

// Map 版本
private Map<String, Certificate> certificates = new HashMap<>();

// SortedMap 版本
private SortedMap<String, Certificate> certificates = new TreeMap<>(new MyComparator());

六、CRUD 測試代碼片段

// 保存
Employee emp = new Employee("Manoj", "Kumar", 4000);
SortedMap<String, Certificate> certs = new TreeMap<>(new MyComparator());
certs.put("ComputerScience", new Certificate("MCA"));
certs.put("BusinessManagement", new Certificate("MBA"));
certs.put("ProjectManagement", new Certificate("PMP"));
emp.setCertificates(certs);
session.save(emp);

// 查詢並遍歷(自動按比較器排序)
SortedMap<String, Certificate> map = emp.getCertificates();
for (Map.Entry<String, Certificate> e : map.entrySet()) {
    System.out.println(e.getKey() + " -> " + e.getValue().getName());
}
// 輸出順序:ProjectManagement -> ComputerScience -> BusinessManagement(降序)

七、對比總結

特性Map (HashMap)SortedMap (TreeMap)
順序無序(插入順序不保證)按鍵排序(自然序/自定義 Comparator)
XML 配置<map><map sort="...">
初始化new HashMap<>()new TreeMap<>(comparator)
性能O(1) 讀寫O(log n) 讀寫
適用場景不關心順序、高頻增刪需有序遍歷、報表輸出

避坑指南

  1. sort="natural" 要求鍵實現 Comparable,String 滿足,自定義鍵類需自行實現。
  2. 比較器類必須有無參構造器,且實現 Comparator<K>(K 為鍵類型)。
  3. cascade="all" 會級聯保存/刪除證書,若證書獨立生命週期請調整為 save-update。
  4. 映射表 certificate_type 建議加唯一約束(employee_id, certificate_type),防止鍵重複。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved