Hibernate4Step5

hardwork

本着应用的目的快速学习Hibernate4框架

  • Hibernate映射继承

前面的话

本节主要介绍:

本文主要讲解Hibernate4的映射继承,主要包括如下:

  1. 每个子类对应一个表
  2. 只有一个基类对应一个表
  3. 每个类对应一个表

Type1:每个子类对应一个表

Demo:建立一个抽象类Picture,并由两个子类LifePic和WorkPic继承该类,每个员工Staff有多个Picture。

Type1.mgc


类图关系如下:
1.mgc

Picture.java


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package cn.codeforgod.model;

public abstract class Picture
{

private long id;
private String name;
private Staff staff;

public long getId()
{

return id;
}
public void setId(long id)
{

this.id = id;
}
public String getName()
{

return name;
}
public void setName(String name)
{

this.name = name;
}
public Staff getStaff()
{

return staff;
}
public void setStaff(Staff staff)
{

this.staff = staff;
}
}

LifePic.java


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package cn.codeforgod.model;

public class LifePic extends Picture
{

private String note; // 备注

public String getNote()
{

return note;
}

public void setNote(String note)
{

this.note = note;
}
}

WorkPic.java


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package cn.codeforgod.model;

public class WorkPic extends Picture
{

private String company; // 公司名称

public String getCompany()
{

return company;
}

public void setCompany(String company)
{

this.company = company;
}
}

Staff.java


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package cn.codeforgod.model;

import java.util.Set;

public class Staff
{

private int id;
private String name;
private Set<Picture> pics;

public int getId()
{

return id;
}
public void setId(int id)
{

this.id = id;
}
public String getName()
{

return name;
}
public void setName(String name)
{

this.name = name;
}
public Set<Picture> getPics()
{

return pics;
}
public void setPics(Set<Picture> pics)
{

this.pics = pics;
}
@Override
public String toString()
{

return "Staff [id=" + id + ", name=" + name + ", pics=" + pics + "]";
}

}

LifePic.hbm.xml


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="cn.codeforgod.model">

<class name="LifePic" table="t_lifePic">
<id name="id" column="lifePicId">
<generator class="native"></generator>
</id>

<property name="name" column="picName"></property>

<many-to-one name="staff" column="staffId"
class="cn.codeforgod.model.Staff">
</many-to-one>

<property name="note" column="note" type="string"></property>
</class>

</hibernate-mapping>

WorkPic.hbm.xml


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="cn.codeforgod.model">

<class name="WorkPic" table="t_workPic">
<id name="id" column="workPicId">
<generator class="native"></generator>
</id>

<property name="name" column="picName"></property>

<many-to-one name="staff" column="staffId"
class="cn.codeforgod.model.Staff">
</many-to-one>

<property name="company" column="company" type="string"></property>
</class>

</hibernate-mapping>

Staff.hbm.xml


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="cn.codeforgod.model">

<class name="Staff" table="t_staff">
<id name="id" column="staffId">
<generator class="native"></generator>
</id>

<property name="name" column="staffName"></property>
</class>

</hibernate-mapping>

Hibernate.cfg.xml


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

<session-factory>

<!--数据库连接设置 -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql:///db_hibernate</property>
<property name="connection.username">root</property>
<property name="connection.password">123456</property>


<!-- 方言 -->
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>

<!-- 控制台显示SQL -->
<property name="show_sql">true</property>

<!-- 自动更新表结构 -->
<property name="hbm2ddl.auto">update</property>

<mapping resource="cn/codeforgod/model/Staff.hbm.xml"/>
<mapping resource="cn/codeforgod/model/LifePic.hbm.xml"/>
<mapping resource="cn/codeforgod/model/WorkPic.hbm.xml"/>

<mapping resource="cn/codeforgod/model/Staff2.hbm.xml"/>
<mapping resource="cn/codeforgod/model/Picture2.hbm.xml"/>

