1.

 <?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping > <class name="mypack.Monkey" table="MONKEYS"> <id name="id" type="long" column="ID">
<generator class="increment"/>
</id> <property name="name" type="string" column="NAME" /> <many-to-one
name="team"
column="TEAM_ID"
class="mypack.Team"
lazy="proxy"
/> </class> </hibernate-mapping>

2.

 package mypack;

 public class Monkey{

     private Long id;
private String name;
private Team team; public Monkey(String name, Team team) {
this.name = name;
this.team = team;
} public Monkey() {
} public Long getId() {
return this.id;
} public void setId(Long id) {
this.id = id;
} public String getName() {
return this.name;
} public void setName(String name) {
this.name = name;
} public Team getTeam() {
return this.team;
} public void setTeam(Team team) {
this.team = team;
}
}

3.

 <?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping > <class name="mypack.Team" table="TEAMS" >
<id name="id" type="long" column="ID">
<generator class="increment"/>
</id> <property name="name" type="string" column="NAME" /> <set
name="monkeys"
inverse="true"
lazy="true"> <key column="TEAM_ID" />
<one-to-many class="mypack.Monkey" />
</set> </class>
</hibernate-mapping>

4.

 package mypack;

 import java.util.Set;
import java.util.HashSet; public class Team { private Long id;
private String name;
private Set monkeys=new HashSet(); public Team(String name, Set monkeys) {
this.name = name;
this.monkeys = monkeys;
} public Team() {
} public Team(Set monkeys) {
this.monkeys = monkeys;
} public Long getId() {
return this.id;
} public void setId(Long id) {
this.id = id;
} public String getName() {
return this.name;
} public void setName(String name) {
this.name = name;
} public Set getMonkeys() {
return this.monkeys;
} public void setMonkeys(Set monkeys) {
this.monkeys = monkeys;
} }

5.

 package mypack;

 import org.hibernate.*;
