30hibernate_fetch_1_select
hibernate抓取策略(单端代理的批量抓取)
保持默认(也就是说什么也不配),同fetch="select",如:
<many-to-one name="classes" column="classesid" fetch="select"/>
fetch="select",另外发送一条select语句抓取当前对象关联实体或集合
====================================================================================
public void testFetch1() {
Session session = null;
try {
session = HibernateUtils.getSession();
session.beginTransaction();
Student student = (Student)session.load(Student.class, 1);
System.out.println("student.name=" + student.getName());
System.out.println("classes.name=" + student.getClasses().getName());
session.getTransaction().commit();
}catch(Exception e) {
e.printStackTrace();
session.getTransaction().rollback();
}finally {
HibernateUtils.closeSession(session);
}
}
ExportDB:
create table t_classes (id integer not null auto_increment, name varchar(255), primary key (id))
create table t_student (id integer not null auto_increment, name varchar(255), classesid integer, primary key (id))
alter table t_student add index FK4B9075708EBC77F7 (classesid), add constraint FK4B9075708EBC77F7 foreign key (classesid) references t_classes (id)
/30hibernate_fetch_1/src/com/bjsxt/hibernate/Student.hbm.xml
<many-to-one name="classes" column="classesid" fetch="select"/>
测试结果:发二条
Hibernate: select student0_.id as id1_0_, student0_.name as name1_0_, student0_.classesid as classesid1_0_ from t_student student0_ where student0_.id=?
student.name=班级0的学生0
Hibernate: select classes0_.id as id0_0_, classes0_.name as name0_0_ from t_classes classes0_ where classes0_.id=?
classes.name=班级0
保持默认(也就是说什么也不配),同fetch="select",如:
<many-to-one name="classes" column="classesid" fetch="select"/>
fetch="select",另外发送一条select语句抓取当前对象关联实体或集合
====================================================================================
public void testFetch1() {
Session session = null;
try {
session = HibernateUtils.getSession();
session.beginTransaction();
Student student = (Student)session.load(Student.class, 1);
System.out.println("student.name=" + student.getName());
System.out.println("classes.name=" + student.getClasses().getName());
session.getTransaction().commit();
}catch(Exception e) {
e.printStackTrace();
session.getTransaction().rollback();
}finally {
HibernateUtils.closeSession(session);
}
}
ExportDB:
create table t_classes (id integer not null auto_increment, name varchar(255), primary key (id))
create table t_student (id integer not null auto_increment, name varchar(255), classesid integer, primary key (id))
alter table t_student add index FK4B9075708EBC77F7 (classesid), add constraint FK4B9075708EBC77F7 foreign key (classesid) references t_classes (id)
/30hibernate_fetch_1/src/com/bjsxt/hibernate/Student.hbm.xml
<many-to-one name="classes" column="classesid" fetch="select"/>
测试结果:发二条
Hibernate: select student0_.id as id1_0_, student0_.name as name1_0_, student0_.classesid as classesid1_0_ from t_student student0_ where student0_.id=?
student.name=班级0的学生0
Hibernate: select classes0_.id as id0_0_, classes0_.name as name0_0_ from t_classes classes0_ where classes0_.id=?
classes.name=班级0