程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> hibernate3學習筆記(十五)|繼承映射

hibernate3學習筆記(十五)|繼承映射

編輯:關於JAVA

這裡詳細討論繼承映射的3種方式:

1.Table per concrete class

繼承關系如下圖:

數據表設計如下圖:

MySQL數據庫中執行如下DDL:

1.CREATE TABLE defaultuser (
2. id INT(11) NOT NULL auto_increment PRIMARY KEY,
3. name VARCHAR(100) NOT NULL default '',
4. someProperty VARCHAR(100)
5.);
6.
7.CREATE TABLE poweruser (
8. id INT(11) NOT NULL auto_increment PRIMARY KEY,
9. name VARCHAR(100) NOT NULL default '',
10. otherProperty VARCHAR(100)
11.);

User.java

1.package com.hb3.pack_08.model;
2.
3.public class User {
4. 5. private Integer id;
6. private String name;
7.
8. public User() {
9. }
10. 11. public Integer getId() {
12. return id;
13. }
14. public void setId(Integer id) {
15. this.id = id;
16. }
17. public String getName() {
18. return name;
19. }
20. public void setName(String name) {
21. this.name = name;
22. }
23.}

DefaultUser.java

1.package com.hb3.pack_08.model;
2.
3.public class DefaultUser extends User {
4. 5. private String someProperty;
6. 7. public DefaultUser() {
8. }
9. 10. public String getSomeProperty() {
11. return someProperty;
12. }
13. public void setSomeProperty(String someProperty) {
14. this.someProperty = someProperty;
15. }
16.}

PowerUser.java

1.package com.hb3.pack_08.model;
2.
3.public class PowerUser extends User {
4. 5. private String otherProperty;
6.
7. public PowerUser() {
8. }
9. 10. public String getOtherProperty() {
11. return otherProperty;
12. }
13. public void setOtherProperty(String otherProperty) {
14. this.otherProperty = otherProperty;
15. }
16.}

DefaultUser.hbm.xml文件:

1.<?xml version="1.0" encoding="UTF-8"?>
2. 3.<!DOCTYPE hibernate-mapping PUBLIC
4. "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
5. "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 6. 7.<hibernate-mapping> 8. 9. <class name="com.hb3.pack_08.model.DefaultUser" table="defaultuser">
10.
11. <id name="id" column="id" type="java.lang.Integer">
12. <generator class="native"/>
13. </id>
14.
15. <property name="name" column="name" type="java.lang.String"/>
16.
17. <property name="someProperty" column="someProperty" type="java.lang.String"/>
18.
19. </class>
20.
21.</hibernate-mapping>

PowerUser.hbm.xml文件:

1.<?xml version="1.0" encoding="UTF-8"?>
2. 3.<!DOCTYPE hibernate-mapping PUBLIC
4. "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
5. "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 6. 7.<hibernate-mapping> 8. 9. <class name="com.hb3.pack_08.model.PowerUser" table="poweruser"> 10.
11. <id name="id" column="id" type="java.lang.Integer">
12. <generator class="native"/>
13. </id>
14.
15. <property name="name" column="name" type="java.lang.String"/>
16.
17. <property name="otherProperty" column="otherProperty" type="java.lang.String"/>
18.
19. </class>
20.
21.</hibernate-mapping>

修改hibernate.cfg.xml文件:

......

<mapping resource="com/hb3/pack_08/model/DefaultUser.hbm.xml" />
<mapping resource="com/hb3/pack_08/model/PowerUser.hbm.xml" />
......

1.package com.hb3.pack_08;
2.
3.import java.io.IOException;
4.import java.sql.SQLException;
5.import java.util.Iterator;
6.
7.import org.hibernate.Query;
8.import org.hibernate.Session;
9.import org.hibernate.SessionFactory;
10.import org.hibernate.Transaction;
11.import org.hibernate.cfg.Configuration;
12.

