---------------------------------题记
在使用hibernate的hql区分隐式(implicit)与显式(explicit)关联的时候,根据show_sql的语句,在数据库做了一下简单的测试,以加深印象。其中如下查询语句中1为隐式查询(如:hql 'from student s where s.class.id=s.id'),2、3、4为显式查询(如:hql 'from student s join class c on c.id=s.id')。在hibernate3.2.3以后的版本不支持1-n、n-n,单个实体和组件依然有效。
---------------------------------代码
create table t_temp_student(
pk_id int primary key not null,
f_name varchar(20),
f_age int,
fk_class_id int
);
create table t_temp_class(
pk_id int primary key not null,
f_name varchar(20)
);
insert into t_temp_class values(1,'一班');
insert into t_temp_class values(2,'二班');
insert into t_temp_class values(3,'三班');
insert into t_temp_student values(1,'张学友',10,1);
insert into t_temp_student values(2,'刘德华',11,1);
insert into t_temp_student values(3,'黎明',10,2);
insert into t_temp_student values(4,'郭富城',9,2);
insert into t_temp_student values(5,'陈冠希',5,4);
-- 下列1、2、3句效果相同(除cross join,其他必须携带on,即join是inner join的简写)
select * from t_temp_student s cross join t_temp_class c where s.fk_class_id = c.pk_id; -- 关联属性只能携带在where后面,不能有on,也可以没有where
select * from t_temp_student s inner join t_temp_class c on s.fk_class_id = c.pk_id; -- 不能没有on
select * from t_temp_student s join t_temp_class c on s.fk_class_id = c.pk_id; -- 不能没有on
select * from t_temp_student s left join t_temp_class c on s.fk_class_id = c.pk_id; -- 不能没有on,且left join左侧数据全部列出,若不满足on,则left join左侧数据对应的右侧数据为null