1. 程式人生 > >Hibernate對映元件(物件)、元件集合屬性

Hibernate對映元件(物件)、元件集合屬性

元件對映

持久化類的屬性可能是一個物件,現在又一個Person類,如下:

@Entity
@Table(name = "person_inf")
public class Person_obj {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;
    private int age;

    //Name物件
    private Name name;

    //省略set、get、建構函式
    ...
}

name屬性是一個Name物件,檢視Name類:

@Embeddable
public class Name { @Column(name = "person_firstname") private String first; @Column(name = "person_lastname") private String last; @Parent private Person_obj owner; //省略set、get、建構函式 ... }

為了讓Name可以做為持久化物件的屬性使用,需要使用@Embeddable註解,該註解不需要指定引數。同時需要在該該類中包含一個屬性,該屬性型別為持久化類,同時要使用@Parent

註解。

測試程式碼:

            session.beginTransaction();
            Person_obj personObj = new Person_obj();
            personObj.setAge(29);
            personObj.setName(new Name("hu", "wenze"));
            session.save(personObj);
            session.getTransaction().commit();

資料庫資料:

mysql> select * from person_inf;
+----+-----+------------------+-----------------+
| id | age | person_firstname | person_lastname | +----+-----+------------------+-----------------+ | 1 | 29 | hu | wenze | | 2 | 29 | hu | wenze | +----+-----+------------------+-----------------+ 2 rows in set (0.00 sec)

可以看到資料表中多了2列,後兩列是Name類中的屬性。

元件集合對映

持久化類的屬性可能是一個集合,比如List,並且List的元素是一個物件,這種其實就是集合對映和元件對映的整合。
現在有一個Person類,該類有一個List屬性,其元素為Name型別; 有一個Map屬性,其鍵為String,值為Score型別。

Person類:

@Entity
@Table(name = "person_inf")
public class Person_obj_list {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;
    private int age;

    @ElementCollection(fetch = FetchType.LAZY, targetClass = Score.class)
    @CollectionTable(name = "score_inf",
            joinColumns = @JoinColumn(name = "person_id", nullable = false))
    @MapKeyClass(String.class)
    @MapKeyColumn(name = "class_name")
    @Column(name = "score_val")
    private Map<String, Score> scores = new HashMap<>();

    @ElementCollection(fetch = FetchType.LAZY, targetClass = Name.class)
    @CollectionTable(name = "nick_inf",
            joinColumns = @JoinColumn(name = "person_id", nullable = false))
    @OrderColumn(name = "list_order")
    private List<Name> nicks = new ArrayList<>();
    //省略set、get、建構函式
    ...
}

Name類:

@Embeddable
public class Name {

    @Column(name = "person_firstname")
    private String first;
    @Column(name = "person_lastname")
    private String last;

    @Parent
    private Person_obj_list owner;
    //省略set、get、建構函式
    ...
}

Score類:

@Embeddable
public class Score {

    @Column(name = "Score_math")
    private double math;
    @Column(name = "Score_science")
    private double science;

    @Parent
    private Person_obj_list owner;
    //省略set、get、建構函式
    ...
}

測試程式碼:

            session.beginTransaction();
            Person_obj_list personObjList = new Person_obj_list();
            personObjList.setAge(21);
            personObjList.getNicks().add(new Name("hu", "wenze"));
            personObjList.getNicks().add(new Name("hu1", "wenze"));
            personObjList.getNicks().add(new Name("hu", "wenze1"));
            personObjList.getScores().put("key2", new Score(1.5, 2.5));
            personObjList.getScores().put("key1", new Score(3.5, 1.1));
            session.save(personObjList);
            session.getTransaction().commit();

資料庫資料:
person_inf

mysql> select * from person_inf;
+----+-----+
| id | age |
+----+-----+
|  1 |  21 |
+----+-----+
1 row in set (0.00 sec)

持久化類的List屬性對應nick_inf:

mysql> select * from nick_inf;
+-----------+------------------+-----------------+------------+
| person_id | person_firstname | person_lastname | list_order |
+-----------+------------------+-----------------+------------+
|         1 | hu               | wenze           |          0 |
|         1 | hu1              | wenze           |          1 |
|         1 | hu               | wenze1          |          2 |
+-----------+------------------+-----------------+------------+
3 rows in set (0.00 sec)

持久化類的Map屬性對應score_inf表:

mysql> select * from score_inf;
+-----------+------------+---------------+------------+
| person_id | Score_math | Score_science | class_name |
+-----------+------------+---------------+------------+
|         1 |        3.5 |           1.1 | key1       |
|         1 |        1.5 |           2.5 | key2       |
+-----------+------------+---------------+------------+
2 rows in set (0.00 sec)

可以看到,元件集合屬性對映就是元件對映+集合對映,理解好元件對映和集合對映後,很容易理解這種情況。