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