码迷,mamicode.com
首页 > Web开发 > 详细

Hibernate逍遥游记-第10章 映射继承关系-002继承关系树中的根类对应一个表(discriminator、subclass)

时间:2016-03-20 19:17:20      阅读:206      评论:0      收藏:0      [点我收藏+]

标签:

1.

技术分享

2.

 1 <?xml version="1.0"?>
 2 <!DOCTYPE hibernate-mapping
 3 PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 4 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
 5 <hibernate-mapping >
 6 
 7    <class name="mypack.Monkey" table="MONKEYS">
 8       <id name="id" type="long" column="ID">
 9         <generator class="increment"/>
10       </id>
11       <discriminator column="MONKEY_TYPE" type="string"  />  
12       <property name="name" type="string" column="NAME" />
13 
14       <many-to-one
15         name="team"
16         column="TEAM_ID"
17         class="mypack.Team"
18       />
19 
20       <subclass name="mypack.JMonkey" discriminator-value="JM" >
21          <property name="color" column="COLOR" type="string" />
22       </subclass>
23 
24       <subclass name="mypack.CMonkey" discriminator-value="CM" >
25          <property name="length" column="LENGTH" type="double" />
26       </subclass>
27      
28     </class>
29  
30 </hibernate-mapping>

 

3.

 1 <?xml version="1.0"?>
 2 <!DOCTYPE hibernate-mapping
 3 PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 4 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
 5 <hibernate-mapping >
 6 
 7   <class name="mypack.Team" table="TEAMS" >
 8     <id name="id" type="long" column="ID">
 9       <generator class="increment"/>
10     </id>
11 
12     <property name="name" type="string"  column="NAME" />
13     <set 
14         name="monkeys"
15         inverse="true"
16      >
17         <key column="TEAM_ID" />
18         <one-to-many class="mypack.Monkey" />
19      </set>   
20 
21   </class>
22 </hibernate-mapping>

 

4.

 1 package mypack;
 2 
 3 abstract public class Monkey{
 4 
 5     private Long id;
 6     private String name;
 7     private Team team;
 8 
 9     /** full constructor */
10     public Monkey(String name,Team team) {
11         this.name = name;
12         this.team = team;
13     }
14 
15     /** default constructor */
16     public Monkey() {
17     }
18 
19     public Long getId() {
20         return this.id;
21     }
22 
23     public void setId(Long id) {
24         this.id = id;
25     }
26 
27     public String getName() {
28         return this.name;
29     }
30 
31     public void setName(String name) {
32         this.name = name;
33     }
34 
35     public Team getTeam() {
36         return this.team;
37     }
38 
39     public void setTeam(Team team) {
40         this.team = team;
41     }
42 }

 

5.

 1 package mypack;
 2 
 3 public class CMonkey extends Monkey {
 4 
 5     private double length;
 6 
 7     /** full constructor */
 8     public CMonkey(String name, double length,Team team) {
 9         super(name,team);
10         this.length=length;
11         
12     }
13 
14     /** default constructor */
15     public CMonkey() {
16     }
17 
18    public double getLength() {
19         return this.length;
20     }
21 
22     public void setLength(double length) {
23         this.length = length;
24     }
25 }

 

6.

 1 package mypack;
 2 
 3 
 4 public class JMonkey extends Monkey{
 5 
 6     private String color;
 7 
 8     /** full constructor */
 9     public JMonkey(String name, String color,Team team) {
10         super(name,team);
11         this.color=color;
12     }
13 
14     /** default constructor */
15     public JMonkey() {
16     }
17 
18     public String getColor() {
19         return this.color;
20     }
21 
22     public void setColor(String color) {
23         this.color = color;
24     }
25     
26 }

 

7.

 1 package mypack;
 2 
 3 import java.util.Set;
 4 import java.util.HashSet;
 5 
 6 public class Team {
 7 
 8     private Long id;
 9     private String name;
10     private Set monkeys=new HashSet();
11 
12     /** full constructor */
13     public Team(String name, Set monkeys) {
14         this.name = name;
15         this.monkeys = monkeys;
16     }
17 
18     /** default constructor */
19     public Team() {
20     }
21 
22     /** minimal constructor */
23     public Team(Set monkeys) {
24         this.monkeys = monkeys;
25     }
26 
27     public Long getId() {
28         return this.id;
29     }
30 
31     public void setId(Long id) {
32         this.id = id;
33     }
34 
35     public String getName() {
36         return this.name;
37     }
38 
39     public void setName(String name) {
40         this.name = name;
41     }
42 
43     public Set getMonkeys() {
44         return this.monkeys;
45     }
46 
47     public void setMonkeys(Set monkeys) {
48         this.monkeys = monkeys;
49     }
50 }

 