13.import com.hb3.pack_08.model.DefaultUser;
14.import com.hb3.pack_08.model.PowerUser;
15.import com.hb3.pack_08.model.User;
16.
17.public class BusinessService {
18.
19. public static void main(String[] args) throws IOException, SQLException {
20.
21. Configuration config = new Configuration().configure();
22. SessionFactory sessionFactory = config.buildSessionFactory();
23. Session session = sessionFactory.openSession();
24.
25.
26. DefaultUser defaultUser = new DefaultUser();
27. defaultUser.setName("chenyan");
28. defaultUser.setSomeProperty("hu....hu...");
29.
30. PowerUser powerUser = new PowerUser();
31. powerUser.setName("shenbin");
32. powerUser.setOtherProperty("Bla...Bla...");
33.
34. Transaction tx = session.beginTransaction();
35. session.save(powerUser);
36. session.save(defaultUser);
37. tx.commit();
38.
39.
40. session.close();
41. session = sessionFactory.openSession();
42.
43.
44. Query query = session.createQuery("from com.hb3.pack_08.model.User");
45. Iterator<?> iterator = query.list().iterator();
46. while(iterator.hasNext()) {
47. User user = (User) iterator.next();
48. System.out.println(user.getName());
49. }
50.
51.
52. session.close();
53. sessionFactory.close();
54. }
55.}

請注意:查詢中查詢的是User,所以會找出所有DefaultUser和PowerUser的資料。User類中不包括的他子類特有的字段也會被找到。

值得一提的是,查詢中如果直接使用"from java.lang.Object"這樣的HQL的話,將查出數據庫中所有的數據資料。因為Object是Java中所有類的父類。

2.Table per class hierarchy

數據表設計如下圖:

DDL文件:

1.CREATE TABLE user (
2. id INT(11) NOT NULL auto_increment PRIMARY KEY,
3. userType VARCHAR(50) NOT NULL,
4. name VARCHAR(100) NOT NULL default '',
5. someProperty VARCHAR(100),
6. otherProperty VARCHAR(100)
7.);

POJO同上,分為:User,PowerUser,DefaultUser。

User.hbm.xml文件:

1.<?xml version="1.0" encoding="utf-8"?>
2.<!DOCTYPE hibernate-mapping
3. PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
4. "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
5.
6.<hibernate-mapping>
7. 8. <class name="com.hb3.pack_09.model.User" table="user">
9.
10. <id name="id" column="id" type="java.lang.Integer">
11. <generator class="native"/>
12. </id>
13.
14. <discriminator column="userType" type="java.lang.String"/>
15.
16. <property name="name" column="name" type="java.lang.String"/>
17.
18. <subclass name="com.hb3.pack_09.model.DefaultUser"
19. discriminator-value="Default">
20.
21. <property name="someProperty"
22. column="someProperty"
23. type="java.lang.String"/>
24. </subclass>
25.
26. <subclass name="com.hb3.pack_09.model.PowerUser"
27. discriminator-value="Power">
28.
29. <property name="otherProperty"
30. column="otherProperty"
31. type="java.lang.String"/>
32. </subclass> 33.
34. </class> 35.
36.</hibernate-mapping>

修改hibernate.cfg.xml文件:

......
<mapping resource="com/hb3/pack_09/model/User.hbm.xml" />
......

測試代碼也和上面的基本一致:

1.package com.hb3.pack_09;
2.
3.import java.io.IOException;
4.import java.sql.SQLException;
5.import java.util.Iterator;
6. 7.import org.hibernate.Query;
8.import org.hibernate.Session;
9.import org.hibernate.SessionFactory;
10.import org.hibernate.Transaction;
11.import org.hibernate.cfg.Configuration;
12. 13.import com.hb3.pack_09.model.DefaultUser;
14.import com.hb3.pack_09.model.PowerUser;
15.import com.hb3.pack_09.model.User;
16. 17.public class BusinessService {
18. 19. public static void main(String[] args) throws IOException, SQLException {
20.
21. Configuration config = new Configuration().configure();
22. SessionFactory sessionFactory = config.buildSessionFactory();
23. Session session = sessionFactory.openSession();
24. 25.
26. DefaultUser defaultUser = new DefaultUser();
27. defaultUser.setName("chenyan");
28. defaultUser.setSomeProperty("hu....hu...");
29.
30. PowerUser powerUser = new PowerUser();
31. powerUser.setName("shenbin");
32. powerUser.setOtherProperty("Bla...Bla...");
33. 34. Transaction tx = session.beginTransaction();
35. session.save(powerUser);
36. session.save(defaultUser);
37. tx.commit();
38.
39.
40. session.close();
41. session = sessionFactory.openSession();
42.
43.
44. Query query = session.createQuery("from com.hb3.pack_09.model.User");
45. Iterator<?> iterator = query.list().iterator();
46. while(iterator.hasNext()) {
47. User user = (User) iterator.next();
48. System.out.println(user.getName());
49. }
50.
51.
52. session.close();
53. sessionFactory.close();
54. }
55.}