import org.hibernate.cfg.Configuration;
import java.util.*; public class BusinessService{
public static SessionFactory sessionFactory;
static{
try{
// Create a configuration based on the properties file we've put
Configuration config = new Configuration();
//load hibernate.cfg.xml
config.configure();
// Get the session factory we can use for persistence
sessionFactory = config.buildSessionFactory();
}catch(RuntimeException e){e.printStackTrace();throw e;}
} public void findAllTeams(){
Session session = sessionFactory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction(); System.out.println("findAllTeams():executing session.createQuery().list()");
List teamLists=session.createQuery("from Team as m").list(); System.out.println("findAllTeams():executing teamLists.iterator()");
Iterator teamIterator=teamLists.iterator(); System.out.println("findAllTeams():iterating team1");
Team team1=(Team)teamIterator.next(); System.out.println("findAllTeams():iterating team2");
Team team2=(Team)teamIterator.next(); System.out.println("findAllTeams():iterating team3");
Team team3=(Team)teamIterator.next(); System.out.println("findAllTeams():iterating team4");
Team team4=(Team)teamIterator.next(); System.out.println("findAllTeams():executing team1.getMonkeys().iterator()");
Iterator monkeyIterator1=team1.getMonkeys().iterator(); System.out.println("findAllTeams():executing team2.getMonkeys().iterator()");
Iterator monkeyIterator2=team2.getMonkeys().iterator(); System.out.println("findAllTeams():executing team3.getMonkeys().iterator()");
Iterator monkeyIterator3=team3.getMonkeys().iterator(); System.out.println("findAllTeams():executing team4.getMonkeys().iterator()");
Iterator monkeyIterator4=team4.getMonkeys().iterator(); tx.commit(); }catch (RuntimeException e) {
if (tx != null) {
tx.rollback();
}
throw e;
} finally {
session.close();
}
} public void loadTeam(){ Session session = sessionFactory.openSession();
Transaction tx = null;
Team team=null;
try {
tx = session.beginTransaction(); System.out.println("loadTeam():executing session.load()");
team=(Team)session.load(Team.class,new Long(1)); System.out.println("loadTeam():executing team.getName()");
team.getName(); System.out.println("loadTeam():executing team.getMonkeys().iterator()");
Iterator monkeyIterator=team.getMonkeys().iterator(); tx.commit(); }catch (RuntimeException e) {
if (tx != null) {
tx.rollback();
}
throw e;
} finally {
session.close();
}
} public void getTeam(){
Session session = sessionFactory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
System.out.println("getTeam():executing session.get()");
Team team=(Team)session.get(Team.class,new Long(1)); System.out.println("getTeam():executing team.getName()");
team.getName(); System.out.println("getTeam():executing team.getMonkeys().iterator()");
Iterator monkeyIterator=team.getMonkeys().iterator(); tx.commit(); }catch (RuntimeException e) {
if (tx != null) {
tx.rollback();
}
throw e;
} finally {
session.close();
}
} public void getMonkey(){
Session session = sessionFactory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
System.out.println("getMonkey():executing session.get()");
Monkey monkey=(Monkey)session.get(Monkey.class,new Long(1)); System.out.println("getMonkey():executing monkey.getName()");
monkey.getName(); System.out.println("getMonkey():executing monkey.getTeam()");
Team team=monkey.getTeam(); System.out.println("getMonkey():executing team.getName()");
team.getName(); tx.commit(); }catch (RuntimeException e) {
if (tx != null) {
tx.rollback();
}
throw e;
} finally {
session.close();
}
} public void loadMonkey(){
Session session = sessionFactory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
System.out.println("loadMonkey():executing session.load()");
Monkey monkey=(Monkey)session.load(Monkey.class,new Long(1)); System.out.println("loadMonkey():executing monkey.getName()");
monkey.getName(); System.out.println("loadMonkey():executing monkey.getTeam()");
Team team=monkey.getTeam(); System.out.println("loadMonkey():executing team.getName()");
team.getName(); tx.commit(); }catch (RuntimeException e) {
if (tx != null) {
tx.rollback();
}
throw e;
} finally {
session.close();
}
} public void findAllMonkeys(){
Session session = sessionFactory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction(); System.out.println("findAllMonkeys():executing session.createQuery().list()");
List monkeyLists=session.createQuery("from Monkey as m ").list(); System.out.println("findAllMonkeys():executing monkeyLists.iterator()");
Iterator monkeyIterator=monkeyLists.iterator(); System.out.println("findAllMonkeys():iterating monkey1");
Monkey monkey1=(Monkey)monkeyIterator.next(); System.out.println("findAllMonkeys():iterating monkey2");
Monkey monkey2=(Monkey)monkeyIterator.next(); System.out.println("findAllMonkeys():iterating monkey3");
Monkey monkey3=(Monkey)monkeyIterator.next(); System.out.println("findAllMonkeys():iterating monkey4");
Monkey monkey4=(Monkey)monkeyIterator.next(); System.out.println("findAllMonkeys():iterating monkey5");
Monkey monkey5=(Monkey)monkeyIterator.next(); System.out.println("findAllMonkeys():iterating monkey6");
Monkey monkey6=(Monkey)monkeyIterator.next(); System.out.println("findAllMonkeys():executing monkey1.getTeam()");
Team team1=monkey1.getTeam(); System.out.println("findAllMonkeys():executing team1.getName()");
if(team1!=null) team1.getName(); System.out.println("findAllMonkeys():executing monkey2.getTeam()");
Team team2=monkey2.getTeam(); System.out.println("findAllMonkeys():executing team2.getName()");
if(team2!=null)team2.getName(); System.out.println("findAllMonkeys():executing monkey3.getTeam()");
Team team3=monkey3.getTeam(); System.out.println("findAllMonkeys():executing team3.getName()");
if(team3!=null)team3.getName(); System.out.println("findAllMonkeys():executing monkey4.getTeam()");
Team team4=monkey4.getTeam(); System.out.println("findAllMonkeys():executing team4.getName()");
if(team4!=null)team4.getName(); System.out.println("findAllMonkeys():executing monkey5.getTeam()");
Team team5=monkey5.getTeam(); System.out.println("findAllMonkeys():executing team5.getName()");
if(team5!=null)team5.getName(); System.out.println("findAllMonkeys():executing monkey6.getTeam()");
Team team6=monkey6.getTeam(); System.out.println("findAllMonkeys():executing team6.getName()");
if(team6!=null)team6.getName(); tx.commit(); }catch (RuntimeException e) {
if (tx != null) {
tx.rollback();
}
throw e;
} finally {
session.close();
}
} public void findTeamLeftJoinMonkey(){
Session session = sessionFactory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
System.out.println("findTeamLeftJoinMonkey():executing session.createQuery().list()");
List teamLists=session
.createQuery("from Team as t left join fetch t.monkeys where t.id=1")
.list(); System.out.println("findTeamLeftJoinMonkey():executing teamLists.iterator()");
Iterator teamIterator=teamLists.iterator(); System.out.println("findTeamLeftJoinMonkey():iterating team");
Team team=(Team)teamIterator.next(); System.out.println("findTeamLeftJoinMonkey():executing team.getMonkeys().iterator()");
Iterator monkeyIterator=team.getMonkeys().iterator(); tx.commit(); }catch (RuntimeException e) {
if (tx != null) {
tx.rollback();
}
throw e;
} finally {
session.close();
}
} public void test(){
loadTeam();
getTeam();
findAllTeams(); loadMonkey();
getMonkey();
findAllMonkeys();
findTeamLeftJoinMonkey();
} public static void main(String args[]) throws Exception {
//new BusinessService().test();
new BusinessService().loadTeam();
sessionFactory.close();
}
}

