Hibernate逍遥游记-第7章 Hibernate的检索策略和检索方式(<set lazy="false" fetch="join">、left join fetch、FetchMode.JOIN、)

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         lazy="proxy"   
20        />
21 
22     </class>
23  
24 </hibernate-mapping>

2.

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

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         inverse="true"
17         lazy="true">
18         
19         <key column="TEAM_ID" />
20         <one-to-many class="mypack.Monkey" />
21      </set>   
22 
23   </class>
24 </hibernate-mapping>

4.

 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     public Team(String name, Set monkeys) {
13         this.name = name;
14         this.monkeys = monkeys;
15     }
16 
17     public Team() {
18     }
19 
20     public Team(Set monkeys) {
21         this.monkeys = monkeys;
22     }
23 
24     public Long getId() {
25         return this.id;
26     }
27 
28     public void setId(Long id) {
29         this.id = id;
30     }
31 
32     public String getName() {
33         return this.name;
34     }
35 
36     public void setName(String name) {
37         this.name = name;
38     }
39 
40     public Set getMonkeys() {
41         return this.monkeys;
42     }
43 
44     public void setMonkeys(Set monkeys) {
45         this.monkeys = monkeys;
46     }
47 
48 }

5.

  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   static{
 10      try{
 11       // Create a configuration based on the properties file we've put
 12        Configuration config = new Configuration();
 13       //load hibernate.cfg.xml
 14        config.configure();
 15       // Get the session factory we can use for persistence
 16       sessionFactory = config.buildSessionFactory();
 17     }catch(RuntimeException e){e.printStackTrace();throw e;}
 18   }
 19 
 20   public void findAllTeams(){
 21     Session session = sessionFactory.openSession();
 22     Transaction tx = null;
 23     try {
 24       tx = session.beginTransaction();
 25 
 26       System.out.println("findAllTeams():executing session.createQuery().list()");
 27       List teamLists=session.createQuery("from Team as m").list();
 28 
 29       System.out.println("findAllTeams():executing teamLists.iterator()");
 30       Iterator teamIterator=teamLists.iterator();
 31 
 32       System.out.println("findAllTeams():iterating team1");
 33       Team team1=(Team)teamIterator.next();
 34 
 35       System.out.println("findAllTeams():iterating team2");
 36       Team team2=(Team)teamIterator.next();
 37 
 38       System.out.println("findAllTeams():iterating team3");
 39       Team team3=(Team)teamIterator.next();
 40 
 41       System.out.println("findAllTeams():iterating team4");
 42       Team team4=(Team)teamIterator.next();
 43 
 44       System.out.println("findAllTeams():executing team1.getMonkeys().iterator()");
 45       Iterator monkeyIterator1=team1.getMonkeys().iterator();
 46 
 47       System.out.println("findAllTeams():executing team2.getMonkeys().iterator()");
 48       Iterator monkeyIterator2=team2.getMonkeys().iterator();
 49 
 50       System.out.println("findAllTeams():executing team3.getMonkeys().iterator()");
 51       Iterator monkeyIterator3=team3.getMonkeys().iterator();
 52 
 53       System.out.println("findAllTeams():executing team4.getMonkeys().iterator()");
 54       Iterator monkeyIterator4=team4.getMonkeys().iterator();
 55 
 56       tx.commit();
 57 
 58     }catch (RuntimeException e) {
 59       if (tx != null) {
 60          tx.rollback();
 61       }
 62       throw e;
 63     } finally {
 64        session.close();
 65     }
 66   }
 67 
 68   public void loadTeam(){
 69 
 70     Session session = sessionFactory.openSession();
 71     Transaction tx = null;
 72     Team team=null;
 73     try {
 74       tx = session.beginTransaction();
 75       
 76       System.out.println("loadTeam():executing session.load()");
 77       team=(Team)session.load(Team.class,new Long(1));
 78       
 79       System.out.println("loadTeam():executing team.getName()");
 80       team.getName();
 81  
 82       System.out.println("loadTeam():executing team.getMonkeys().iterator()");
 83       Iterator monkeyIterator=team.getMonkeys().iterator();
 84 
 85     
 86       tx.commit();
 87 
 88     }catch (RuntimeException e) {
 89       if (tx != null) {
 90         tx.rollback();
 91       }
 92       throw e;
 93     } finally {
 94       session.close();
 95     }
 96   }
 97 
 98   public void getTeam(){
 99     Session session = sessionFactory.openSession();
100     Transaction tx = null;
101     try {
102       tx = session.beginTransaction();
103       System.out.println("getTeam():executing session.get()");
104       Team team=(Team)session.get(Team.class,new Long(1));
105       
106       System.out.println("getTeam():executing team.getName()");
107       team.getName();
108       
109       System.out.println("getTeam():executing team.getMonkeys().iterator()");
110       Iterator monkeyIterator=team.getMonkeys().iterator();
111 
112       tx.commit();
113 
114     }catch (RuntimeException e) {
115       if (tx != null) {
116         tx.rollback();
117       }
118       throw e;
119     } finally {
120       session.close();
121     }
122   }
123 
124   public void getMonkey(){
125     Session session = sessionFactory.openSession();
126     Transaction tx = null;
127     try {
128       tx = session.beginTransaction();
129       System.out.println("getMonkey():executing session.get()");
130       Monkey monkey=(Monkey)session.get(Monkey.class,new Long(1));
131       
132       System.out.println("getMonkey():executing monkey.getName()");
133       monkey.getName();
134       
135       System.out.println("getMonkey():executing monkey.getTeam()");
136       Team team=monkey.getTeam();
137 
138       System.out.println("getMonkey():executing team.getName()");
139       team.getName();
140 
141       tx.commit();
142 
143     }catch (RuntimeException e) {
144       if (tx != null) {
145         tx.rollback();
146       }
147       throw e;
148     } finally {
149       session.close();
150     }
151   }
152 
153   public void loadMonkey(){
154     Session session = sessionFactory.openSession();
155     Transaction tx = null;
156     try {
157       tx = session.beginTransaction();
158       System.out.println("loadMonkey():executing session.load()");
159       Monkey monkey=(Monkey)session.load(Monkey.class,new Long(1));
160       
161       System.out.println("loadMonkey():executing monkey.getName()");
162       monkey.getName();
163       
164       System.out.println("loadMonkey():executing monkey.getTeam()");
165       Team team=monkey.getTeam();
166 
167       System.out.println("loadMonkey():executing team.getName()");
168       team.getName();
169 
170       tx.commit();
171 
172     }catch (RuntimeException e) {
173       if (tx != null) {
174         tx.rollback();
175       }
176       throw e;
177     } finally {
178       session.close();
179     }
180   }
181 
182   public void findAllMonkeys(){
183     Session session = sessionFactory.openSession();
184     Transaction tx = null;
185     try {
186       tx = session.beginTransaction();
187 
188       System.out.println("findAllMonkeys():executing session.createQuery().list()");
189       List monkeyLists=session.createQuery("from Monkey as m ").list();
190 
191       System.out.println("findAllMonkeys():executing monkeyLists.iterator()");
192       Iterator monkeyIterator=monkeyLists.iterator();
193 
194       System.out.println("findAllMonkeys():iterating monkey1");
195       Monkey monkey1=(Monkey)monkeyIterator.next();
196 
197       System.out.println("findAllMonkeys():iterating monkey2");
198       Monkey monkey2=(Monkey)monkeyIterator.next();
199 
200       System.out.println("findAllMonkeys():iterating monkey3");
201       Monkey monkey3=(Monkey)monkeyIterator.next();
202 
203       System.out.println("findAllMonkeys():iterating monkey4");
204       Monkey monkey4=(Monkey)monkeyIterator.next();
205 
206       System.out.println("findAllMonkeys():iterating monkey5");
207       Monkey monkey5=(Monkey)monkeyIterator.next();
208       
209       System.out.println("findAllMonkeys():iterating monkey6");
210       Monkey monkey6=(Monkey)monkeyIterator.next();
211 
212       System.out.println("findAllMonkeys():executing monkey1.getTeam()");
213       Team team1=monkey1.getTeam();
214 
215       System.out.println("findAllMonkeys():executing team1.getName()");
216       if(team1!=null) team1.getName();
217 
218       System.out.println("findAllMonkeys():executing monkey2.getTeam()");
219       Team team2=monkey2.getTeam();
220 
221       System.out.println("findAllMonkeys():executing team2.getName()");
222       if(team2!=null)team2.getName();
223 
224       System.out.println("findAllMonkeys():executing monkey3.getTeam()");
225       Team team3=monkey3.getTeam();
226 
227       System.out.println("findAllMonkeys():executing team3.getName()");
228       if(team3!=null)team3.getName();
229 
230       System.out.println("findAllMonkeys():executing monkey4.getTeam()");
231       Team team4=monkey4.getTeam();
232 
233       System.out.println("findAllMonkeys():executing team4.getName()");
234       if(team4!=null)team4.getName();
235 
236       System.out.println("findAllMonkeys():executing monkey5.getTeam()");
237       Team team5=monkey5.getTeam();
238 
239       System.out.println("findAllMonkeys():executing team5.getName()");
240       if(team5!=null)team5.getName();
241 
242       System.out.println("findAllMonkeys():executing monkey6.getTeam()");
243       Team team6=monkey6.getTeam();
244 
245       System.out.println("findAllMonkeys():executing team6.getName()");
246       if(team6!=null)team6.getName();
247 
248       tx.commit();
249 
250     }catch (RuntimeException e) {
251       if (tx != null) {
252         tx.rollback();
253       }
254       throw e;
255     } finally {
256       session.close();
257     }
258   }
259 
260   public void findTeamLeftJoinMonkey(){
261     Session session = sessionFactory.openSession();
262     Transaction tx = null;
263     try {
264       tx = session.beginTransaction();
265       System.out.println("findTeamLeftJoinMonkey():executing session.createQuery().list()");
266       List teamLists=session
267        .createQuery("from Team as t left join fetch t.monkeys where t.id=1")
268        .list();
269       
270       System.out.println("findTeamLeftJoinMonkey():executing teamLists.iterator()");
271       Iterator teamIterator=teamLists.iterator();
272       
273       System.out.println("findTeamLeftJoinMonkey():iterating team");
274       Team team=(Team)teamIterator.next();
275 
276       System.out.println("findTeamLeftJoinMonkey():executing team.getMonkeys().iterator()");
277       Iterator monkeyIterator=team.getMonkeys().iterator();
278 
279       tx.commit();
280 
281     }catch (RuntimeException e) {
282       if (tx != null) {
283         tx.rollback();
284       }
285       throw e;
286     } finally {
287       session.close();
288     }
289   }
290 
291     public void test(){
292       loadTeam();
293       getTeam();
294       findAllTeams();
295       
296       loadMonkey();
297       getMonkey();
298       findAllMonkeys();
299       findTeamLeftJoinMonkey();
300   }
301 
302   public static void main(String args[]) throws Exception {
303     //new BusinessService().test();
304     new BusinessService().loadTeam();
305     sessionFactory.close();
306   }
307 }

6.

 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>

 7.

原文地址:https://www.cnblogs.com/shamgod/p/5297740.html