8.

  1 package mypack;
  2 
  3 import org.hibernate.*;
  4 import org.hibernate.cfg.Configuration;
  5 import java.util.*;
  6 import java.sql.*;
  7 
  8 public class BusinessService{
  9   public static SessionFactory sessionFactory;
 10   static{
 11      try{
 12        Configuration config = new Configuration().configure();
 13        sessionFactory = config.buildSessionFactory();
 14      }catch(RuntimeException e){e.printStackTrace();throw e;}
 15 
 16   }
 17 
 18   public void saveMonkey(Monkey monkey) {
 19       Session session = sessionFactory.openSession();
 20       Transaction tx = null;
 21       List results=new ArrayList();
 22       try {
 23        tx = session.beginTransaction();
 24        session.save(monkey);
 25        tx.commit();
 26     }catch (RuntimeException e) {
 27       if (tx != null) {
 28         tx.rollback();
 29       }
 30       throw e;
 31     } finally {
 32       session.close();
 33     }
 34   }
 35 
 36 
 37   public List findAllJMonkeys(){
 38       Session session = sessionFactory.openSession();
 39       Transaction tx = null;
 40 
 41       try {
 42 
 43        tx = session.beginTransaction();
 44        List results=session.createQuery("from JMonkey").list();
 45        tx.commit();
 46        return results;
 47     }catch (RuntimeException e) {
 48       if (tx != null) {
 49         tx.rollback();
 50       }
 51       throw e;
 52     } finally {
 53       session.close();
 54     }
 55   }
 56 
 57   public List findAllMonkeys(){
 58       Session session = sessionFactory.openSession();
 59       Transaction tx = null;
 60 
 61       try {
 62        tx = session.beginTransaction();
 63        List results=session.createQuery("from Monkey").list();
 64        tx.commit();
 65        return results;
 66      }catch (RuntimeException e) {
 67       if (tx != null) {
 68         tx.rollback();
 69       }
 70       throw e;
 71     } finally {
 72       session.close();
 73     }
 74   }
 75 
 76   public Team loadTeam(long id){
 77       Session session = sessionFactory.openSession();
 78       Transaction tx = null;
 79       try {
 80        tx = session.beginTransaction();
 81        Team team=(Team)session.get(Team.class,new Long(id));
 82        Hibernate.initialize(team.getMonkeys());
 83       tx.commit();
 84       return team;
 85     }catch (RuntimeException e) {
 86       if (tx != null) {
 87         tx.rollback();
 88       }
 89       throw e;
 90     } finally {
 91       session.close();
 92     }
 93   }
 94 
 95    public void test(){
 96       List jMonkeys=findAllJMonkeys();
 97       printAllMonkeys(jMonkeys.iterator());
 98 
 99       List monkeys=findAllMonkeys();
100       printAllMonkeys(monkeys.iterator());
101 
102       Team team=loadTeam(1);
103       printAllMonkeys(team.getMonkeys().iterator());
104 
105       Monkey monkey=new JMonkey("Mary","yellow",team);
106       saveMonkey(monkey);
107 
108   }
109 
110   private void printAllMonkeys(Iterator it){
111      while(it.hasNext()){
112         Monkey m=(Monkey)it.next();
113         if(m instanceof JMonkey)
114           System.out.println(((JMonkey)m).getColor());
115         else
116           System.out.println(((CMonkey)m).getLength());
117       }
118   }
119   public static void main(String args[]) {
120     new BusinessService().test();
121     sessionFactory.close();
122   }
123 }

技术分享

9.

 1 <?xml version="1.0" encoding="utf-8" ?>
 2 <!DOCTYPE hibernate-configuration
 3  PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
 4  "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
 5 
 6 <hibernate-configuration>
 7     <session-factory>
 8         <property name="dialect">
 9             org.hibernate.dialect.MySQLDialect
10         </property>
11         <property name="connection.driver_class">
12             com.mysql.jdbc.Driver
13         </property>
14         <property name="connection.url">
15             jdbc:mysql://localhost:3306/sampledb
16         </property>
17         <property name="connection.username">
18             root
19         </property>
20         <property name="connection.password">
21             1234
22         </property>
23 
24         <property name="show_sql">true</property>
25 
26         <mapping resource="mypack/Team.hbm.xml" />
27         <mapping resource="mypack/Monkey.hbm.xml" />
28     </session-factory>
29 </hibernate-configuration>

 

10.

 1 drop database if exists SAMPLEDB;
 2 create database SAMPLEDB;
 3 use SAMPLEDB;
 4 
 5 create table TEAMS (
 6    ID bigint not null,
 7    NAME varchar(15),
 8    primary key (ID)
 9 );
10 create table MONKEYS (
11    ID bigint not null,
12    NAME varchar(15),
13    MONKEY_TYPE varchar(2),
14    COLOR varchar(15),
15    LENGTH double precision,
16    TEAM_ID bigint,
17    primary key (ID)
18 );
19 
20 alter table MONKEYS add index IDX_TEAM(TEAM_ID), add constraint FK_TEAM foreign key (TEAM_ID) references TEAMS (ID);
21 
22 insert into TEAMS(ID,NAME) values(1,‘ABC Company‘);
23 
24 insert into MONKEYS(ID,MONKEY_TYPE,NAME,COLOR,LENGTH,TEAM_ID) values(1,‘JM‘,‘Tom‘,‘yellow‘,null,1);
25 
26 insert into MONKEYS(ID,MONKEY_TYPE,NAME,COLOR,LENGTH,TEAM_ID) values(2,‘JM‘,‘Mike‘,‘orange‘,null,1);
27 
28 insert into MONKEYS(ID,MONKEY_TYPE,NAME,COLOR,LENGTH,TEAM_ID) values(3,‘CM‘,‘Jack‘,null,1.2,1);
29 
30 insert into MONKEYS(ID,MONKEY_TYPE,NAME,COLOR,LENGTH,TEAM_ID) values(4,‘CM‘,‘Linda‘,null,2.0,1);

 

Hibernate逍遥游记-第10章 映射继承关系-002继承关系树中的根类对应一个表(discriminator、subclass)

标签:

原文地址:http://www.cnblogs.com/shamgod/p/5299052.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!