6.

 <?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE hibernate-configuration
PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration>
<session-factory>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="connection.url">
jdbc:mysql://localhost:3306/sampledb
</property>
<property name="connection.username">
root
</property>
<property name="connection.password">
1234
</property> <property name="show_sql">true</property> <mapping resource="mypack/Team.hbm.xml" />
<mapping resource="mypack/Monkey.hbm.xml" />
</session-factory>
</hibernate-configuration>

7.

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

  1. Hibernate逍遥游记-第9章 Hibernate的映射类型

    1. 2. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate ...

  2. Hibernate逍遥游记-第5章映射一对多-01单向<many-to-one>、cascade="save-update"、lazy、TransientObjectException

    1. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hi ...

  3. Hibernate逍遥游记-第15章处理并发问题-003乐观锁

    1. 2. drop database if exists SAMPLEDB; create database SAMPLEDB; use SAMPLEDB; drop table if exists ...

  4. Hibernate逍遥游记-第15章处理并发问题-002悲观锁

    1. 2. hibernate.dialect=org.hibernate.dialect.MySQLDialect hibernate.connection.driver_class=com.mys ...

  5. Hibernate逍遥游记-第13章 映射实体关联关系-006双向多对多(分解为一对多)

    1. 2. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate ...

  6. Hibernate逍遥游记-第13章 映射实体关联关系-005双向多对多(使用组件类集合\<composite-element>\)

    1. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hi ...

  7. Hibernate逍遥游记-第13章 映射实体关联关系-004双向多对多(inverse="true")

    1. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hi ...

  8. Hibernate逍遥游记-第13章 映射实体关联关系-003单向多对多

    0. 1. drop database if exists SAMPLEDB; create database SAMPLEDB; use SAMPLEDB; create table MONKEYS ...

  9. Hibernate逍遥游记-第13章 映射实体关联关系-002用主键映射一对一(<one-to-one constrained="true">、<generator class="foreign">)

    1. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hi ...

随机推荐

  1. Mac开发利器之程序员编辑器MacVim学习总结

    Emacs和Vim都是程序员专用编辑器,Emacs被称为神的编辑器,Vim则是编辑器之神.至于两者到底哪个更好用,网络上两大派系至今还争论不休.不过,相比之下,Emacs更加复杂,已经不能算是一个编辑 ...

  2. SQL中char,varchar,nvarchar等的异同

    比较这几个数据类型,总是忘记,可能比较细节的原因.先做个记号,回头完善.

  3. cdev成员结构体file_operations文件操作结构的分析

    struct file_operations{ struct module *owner; // 指向拥有该结构的模块的指针,避免正在操作时被卸载,一般为初始化为THIS_MODULES loff_t ...

  4. Log4j详细使用教程

    日志是应用软件中不可缺少的部分,Apache的开源项目log4j是一个功能强大的日志组件,提供方便的日志记录.在apache网站:jakarta.apache.org/log4j 可以免费下载到Log ...

  5. 51nod 1257 背包问题 V3

    1257 背包问题 V3 基准时间限制:3 秒 空间限制:131072 KB 分值: 80 难度:5级算法题 N个物品的体积为W1,W2......Wn(Wi为整数),与之相对应的价值为P1,P2.. ...

  6. Android开发在使用第三方推送的时候出现INSTALL_FAILED_VERSION_DOWNGRADE

    [-- :: - push_getui_test] Uploading push_getui_test.apk onto device 'emulator-5554' [-- :: - push_ge ...

  7. Vbox下linux虚拟机根分区扩容

    前言 使用一段时间VBox中的linux后可能会显示根分区空间不足的情况,需要扩容. 通过查阅相关资料,VBox中linux扩容主要有两种办法:通过lvm扩容和通过gparted扩容 LVM条件:VB ...

  8. shell script 的追踪与 debug

    shell script 的追踪与 debug scripts 在运行之前,最怕的就是出现语法错误的问题了!那么我们如何 debug 呢?有没有办法不需要透过直接运行该 scripts 就可以来判断是 ...

  9. CI_Autocomplete_2.0.php轻松实现Bebeans与Codeigniter的智能提示

    在你的NetBeans项目下建立一个CI_Autocomplete_2.0.php的文件,粘贴以下代码:(codeigniter太旧了,其实性能不行,应该没人更了,换了吧,别学这玩意了,坑人) < ...

  10. 阿旭的php开发环境

    过了几年,php技术也日新月异,变化也挺多.哪么对于开发者,感觉有一些简单的方法,写下来,以备以后使用.我觉得吧,Linux写php不如windows写php,调试,查资料和各种功能比较全面,而lin ...