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. XHTML1.0对HTML4.0的改进

    1.XHTML借鉴了XML的写法,语法更加严格: 2.XHTML实现了把页面样式和内容分离了,废弃了HTML4.0中表示样式的标签和属性,推荐使用CSS样式来描述页面的样式. XHTML1.0 分为两 ...

  2. selenium+python 浏览器标签页跳转 switch_to_window

    浏览器页面跳转方法记录: from selenium import webdriver import time browser = webdriver.Chrome() first_url='http ...

  3. 【Web学习日记】——在IIS上发布一个WebService

    没有开发过程,只是发布过程 一.前提 开发使用的是VS2013 从来没有做过Web的发布,在网上找例子,看到的总是与自己的情况不相符,而且也有人提出了VS2013发布网站的问题,但解决方案却很少,好不 ...

  4. iOS 进阶 第三天(0326)

    0326 Xib:描述软件界面,轻量级,一般用来描述局部界面 Xib的获取,如下图所示: xib在我们开发的时候叫xib,但如果运行在我们手机里会生成nib.所以xib和nib两种方法来访问xib

  5. Java枚举常见7种用法

    DK1.5引入了新的类型——枚举.在 Java 中它虽然算个“小”功能,却给我的开发带来了“大”方便.用法一:常量在JDK1.5 之前,我们定义常量都是: publicstaticfianl…… .现 ...

  6. Log4j详细使用教程

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

  7. Build Settings

    Add Open Scenes 选择一个关卡,使其处于打开状态,在菜单栏选择 File -> Build Settings 打开Build Settings窗口.选择 Add Open Scen ...

  8. tomcat 7 用mod_jk做 负载均衡

    在Win7中使用apache为tomcat做负载均衡,各组件及版本如下: 两个tomcat v 7.0.57 一个apache v 2.2.14 一个mod_jk v 1.2.33(for windo ...

  9. 一点简单的关于ASP.NET下载

    一点简单的关于ASP.NET下载 个人简单的认为是有两种方法的,第一种就是直接用一个超链接链接到我们要下载的资源就可以了.只是说这个方法会有一点小问题就是,比如像图片或者文本文件这些浏览器是可以自动将 ...

  10. SQL Server视图

    想来想去,总想写写SQL Server方面的知识,像视图.存储过程,大数据量操作的优化等等. 先把基础的知识总结个遍先,然后再寻求更高更远的发展.这篇文章,将带大家来看看视图. 何谓视图,视图包含行和 ...