1.
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 9 <id name="id" type="long" column="ID"> 10 <generator class="increment"/> 11 </id> 12 13 <property name="name" type="string" column="NAME" /> 14 15 <many-to-one 16 name="team" 17 column="TEAM_ID" 18 class="mypack.Team" 19 cascade="save-update" 20 /> 21 22 </class> 23 24 </hibernate-mapping>
2.
1 package mypack; 2 3 public class Monkey { 4 5 6 private long id; 7 private String name; 8 private Team team; 9 10 public Monkey() {} 11 12 public Monkey(String name, Team team) { 13 this.name = name; 14 this.team = team; 15 } 16 17 public long getId() { 18 return this.id; 19 } 20 21 public void setId(long id) { 22 this.id = id; 23 } 24 public String getName() { 25 return this.name; 26 } 27 28 public void setName(String name) { 29 this.name = name; 30 } 31 public Team getTeam() { 32 return this.team; 33 } 34 35 public void setTeam(Team team) { 36 this.team = team; 37 } 38 }
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 <!-- 14 <set 15 name="monkeys" 16 cascade="all-delete-orphan" 17 inverse="true" 18 > 19 20 <key column="TEAM_ID" /> 21 <one-to-many class="mypack.Monkey" /> 22 </set> 23 --> 24 <set 25 name="monkeys" 26 inverse="true" 27 cascade="save-update" 28 > 29 30 <key column="TEAM_ID" /> 31 <one-to-many class="mypack.Monkey" /> 32 </set> 33 34 </class> 35 </hibernate-mapping>
4.
1 package mypack; 2 3 import java.util.HashSet; 4 import java.util.Set; 5 6 public class Team{ 7 8 private long id; 9 private String name; 10 private Set monkeys = new HashSet(); 11 12 public Team() {} 13 14 public Team(String name, Set monkeys) { 15 this.name = name; 16 this.monkeys = monkeys; 17 } 18 19 public long getId() { 20 return this.id; 21 } 22 23 public void setId(long id) { 24 this.id = id; 25 } 26 public String getName() { 27 return this.name; 28 } 29 30 public void setName(String name) { 31 this.name = name; 32 } 33 public Set getMonkeys() { 34 return this.monkeys; 35 } 36 37 public void setMonkeys(Set monkeys) { 38 this.monkeys = monkeys; 39 } 40 }
5.
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 <hibernate-configuration> 6 <session-factory> 7 <property name="dialect"> 8 org.hibernate.dialect.MySQLDialect 9 </property> 10 <property name="connection.driver_class"> 11 com.mysql.jdbc.Driver 12 </property> 13 <property name="connection.url"> 14 jdbc:mysql://localhost:3306/sampledb 15 </property> 16 <property name="connection.username"> 17 root 18 </property> 19 <property name="connection.password"> 20 1234 21 </property> 22 <property name="show_sql">true</property> 23 <mapping resource="mypack/Team.hbm.xml" /> 24 <mapping resource="mypack/Monkey.hbm.xml" /> 25 </session-factory> 26 </hibernate-configuration>
6.
1 package mypack; 2 3 import org.hibernate.*; 4 import org.hibernate.cfg.Configuration; 5 import java.util.*; 6 7 public class BusinessService{ 8 public static SessionFactory sessionFactory; 9 private Long idOfTom; 10 private Long idOfBULL; 11 private Long idOfJack; 12 private Long idOfDREAM; 13 14 static{ 15 try{ 16 Configuration config = new Configuration(); 17 config.configure(); 18 sessionFactory = config.buildSessionFactory(); 19 }catch(RuntimeException e){e.printStackTrace();throw e;} 20 } 21 22 public void printMonkeysOfTeam(Long teamId){ 23 Session session = sessionFactory.openSession(); 24 Transaction tx = null; 25 try { 26 tx = session.beginTransaction(); 27 Team team=(Team)session.get(Team.class,teamId); 28 printMonkeys(team.getMonkeys()); 29 tx.commit(); 30 }catch (RuntimeException e) { 31 if (tx != null) { 32 tx.rollback(); 33 } 34 throw e; 35 } finally { 36 session.close(); 37 } 38 } 39 40 public void saveTeamAndMonkeyWithCascade(){ 41 Session session = sessionFactory.openSession(); 42 Transaction tx = null; 43 try { 44 tx = session.beginTransaction(); 45 46 Team team=new Team("BULL",new HashSet()); 47 Monkey monkey=new Monkey(); 48 monkey.setName("Tom"); 49 50 monkey.setTeam(team); 51 team.getMonkeys().add(monkey); 52 53 session.save(team); 54 tx.commit(); 55 56 idOfBULL=team.getId(); 57 idOfTom=monkey.getId(); 58 59 }catch (RuntimeException e) { 60 if (tx != null) { 61 tx.rollback(); 62 } 63 e.printStackTrace(); 64 } finally { 65 session.close(); 66 } 67 } 68 69 public void associateTeamAndMonkey(){ 70 Session session = sessionFactory.openSession(); 71 Transaction tx = null; 72 try { 73 tx = session.beginTransaction(); 74 Team team=(Team)session.load(Team.class,idOfDREAM); 75 Monkey monkey=(Monkey)session.load(Monkey.class,idOfJack); 76 monkey.setTeam(team); 77 team.getMonkeys().add(monkey); 78 tx.commit(); 79 }catch (RuntimeException e) { 80 if (tx != null) { 81 tx.rollback(); 82 } 83 e.printStackTrace(); 84 } finally { 85 session.close(); 86 } 87 } 88 89 public void saveTeamAndMonkeySeparately(){ 90 Session session = sessionFactory.openSession(); 91 Transaction tx = null; 92 try { 93 tx = session.beginTransaction(); 94 95 Team team=new Team("DREAM",new HashSet()); 96 Monkey monkey=new Monkey(); 97 monkey.setName("Jack"); 98 99 session.save(team); 100 session.save(monkey); 101 102 tx.commit(); 103 idOfDREAM=team.getId(); 104 idOfJack=monkey.getId(); 105 106 }catch (RuntimeException e) { 107 if (tx != null) { 108 tx.rollback(); 109 } 110 e.printStackTrace(); 111 } finally { 112 session.close(); 113 } 114 } 115 116 public void deleteTeam(Long teamId){ 117 Session session = sessionFactory.openSession(); 118 Transaction tx = null; 119 try { 120 tx = session.beginTransaction(); 121 Team team=(Team)session.load(Team.class,teamId); 122 session.delete(team); 123 tx.commit(); 124 125 }catch (RuntimeException e) { 126 if (tx != null) { 127 tx.rollback(); 128 } 129 e.printStackTrace(); 130 } finally { 131 session.close(); 132 } 133 } 134 135 public void removeMonkeyFromTeam(Long teamId){ 136 Session session = sessionFactory.openSession(); 137 Transaction tx = null; 138 try { 139 tx = session.beginTransaction(); 140 Team team=(Team)session.load(Team.class,teamId); 141 Monkey monkey=(Monkey)team.getMonkeys().iterator().next(); 142 143 //解除team和Monkey的关联关系 144 team.getMonkeys().remove(monkey); 145 monkey.setTeam(null); 146 tx.commit(); 147 148 }catch (RuntimeException e) { 149 if (tx != null) { 150 tx.rollback(); 151 } 152 e.printStackTrace(); 153 } finally { 154 session.close(); 155 } 156 } 157 158 public void printMonkeys(Set monkeys){ 159 for (Iterator it = monkeys.iterator(); it.hasNext();) { 160 Monkey monkey=(Monkey)it.next(); 161 System.out.println("Monkeys in "+monkey.getTeam().getName()+ " :"+monkey.getName()); 162 } 163 } 164 165 public void saveTeamAndMonkeyWithInverse(){ 166 saveTeamAndMonkeySeparately(); 167 associateTeamAndMonkey(); 168 } 169 public void test(){ 170 171 saveTeamAndMonkeyWithCascade(); 172 saveTeamAndMonkeyWithInverse(); 173 printMonkeysOfTeam(idOfBULL); 174 //deleteTeam(idOfDREAM); 175 removeMonkeyFromTeam(idOfBULL); 176 } 177 178 public static void main(String args[]){ 179 new BusinessService().test(); 180 sessionFactory.close(); 181 } 182 }