Hibernate逍遥游记-第10章 映射继承关系-003继承关系树中的每个类对应一个表(joined-subclass)
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 <property name="name" type="string" column="NAME" /> 12 13 <many-to-one 14 name="team" 15 column="TEAM_ID" 16 class="mypack.Team" 17 /> 18 19 <joined-subclass name="mypack.JMonkey" table="JMONKEYS" > 20 <key column="MONKEY_ID" /> 21 <property name="color" column="COLOR" type="string" /> 22 </joined-subclass> 23 24 <joined-subclass name="mypack.CMonkey" table="CMONKEYS" > 25 <key column="MONKEY_ID" /> 26 <property name="length" column="LENGTH" type="double" /> 27 </joined-subclass> 28 29 </class> 30 31 </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 import java.io.Serializable; 4 5 public class CMonkey extends Monkey { 6 7 private double length; 8 9 /** full constructor */ 10 public CMonkey(String name, double length,Team team) { 11 super(name,team); 12 this.length=length; 13 14 } 15 16 /** default constructor */ 17 public CMonkey() { 18 } 19 20 public double getLength() { 21 return this.length; 22 } 23 24 public void setLength(double length) { 25 this.length = length; 26 } 27 }
6.
1 package mypack; 2 3 public class JMonkey extends Monkey{ 4 5 private String color; 6 7 /** full constructor */ 8 public JMonkey(String name, String color,Team team) { 9 super(name,team); 10 this.color=color; 11 } 12 13 /** default constructor */ 14 public JMonkey() { 15 } 16 17 public String getColor() { 18 return this.color; 19 } 20 21 public void setColor(String color) { 22 this.color = color; 23 } 24 25 }
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 try { 41 42 tx = session.beginTransaction(); 43 List results=session.createQuery("from JMonkey").list(); 44 tx.commit(); 45 return results; 46 }catch (RuntimeException e) { 47 if (tx != null) { 48 tx.rollback(); 49 } 50 throw e; 51 } finally { 52 session.close(); 53 } 54 } 55 56 public List findAllMonkeys(){ 57 Session session = sessionFactory.openSession(); 58 Transaction tx = null; 59 60 try { 61 tx = session.beginTransaction(); 62 List results=session.createQuery("from Monkey").list(); 63 tx.commit(); 64 return results; 65 }catch (RuntimeException e) { 66 if (tx != null) { 67 tx.rollback(); 68 } 69 throw e; 70 } finally { 71 session.close(); 72 } 73 } 74 75 public Team loadTeam(long id){ 76 Session session = sessionFactory.openSession(); 77 Transaction tx = null; 78 try { 79 tx = session.beginTransaction(); 80 Team team=(Team)session.get(Team.class,new Long(id)); 81 Hibernate.initialize(team.getMonkeys()); 82 tx.commit(); 83 return team; 84 }catch (RuntimeException e) { 85 if (tx != null) { 86 tx.rollback(); 87 } 88 throw e; 89 } finally { 90 session.close(); 91 } 92 } 93 94 public void test(){ 95 List jMonkeys=findAllJMonkeys(); 96 printAllMonkeys(jMonkeys.iterator()); 97 98 List monkeys=findAllMonkeys(); 99 printAllMonkeys(monkeys.iterator()); 100 101 Team team=loadTeam(1); 102 printAllMonkeys(team.getMonkeys().iterator()); 103 104 Monkey monkey=new JMonkey("Mary","yellow",team); 105 saveMonkey(monkey); 106 107 } 108 109 private void printAllMonkeys(Iterator it){ 110 while(it.hasNext()){ 111 Monkey m=(Monkey)it.next(); 112 if(m instanceof JMonkey) 113 System.out.println(((JMonkey)m).getColor()); 114 else 115 System.out.println(((CMonkey)m).getLength()); 116 } 117 } 118 public static void main(String args[]) { 119 new BusinessService().test(); 120 sessionFactory.close(); 121 } 122 }
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 TEAM_ID bigint, 14 primary key (ID) 15 ); 16 17 create table JMONKEYS ( 18 MONKEY_ID bigint not null, 19 COLOR varchar(15), 20 primary key (MONKEY_ID) 21 ); 22 23 create table CMONKEYS ( 24 MONKEY_ID bigint not null, 25 LENGTH double precision, 26 primary key (MONKEY_ID) 27 ); 28 29 alter table MONKEYS add index IDX_TEAM(TEAM_ID), add constraint FK_TEAM foreign key (TEAM_ID) references TEAMS (ID); 30 31 alter table JMONKEYS add index IDX_MONKEY1(MONKEY_ID), add constraint FK_MONKEY1 foreign key (MONKEY_ID) references MONKEYS (ID); 32 33 alter table CMONKEYS add index IDX_MONKEY2(MONKEY_ID), add constraint FK_MONKEY2 foreign key (MONKEY_ID) references MONKEYS (ID); 34 35 insert into TEAMS(ID,NAME) values(1,'NBA'); 36 37 insert into MONKEYS(ID,NAME,TEAM_ID) values(1,'Tom',1); 38 insert into JMONKEYS(MONKEY_ID,COLOR) values(1,'yellow'); 39 40 insert into MONKEYS(ID,NAME,TEAM_ID) values(2,'Mike',1); 41 insert into JMONKEYS(MONKEY_ID,COLOR) values(2,'orange'); 42 43 insert into MONKEYS(ID,NAME,TEAM_ID) values(3,'Jack',1); 44 insert into CMONKEYS(MONKEY_ID,LENGTH) values(3,1.2); 45 46 insert into MONKEYS(ID,NAME,TEAM_ID) values(4,'Linda',1); 47 insert into CMONKEYS(MONKEY_ID,LENGTH) values(4,2.0);
11.
You can do anything you set your mind to, man!