hql不会加载关联对象

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="com.zjlolife.hibernate">
    <class name="Teacher" table="t_teacher">
            <id name="id">
               <generator class="native"></generator>
            </id>
            <property name="name"/>
            <property name="age"/>
     <set name="students" lazy="false">
     	<key column="teacher_id"></key>
     	<one-to-many class="Student"/>
     </set>       
    </class>
</hibernate-mapping>



<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="com.zjlolife.hibernate">
    <class name="Student" table="t_studnet">
            <id name="id">
               <generator class="native"></generator>
            </id>
            <property name="name"/>
            <property name="age"/>
            <!-- 抓取策略默认为select,即不通过外连接,而join属性通过外关联抓取 -->
            <many-to-one name="teacher" column="teacher_id" cascade="all"  not-null="true" lazy="false" fetch="join"/>
            
    </class>
</hibernate-mapping>



public void test5() {		
		Configuration conf = new Configuration().configure();
		SessionFactory sessFactory = conf.buildSessionFactory();
		Session session = null;
		Transaction tx = null;
		try {
			session = sessFactory.openSession();
			tx = session.beginTransaction();
			
			List list = session.createQuery("select s.name,t.name from Student s inner join  s.teacher t ").list();
		   
		    Iterator it = list.iterator();
		   while(it.hasNext()) {
		    	Object [] o = (Object[]) it.next();
		    	System.out.println("student的名字"+o[0]);
		    	System.out.println("teacher的名字"+o[1]);
		    	
		    }
		    tx.commit();
		}catch(Exception e) {
			e.printStackTrace();
		    tx.rollback();
		}
	}


Hibernate: 
    select
        student0_.name as col_0_0_,
        teacher1_.name as col_1_0_ 
    from
        t_studnet student0_ 
    inner join
        t_teacher teacher1_ 
            on student0_.teacher_id=teacher1_.id


一对多双向关联 ,上面观察语句是没有发生加载现象,可以观察到 teacher中的set是设置的是lazy=false;上面并没有 加载所有的sutdent,所以Hql不会加载关联对象的

你可能感兴趣的:(hql不会加载关联对象)