hibernate 查询与连接

drop   database   if   exists  schoolproject;
create   database  schoolproject;
use  schoolproject;

drop   table   if   exists  certificate;
CREATE   TABLE  certificate (
  id 
varchar ( 100 NOT   NULL   default   '' ,
  `describe` 
varchar ( 100 default   '' ,
  
PRIMARY   KEY   (id)
) ENGINE 
=  InnoDB
CHARACTER   SET  utf8 COLLATE utf8_general_ci;


--
--
 Dumping data for table 'certificate'
--

INSERT   INTO  certificate  VALUES  ( ' ff80808105416d3b0105416d3eca0001 ' , ' 222 ' );
INSERT   INTO  certificate  VALUES  ( ' ff808081054175b501054175b9190001 ' , ' 222 ' );

--
--
 Table structure for table 'student'
--
drop   table   if   exists  student;
CREATE   TABLE  student (
  team_id 
varchar ( 100 default   '' ,
  id 
varchar ( 100 NOT   NULL   default   '' ,
  name 
varchar ( 20 default   '' ,
  `cardId` 
varchar ( 20 NOT   NULL   default   '' ,
  age 
int ( 11 default   ' 0 ' ,
  
PRIMARY   KEY   (id)
) ENGINE 
=  InnoDB
CHARACTER   SET  utf8 COLLATE utf8_general_ci;

--
--
 Dumping data for table 'student'
--

INSERT   INTO  student  VALUES  ( ' ff80808105416d3b0105416d3eca0002 ' , ' ff80808105416d3b0105416d3eca0001 ' , ' tomclus ' , ' 200512345 ' , 33 );
INSERT   INTO  student  VALUES  ( ' ff80808105416d3b0105416d3eca0002 ' , ' ff808081054175b501054175b9190001 ' , ' tom ' , ' 11111111 ' , 33 );

--
--
 Table structure for table 'team'
--
drop   table   if   exists  team;
CREATE   TABLE  team (
  id 
varchar ( 100 NOT   NULL   default   '' ,
  teamName 
varchar ( 100 default   '' ,
  
PRIMARY   KEY   (id)
) ENGINE 
=  InnoDB
CHARACTER   SET  utf8 COLLATE utf8_general_ci;

--
--
 Dumping data for table 'team'
--

INSERT   INTO  team  VALUES  ( ' ff80808105416d3b0105416d3eca0002 ' , ' team1 ' );

 
Student.hbm.xml

<? xml version="1.0" ?>
<! DOCTYPE hibernate-mapping
    PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"
>
< hibernate-mapping  >
    
< class  name ="model.Student"  table ="student"  lazy ="true" > <!-- 把类和数表关联起来 -->
        
< id  name ="id"  unsaved-value ="null" > <!-- id的产生方式是uuid.hex -->
            
< generator  class ="uuid.hex"   />
        
</ id >
        
< property  name ="cardId"  type ="string"   /> <!-- 映射号 -->
        
< property  name ="name"  type ="string"   /> <!-- 映射学生名 -->
        
< property  name ="age"  type ="int"   /> <!-- 映射学生岁数 -->
        
< many-to-one  name ="team"  column ="team_id"  class ="model.Team"  cascade ="none"   lazy ="true" /> <!-- 映射班级 -->

    
</ class >
</ hibernate-mapping >

Team.hbm.xml

<? xml version="1.0" ?>
<! DOCTYPE hibernate-mapping
    PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"
>
< hibernate-mapping >
    
< class  name ="model.Team"  table ="team"  lazy ="true" > <!-- 把类和数表关联起来 -->
        
< id  name ="id"  unsaved-value ="null" > <!-- id的产生方式是uuid.hex -->
            
< generator  class ="uuid.hex"   />
        
</ id >
        
< property  name ="teamName"  type ="string"   />
        
< set  name ="students"  lazy ="true"  cascade ="all" >
            
< key  column ="team_id"   />
            
< one-to-many  class ="model.Student"   />
        
</ set >
        
< set  name ="cers"  lazy ="true"  cascade ="all" >
            
< key  column ="team_id"   />
            
< one-to-many  class ="model.Certificate"   />
        
</ set >
    
</ class >
</ hibernate-mapping >

 

二、简单的单表查询

public static void showOneSheet() {
  List list = StudentDAO.getAllStu("from Student as s");
  for (int i=0;i<list.size();i++) {
   Student stu = (Student) list.get(i);
   System.out.println(stu.getName());//打印学生名
   System.out.println(stu.getTeam().getTeamName());//打印班级名
  }
 }

则,输出:

Hibernate: select student0_.id as id, student0_.cardId as cardId0_, student0_.name as name0_, student0_.age as age0_, student0_.team_id as team5_0_ from student student0_
Hibernate: select team0_.id as id0_, team0_.teamName as teamName2_0_ from team team0_ where team0_.id=?

两条sql语句。

总结:

1、当查询子类时会立即加载其所对应的父类。

如果在子类的配置文件中配置了多个关联父类,则在查询子类时会将其所对应的多个父类加载出来,即每加载一个父类会连接一次数据库产生一条sql语句。

共产生的sql语句的条数=子类+每个父类

2、访问父类对象时,用子类对象.父类对象即可。

Team t = tu.getTeam();

三、外连接

1、什么时候用外连接?

1)、如果想用一条sql语句查出子类与父类

2)、或是想将符合一个条件的某一张表A中的数据全部输出,不管其连接的另外一张表B中有无表A中的记录。

3)、父类加载子类时,我们一般情况不会在配置文件里设置立即加载。而多数情况是父表对延迟加载。所以这时候用到了 left outer join

2、使用情况:

1)、子表外连接父表

 List list = StudentDAO.getAllStu("from Student as s left join s.team ");

Hibernate: select student0_.id as id0_, team1_.id as id1_, student0_.cardId as cardId0_0_, student0_.name as name0_0_, student0_.age as age0_0_, student0_.team_id as team5_0_0_, team1_.teamName as teamName2_1_ from student student0_ left outer join team team1_ on student0_.team_id=team1_.id

个人觉得没必要这样。

2)、父类外连接子类(多数情况)

List list = StudentDAO.getAllStu("from Team t left join t.students ");

Hibernate: select team0_.id as id0_, students1_.id as id1_, team0_.teamName as teamName2_0_, students1_.cardId as cardId0_1_, students1_.name as name0_1_, students1_.age as age0_1_, students1_.team_id as team5_0_1_ from team team0_ left outer join student students1_ on team0_.id=students1_.team_id

配置文件中可以不配置fetch="join" 或outer-join="auto"

取得每个对象:

Object stuAndTeam1[]=(Object[]) list.get(0);//取得第一个数组
  Student stu=(Student) stuAndTeam1[0];//

Team t = stu.getTeam();

 

四、预先抓取

待下篇幅

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值