<mapping resource="cn/codeforgod/model/Staff3.hbm.xml"/>
<mapping resource="cn/codeforgod/model/Picture3.hbm.xml"/>

</session-factory>

</hibernate-configuration>

Test.java


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package cn.codeforgod.test;


import java.io.IOException;
import java.text.ParseException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import cn.codeforgod.model.LifePic;
import cn.codeforgod.model.LifePic2;
import cn.codeforgod.model.LifePic3;
import cn.codeforgod.model.Picture;
import cn.codeforgod.model.Picture2;
import cn.codeforgod.model.Picture3;
import cn.codeforgod.model.Staff;
import cn.codeforgod.model.Staff2;
import cn.codeforgod.model.Staff3;
import cn.codeforgod.model.WorkPic;
import cn.codeforgod.model.WorkPic2;
import cn.codeforgod.model.WorkPic3;
import cn.codeforgod.util.HibernateUtil;

public class Test
{

public static void main(String[] args) throws ParseException, IOException
{

testType1();
// testType2();
// testType3();
}

public static void testType1()
{

SessionFactory sessionFactory = HibernateUtil.getSessionFactory();

// 存
Session session1 = sessionFactory.openSession();
Transaction transaction1 = session1.beginTransaction();

Staff staff = new Staff();
staff.setName("Tom");

Picture lifePic = new LifePic();
lifePic.setName("life1.jpg");
lifePic.setStaff(staff);
((LifePic) lifePic).setNote("备注...");

Picture workPic = new WorkPic();
workPic.setName("work1.jpg");
workPic.setStaff(staff);
((WorkPic) workPic).setCompany("公司1");

Set<Picture> pics = new HashSet<Picture>();
pics.add(lifePic);
pics.add(workPic);

staff.setPics(pics);

session1.save(lifePic);
session1.save(workPic);
session1.save(staff);

transaction1.commit();
session1.close();

// 取
Session session2 = sessionFactory.openSession();
Transaction transaction2 = session2.beginTransaction();

Staff staff2 = (Staff) session2.get(Staff.class, Integer.valueOf(1));
@SuppressWarnings("unchecked")
List<LifePic> lifePics = (List<LifePic>) session2
.createQuery(
"From LifePic l where l.staff.id=" + staff2.getId())
.list();
@SuppressWarnings("unchecked")
List<WorkPic> workPics = (List<WorkPic>) session2
.createQuery(
"From WorkPic w where w.staff.id=" + staff2.getId())
.list();
Set<Picture> pics2 = new HashSet<Picture>();
pics2.addAll(lifePics);
pics2.addAll(workPics);
System.out.println(staff2);
for (Picture pic : pics2)
{
System.out.println(pic.getName());
}

transaction2.commit();
session2.close();

sessionFactory.close();
}

public static void testType2()
{

SessionFactory sessionFactory = HibernateUtil.getSessionFactory();

// 存
Session session1 = sessionFactory.openSession();
Transaction transaction1 = session1.beginTransaction();

Staff2 staff = new Staff2();
staff.setName("Tom");

Picture2 lifePic = new LifePic2();
lifePic.setName("life1.jpg");
lifePic.setStaff(staff);
((LifePic2) lifePic).setNote("备注...");

Picture2 workPic = new WorkPic2();
workPic.setName("work1.jpg");
workPic.setStaff(staff);
((WorkPic2) workPic).setCompany("公司1");

Set<Picture2> pics = new HashSet<Picture2>();
pics.add(lifePic);
pics.add(workPic);

staff.setPics(pics);

session1.save(staff);
session1.save(lifePic);
session1.save(workPic);

transaction1.commit();
session1.close();

// 取
Session session2 = sessionFactory.openSession();
Transaction transaction2 = session2.beginTransaction();

Staff2 staff2 = (Staff2) session2.get(Staff2.class, Integer.valueOf(1));
Set<Picture2> pics2 = staff2.getPics();
System.out.println(staff2);
for (Picture2 pic : pics2)
{
System.out.println(pic.getName());
}

transaction2.commit();
session2.close();

sessionFactory.close();
}

public static void testType3()
{

SessionFactory sessionFactory = HibernateUtil.getSessionFactory();

// 存
Session session1 = sessionFactory.openSession();
Transaction transaction1 = session1.beginTransaction();

Staff3 staff = new Staff3();
staff.setName("Tom");

Picture3 lifePic = new LifePic3();
lifePic.setName("life1.jpg");
lifePic.setStaff(staff);
((LifePic3) lifePic).setNote("备注...");

Picture3 workPic = new WorkPic3();
workPic.setName("work1.jpg");
workPic.setStaff(staff);
((WorkPic3) workPic).setCompany("公司1");

Set<Picture3> pics = new HashSet<Picture3>();
pics.add(lifePic);
pics.add(workPic);

staff.setPics(pics);

session1.save(staff);
session1.save(lifePic);
session1.save(workPic);

transaction1.commit();
session1.close();

// 取
Session session2 = sessionFactory.openSession();
Transaction transaction2 = session2.beginTransaction();

Staff3 staff2 = (Staff3) session2.get(Staff3.class, Integer.valueOf(1));
Set<Picture3> pics2 = staff2.getPics();
System.out.println(staff2);
for (Picture3 pic : pics2)
{
System.out.println(pic.getName());
}

transaction2.commit();
session2.close();

sessionFactory.close();
}
}

数据库中表的关系如下图:


mappingExtend1

Type2:基类对应一个表

Demo:建立一个具体类Picture,并由两个子类LifePic和WorkPic继承该类,每个员工Staff有多个Picture。

Type2.mgc


类图关系如下:
2.mgc

Picture2.java


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package cn.codeforgod.model;

public class Picture2
{

private long id;
private String picType;
private String name;
private Staff2 staff;

public String getPicType()
{

return picType;
}
public void setPicType(String picType)
{

this.picType = picType;
}
public long getId()
{

return id;
}
public void setId(long id)
{

this.id = id;
}
public String getName()
{

return name;
}
public void setName(String name)
{

this.name = name;
}
public Staff2 getStaff()
{

return staff;
}
public void setStaff(Staff2 staff)
{

this.staff = staff;
}

}

LifePic2.java


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package cn.codeforgod.model;

public class LifePic2 extends Picture2
{

private String note; // 备注

public String getNote()
{

return note;
}

public void setNote(String note)
{

this.note = note;
}


}

WorkPic2.java


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package cn.codeforgod.model;

public class WorkPic2 extends Picture2
{

private String company; // 公司名称

public String getCompany()
{

return company;
}

public void setCompany(String company)
{

this.company = company;
}
}

Staff2.java


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package cn.codeforgod.model;

import java.util.Set;

public class Staff2
{

private int id;
private String name;
private Set<Picture2> pics;

public int getId()
{

return id;
}
public void setId(int id)
{

this.id = id;
}
public String getName()
{

return name;
}
public void setName(String name)
{

this.name = name;
}
public Set<Picture2> getPics()
{

return pics;
}
public void setPics(Set<Picture2> pics)
{

this.pics = pics;
}
@Override
public String toString()
{

return "Staff [id=" + id + ", name=" + name + ", pics=" + pics + "]";
}

}

Picture2.hbm.xml


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="cn.codeforgod.model">

<class name="Picture2" table="t_picture2">
<id name="id" column="pictureId">
<generator class="native"></generator>
</id>

<discriminator column="picType" type="string"></discriminator>

<property name="name" column="picName"></property>

<many-to-one name="staff" column="staffId"
class="cn.codeforgod.model.Staff2">
</many-to-one>

<subclass name="cn.codeforgod.model.LifePic2" discriminator-value="lifePic">
<property name="note" type="string"></property>
</subclass>

<subclass name="cn.codeforgod.model.WorkPic2" discriminator-value="workPic">
<property name="company" type="string"></property>
</subclass>
</class>

</hibernate-mapping>

Staff2.hbm.xml


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="cn.codeforgod.model">

<class name="Staff2" table="t_staff2">
<id name="id" column="staffId">
<generator class="native"></generator>
</id>

<property name="name" column="staffName"></property>

<set name="pics">
<key column="staffId"></key>
<one-to-many class="cn.codeforgod.model.Picture2" />
</set>
</class>

</hibernate-mapping>

数据库中表的关系如下图:


mappingExtend2

Type3:每个类对应一个表

Demo:建立一个具体类Picture,并由两个子类LifePic和WorkPic继承该类,每个员工Staff有多个Picture。

Type3.mgc


类图关系如下:
3.mgc

Picture3.java


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package cn.codeforgod.model;

public class Picture3
{

private long id;
private String picType;
private String name;
private Staff3 staff;

public String getPicType()
{

return picType;
}
public void setPicType(String picType)
{

this.picType = picType;
}
public long getId()
{

return id;
}
public void setId(long id)
{

this.id = id;
}
public String getName()
{

return name;
}
public void setName(String name)
{

this.name = name;
}
public Staff3 getStaff()
{

return staff;
}
public void setStaff(Staff3 staff)
{

this.staff = staff;
}
}

LifePic3.java


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package cn.codeforgod.model;

public class LifePic3 extends Picture3
{

private String note; // 备注

public String getNote()
{

return note;
}

public void setNote(String note)
{

this.note = note;
}


}

WorkPic3.java


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package cn.codeforgod.model;

public class WorkPic3 extends Picture3
{

private String company; // 公司名称

public String getCompany()
{

return company;
}

public void setCompany(String company)
{

this.company = company;
}
}

Staff3.java


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package cn.codeforgod.model;

import java.util.Set;

public class Staff3
{

private int id;
private String name;
private Set<Picture3> pics;

public int getId()
{

return id;
}
public void setId(int id)
{

this.id = id;
}
public String getName()
{

return name;
}
public void setName(String name)
{

this.name = name;
}
public Set<Picture3> getPics()
{

return pics;
}
public void setPics(Set<Picture3> pics)
{

this.pics = pics;
}
@Override
public String toString()
{

return "Staff [id=" + id + ", name=" + name + ", pics=" + pics + "]";
}

}

Picture3.hbm.xml


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="cn.codeforgod.model">

<class name="Picture3" table="t_picture3">
<id name="id" column="pictureId">
<generator class="native"></generator>
</id>

<property name="name" column="picName"></property>

<many-to-one name="staff" column="staffId"
class="cn.codeforgod.model.Staff3">
</many-to-one>

<joined-subclass name="cn.codeforgod.model.LifePic3"
table="t_lifePic3">
<key column="lifePicId"></key>
<property name="note" column="note" type="string"></property>
</joined-subclass>

<joined-subclass name="cn.codeforgod.model.WorkPic3"
table="t_workPic3">
<key column="workPicId"></key>
<property name="company" column="company" type="string"></property>
</joined-subclass>
</class>

</hibernate-mapping>

Staff3.hbm.xml


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="cn.codeforgod.model">

<class name="Staff3" table="t_staff3">
<id name="id" column="staffId">
<generator class="native"></generator>
</id>

<property name="name" column="staffName"></property>

<set name="pics">
<key column="staffId"></key>
<one-to-many class="cn.codeforgod.model.Picture3" />
</set>
</class>

</hibernate-mapping>

数据库中表的关系如下图:


mappingExtend3

Tips

在映射文件中,不能有cascade属性。

然后本文好似有点难以理解,但是这是Hibernate给我们的思想,值得深思。愿你我都能写出漂亮的代码,让中国的IT也可以让世界瞩目(虽然这几乎不可能实现)。

坚持原创技术分享,您的支持将鼓励我继续创作