转自:https://blog.csdn.net/iteye_3357/article/details/81862615
1 可能经常遇到这样的情况:
2
3 在数据库表中会有这样的一个字段用来区别记录的属性,如:在客户表中有一个字段表示客户级别,当这个记录为A时是一级客户,为B时是二级客户。在用hiberante做OR表示时类可能是这样的:
4
5 public class Customer{
6
7 private String flag; //表示客户的级别
8
9 ...
10
11 }
12
13 然后,在程序中手动控制flag的值,但是这样当每个级的客户有不同的属性时Customer类将包含所有级别的属性,这样不是很好。
14
15 hibernate提供一个Discriminator映射的方法,就是把一个表映射成不同的类,有不同的属性。
16
17 public class Customer{
18
19 //包含所有级别的公共属性
20
21 ...
22
23 }
24
25
26
27 public class CustomerA extends Customer{
28
29 //只包括一级客户的特有属性
30
31 }
32
33 public class CustomerB extends Customer{
34
35 //只包含二级客户特有的属性
36
37 }
38
39 这样更符合面向对象的原则,然后在hbm.xml中这样写:
40
41 <id name="id" type="int">
42
43 ...
44
45 </id>
46
47 <discriminator column="flag" type="string" />
48
49 <!-- 公共属性的映射 -->
50
51 <subclass name="CustomerA" discriminator-value="A">
52
53 <!-- 一级客户特有属性的映射 -->
54
55 </subclass>
56
57 <subclass name="CustomerB" discriminator-value="B">
58
59 <!-- 二级客户特有属性的映射 -->
60
61 </subclass>
62
63 这样就可以单独的用CustomerA,CustomerB这样的实例了,做数据库修改时就不用关心flag字段的值了,会自动的加A或B。
64
65 如果是使用hibernate Annotation而不是xml来描述映谢关系,代码如下:
66
67 @Entity
68
69 @Table(name = "customer")
70
71 @Inheritance(strategy = InheritanceType.SINGLE_TABLE)
72
73 @DiscriminatorColumn(name = "flag", discriminatorType = DiscriminatorType.STRING)
74
75 public class Customer{
76
77 }
78
79 @Entity
80
81 @DiscriminatorValue(value = "A")
82
83 public class CustomerA extends Customer{
84
85 }
86
87 @Entity
88
89 @DiscriminatorValue(value = "B")
90
91 public class CustomerB extends Customer{
92
93 }
94
95 这样就可以了。
2.
1 public class Singer {
2 private String region;
3 private Long id;
4 private Set<Song> songs = new HashSet<Song>();
5
6 public String getRegion() {
7 return region;
8 }
9
10 public void setRegion(String region) {
11 this.region = region;
12 }
13
14 public Long getId() {
15 return id;
16 }
17
18 public void setId(Long id) {
19 this.id = id;
20 }
21
22 public Set<Song> getSongs() {
23 return songs;
24 }
25
26 public void setSongs(Set<Song> songs) {
27 this.songs = songs;
28 }
29
30 public void addSong(Song song){
31 songs.add(song);
32 song.setSinger(this);
33 }
34 }
3.
1 public class SingleSinger extends Singer {
2 private String name;
3 private char sex;
4
5 public String getName() {
6 return name;
7 }
8
9 public void setName(String name) {
10 this.name = name;
11 }
12
13 public char getSex() {
14 return sex;
15 }
16
17 public void setSex(char sex) {
18 this.sex = sex;
19 }
20 }
3.
1 package edu.jlu.fuliang.domain;
2
3 import java.util.HashSet;
4 import java.util.Set;
5
6 public class CompositeSinger extends Singer {
7 private Set<SingleSinger> singleSingers = new HashSet<SingleSinger>();
8
9 public Set<SingleSinger> getSingleSingers() {
10 return singleSingers;
11 }
12
13 public void setSingleSingers(Set<SingleSinger> singleSingers) {
14 this.singleSingers = singleSingers;
15 }
16 public void addSinger(SingleSinger singleSinger){
17 singleSingers.add(singleSinger);
18 }
19 }
5.
1 public class Band extends CompositeSinger {
2 private String name;
3
4 public String getName() {
5 return name;
6 }
7
8 public void setName(String name) {
9 this.name = name;
10 }
11 }
6.
1 <?xml version="1.0" encoding="utf-8"?> 2 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 3 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 4 5 <hibernate-mapping package="edu.jlu.fuliang.domain"> 6 <class name="Singer" table="SINGER"> 7 <id name="id" type="java.lang.Long"> 8 <column name="SINGER_ID"/> 9 <generator class="identity"/> 10 </id> 11 12 <discriminator column="TYPE" type="string"/> 13 14 <property name="region" type="java.lang.String"> 15 <column name="REGION"/> 16 </property> 17 18 <set name="songs" table="SONG" inverse="true" cascade="save-update"> 19 <key column="SINGER_ID"/> 20 <one-to-many class="Song"/> 21 </set> 22 23 <subclass name="SingleSinger" discriminator-value="1"> 24 <property name="name" type="java.lang.String"> 25 <column name="NAME"/> 26 </property> 27 <property name="sex" type="java.lang.Character"> 28 <column name="SEX"/> 29 </property> 30 </subclass> 31 32 <subclass name="CompositeSinger" discriminator-value="2"> 33 <set name="singleSingers" table="SINGER" cascade="save-update" lazy="false"> 34 <key column="COMPSITESINGER_ID"/> 35 <one-to-many class="SingleSinger"/> 36 </set> 37 </subclass> 38 39 <subclass name="Band" discriminator-value="3"> 40 <set name="singleSingers" table="SINGER" cascade="save-update"> 41 <key column="BAND_ID"/> 42 <one-to-many class="SingleSinger"/> 43 </set> 44 </subclass> 45 </class> 46 </hibernate-mapping>