3.Table per subclass

數據表設計如下圖:

DDL文件:

1.CREATE TABLE user (
2. id INT(11) NOT NULL auto_increment PRIMARY KEY,
3. name VARCHAR(100) NOT NULL default ''
4.);
5.
6.CREATE TABLE defaultuser (
7. id INT(11) NOT NULL PRIMARY KEY,
8. someProperty VARCHAR(100)
9.);
10. 11.CREATE TABLE poweruser (
12. id INT(11) NOT NULL PRIMARY KEY,
13. otherProperty VARCHAR(100)
14.);

POJO同上,分為:User,PowerUser,DefaultUser。

User.hbm.xml文件:

1.<?xml version="1.0" encoding="utf-8"?>
2.<!DOCTYPE hibernate-mapping
3. PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
4. "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
5.
6.<hibernate-mapping>
7.
8. <class name="com.hb3.pack_10.model.User" table="user">
9.
10. <id name="id" column="id" type="java.lang.Integer">
11. <generator class="native"/>
12. </id>
13.
14. <property name="name" column="name" type="java.lang.String"/>
15.
16. <joined-subclass name="com.hb3.pack_10.model.DefaultUser"
17. table="defaultuser"> 18.
19. <key column="id"/>
20.
21. <property name="someProperty"
22. column="someProperty"
23. type="java.lang.String"/>
24.
25. </joined-subclass>
26.
27. <joined-subclass name="com.hb3.pack_10.model.PowerUser"
28. table="poweruser">
29.
30. <key column="id"/>
31.
32. <property name="otherProperty"
33. column="otherProperty"
34. type="java.lang.String"/>
35.
36. </joined-subclass>
37.
38. </class>
39.
40.</hibernate-mapping>

修改hibernate.cfg.xml文件:

......
<mapping resource="com/hb3/pack_10/model/User.hbm.xml" />
......

測試代碼如下:

1.package com.hb3.pack_10;
2.
3.import java.io.IOException;
4.import java.sql.SQLException;
5.import java.util.Iterator;
6.
7.import org.hibernate.Query;
8.import org.hibernate.Session;
9.import org.hibernate.SessionFactory;
10.import org.hibernate.Transaction;
11.import org.hibernate.cfg.Configuration;
12.
13.import com.hb3.pack_10.model.DefaultUser;
14.import com.hb3.pack_10.model.PowerUser;
15.import com.hb3.pack_10.model.User;
16. 17.public class BusinessService {
18. 19. public static void main(String[] args) throws IOException, SQLException {
20.
21. Configuration config = new Configuration().configure();
22. SessionFactory sessionFactory = config.buildSessionFactory();
23. Session session = sessionFactory.openSession();
24. 25.
26. DefaultUser defaultUser = new DefaultUser();
27. defaultUser.setName("chenyan");
28. defaultUser.setSomeProperty("hu....hu...");
29.
30. PowerUser powerUser = new PowerUser();
31. powerUser.setName("shenbin");
32. powerUser.setOtherProperty("Bla...Bla...");
33. 34. Transaction tx = session.beginTransaction();
35. session.save(powerUser);
36. session.save(defaultUser);
37. tx.commit();
38.
39.
40. session.close();
41. session = sessionFactory.openSession();
42.
43.
44. Query query = session.createQuery("from com.hb3.pack_10.model.User");
45. Iterator<?> iterator = query.list().iterator();
46. while(iterator.hasNext()) {
47. User user = (User) iterator.next();
48. System.out.println(user.getName());
49. }
50.
51.
52. session.close();
53. sessionFactory.close();
54. }
55.}
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved