Hibernate學(xué)習(xí)筆記(五)-- 實(shí)體層設(shè)計(jì)之Table per class hierarchy
Posted on 2008-09-01 17:18 ∪∩BUG 閱讀(635) 評(píng)論(1) 編輯 收藏 所屬分類: Hibernate學(xué)習(xí)筆記關(guān)于如何配置請(qǐng)參看:Hibernate學(xué)習(xí)筆記(一)--用MyEclipse 6.5+MySQL 5.0的環(huán)境跑起來
準(zhǔn)備:建表
用MySQL在名為STMS數(shù)據(jù)庫中建表Titems
src/org.lxh.hibernate4.TItems.java
1
package org.lxh.hibernate4;
2
/**
3
* @author ∪∩BUG E-mail: tidelgl@163.com
4
* @version Aug 31, 2008 3:27:51 PM
5
* @父類
6
*/
7
public class TItems {
8
9
private String id;
10
private String name;
11
private String manufacturer;
12
13
public String getId() {
14
return id;
15
}
16
17
public void setId(String id) {
18
this.id = id;
19
}
20
21
public String getName() {
22
return name;
23
}
24
25
public void setName(String name) {
26
this.name = name;
27
}
28
29
public String getManufacturer() {
30
return manufacturer;
31
}
32
33
public void setManufacturer(String manufacturer) {
34
this.manufacturer = manufacturer;
35
}
36
37
}
38

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

src/org.lxh.hibernate4.TDvds.java
1
package org.lxh.hibernate4;
2
/**
3
* @author ∪∩BUG E-mail: tidelgl@163.com
4
* @version Aug 31, 2008 3:34:06 PM
5
* @ 繼承父類TItems
6
*/
7
public class TDvds extends TItems {
8
9
private String regionCode;
10
11
public String getRegionCode() {
12
return regionCode;
13
}
14
15
public void setRegionCode(String regionCode) {
16
this.regionCode = regionCode;
17
}
18
19
}
20

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

src/org.lxh.hibernate4.TBooks.java
1
package org.lxh.hibernate4;
2
3
/**
4
* @author ∪∩BUG E-mail: tidelgl@163.com
5
* @version Aug 31, 2008 3:31:47 PM
6
* @ 繼承父類TItems
7
*/
8
public class TBooks extends TItems {
9
10
private int pageCount;
11
12
public int getPageCount() {
13
return pageCount;
14
}
15
16
public void setPageCount(int pageCount) {
17
this.pageCount = pageCount;
18
}
19
}
20

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

src/org.lxh.hibernate4.Titems.hbm.xml
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
7
<hibernate-mapping>
8
<class name="org.lxh.hibernate4.TItems" table="titems"
9
catalog="stms">
10
<id name="id" type="java.lang.String">
11
<column name="id" length="32" />
12
<generator class="assigned" />
13
id>
14
<property name="name" type="java.lang.String">
15
<column name="name" length="20" not-null="true" />
16
property>
17
<property name="manufacturer" type="java.lang.String">
18
<column name="manufacturer" length="20" not-null="true" />
19
property>
20
21
27
<discriminator type="string" column="category">discriminator>
28
<subclass name="org.lxh.hibernate4.TBooks"
29
discriminator-value="1">
30
<property name="pageCount" type="java.lang.Integer">
31
<column name="pageCount" />
32
property>
33
subclass>
34
<subclass name="org.lxh.hibernate4.TDvds"
35
discriminator-value="2">
36
<property name="regionCode" type="java.lang.String">
37
<column name="regionCode" length="2" />
38
property>
39
subclass>
40
class>
41
hibernate-mapping>
42

2

3

4

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

src/hibernate.cfg.xml
1
xml version='1.0' encoding='UTF-8'?>
2
DOCTYPE hibernate-configuration PUBLIC
3
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
4
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
5
6
7
<hibernate-configuration>
8
9
<session-factory>
10
<property name="connection.username">rootproperty>
11
<property name="connection.url">
12
jdbc:mysql://localhost:3306/STMS
13
property>
14
<property name="dialect">
15
org.hibernate.dialect.MySQLDialect
16
property>
17
<property name="myeclipse.connection.profile">
18
MySql_localhost
19
property>
20
<property name="connection.password">rootproperty>
21
<property name="connection.driver_class">
22
com.mysql.jdbc.Driver
23
property>
24
<property name="show_sql">trueproperty>
25
26
27
<mapping resource="org/lxh/hibernate4/Titems.hbm.xml" />
28
29
session-factory>
30
31
hibernate-configuration>

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

src/org.lxh.hibernate4.TItemsOperate.java
1
package org.lxh.hibernate4;
2
3
import java.util.Iterator;
4
5
import org.hibernate.Query;
6
import org.hibernate.Session;
7
import org.hibernate.SessionFactory;
8
import org.hibernate.cfg.Configuration;
9
10
/**
11
* @author ∪∩BUG E-mail: tidelgl@163.com
12
* @version Aug 31, 2008 4:08:14 PM
13
* @具體操作Hibernate類
14
*/
15
public class TItemsOperate {
16
private Session session;
17
18
public TItemsOperate() {
19
//找到Hibernate配置文件
20
Configuration config = new Configuration().configure();
21
22
//從全局文件中取出SessionFactory
23
SessionFactory factory = config.buildSessionFactory();
24
25
//從SessionFactory取出一個(gè)session
26
this.session = factory.openSession();
27
}
28
29
//插入操作
30
//TBook和TDvd類都是TItem的子類,所以只需往TItem里插入就可以了.
31
public void insert(TItems item) {
32
//執(zhí)行語句
33
this.session.save(item);
34
35
//開始事務(wù).提交事務(wù)
36
this.session.beginTransaction().commit();
37
this.session.close();
38
}
39
40
//查詢操作
41
//TBooks和TDvds類都是TItems的子類,所以只需查詢TItem的ID就可以了.
42
public TItems QueryById(String id) {
43
TItems item = new TItems();
44
String hql = "FROM TItems as t WHERE t.id=?";
45
Query q = this.session.createQuery(hql);
46
q.setString(0, id);
47
Iterator iter = q.list().iterator();
48
if(iter.hasNext()){
49
item = (TItems)iter.next();
50
}
51
return item;
52
}
53
}
54

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

src/org.lxh.hibernate4.Test.java
1
package org.lxh.hibernate4;
2
3
import java.awt.print.Book;
4
5
/**
6
* @author ∪∩BUG E-mail: tidelgl@163.com
7
* @version Aug 31, 2008 10:52:25 PM
8
* 類說明
9
*/
10
public class Test {
11
12
public static void main(String[] args) {
13
14
TItemsOperate to = new TItemsOperate();
15
16
//插入數(shù)據(jù)
17
TBooks books = new TBooks();
18
books.setId("03");
19
books.setManufacturer("Apache.org");
20
books.setName("Struts");
21
books.setPageCount(10);
22
23
to.insert(books);
24
25
// TDvds dvds = new TDvds();
26
// dvds.setId("02");
27
// dvds.setManufacturer("MySQL.com");
28
// dvds.setName("MySQL");
29
// dvds.setRegionCode("31");
30
//
31
// to.insert(dvds);
32
33
//查詢操作
34
// TBooks books = (TBooks)to.QueryById("01");
35
// System.out.println(books.getName());
36
}
37
38
}
39

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

例子結(jié)構(gòu):