2个持久类分别如下
class title{
         private int id;
private String title;
private String title_username;
private java.sql.Date title_time;
private String top;
private Time title_hourtime;
private List<reply> replys=new ArrayList<reply>();
}public class reply {
private int id;
private int title_id;
private String reply_text;
private String reply_username;
private String shield;
private java.sql.Date reply_time;
private Time reply_hourtime;
}
title和reply是一对多关系
映射文件如下
title.hbm.xml
<class name="bag.Forum.Bean.title" table="learncommunicate_title" lazy="true">
  <id name="id">
    <column name="id"/>
    <generator class="identity"/>
  </id>
  <property name="title" type="java.lang.String" not-null="true">
    <column name="title"/>
  </property>
  <property name="title_username" type="java.lang.String" not-null="true">
    <column name="title_username"/>
  </property>
  <property name="title_time" type="java.sql.Date" not-null="true">
    <column name="title_time"/>
  </property>
  <property name="top" type="java.lang.String">
    <column name="top"/>
  </property>
  <property name="title_hourtime" type="java.sql.Time">
    <column name="title_hourtime"/>
  </property>
  <list name="replys">
    <key column="title_id"/>
    <list-index column="id"></list-index>
    <one-to-many class="bag.Forum.Bean.reply"/>
  </list>
</class>
reply.hbm.xml
<class name="bag.Forum.Bean.learncommunicate_reply" table="learncommunicate_reply">
  <id name="id">
    <column name="id"/>
    <generator class="identity"/>
  </id>
  <property name="title_id" not-null="true">
    <column name="title_id"/>
  </property>
  <property name="reply_text" type="java.lang.String" not-null="true">
    <column name="reply_text"/>
  </property>
  <property name="reply_username" type="java.lang.String" not-null="true">
    <column name="reply_username"/>
  </property>
  <property name="shield" type="java.lang.String">
    <column name="shield"/>
  </property>
  <property name="reply_time" type="java.sql.Date" not-null="true">
    <column name="reply_time"/>
  </property>
  <property name="reply_hourtime" type="java.sql.Time">
    <column name="reply_hourtime"/>
  </property>
</class>
我想问的是怎么样能在读取title时,把和每个title关联的reply也一起读取。我有个测试代码是这样写的
Configuration cfg=new Configuration();
cfg.configure();
SessionFactory sf=cfg.buildSessionFactory();
Session session=sf.getCurrentSession();
Transaction tx=session.beginTransaction();
Query q=session.createQuery("from learncommunicate_title where top='false' order by title_time desc,title_hourtime desc");
List<title> list=new ArrayList<title>();
list=q.list();
reply l=list.get(0).getReplys().get(0);
System.out.println(list.get(0).getReplys().get(0).getReply_text());
tx.commit();为什么提示System.out.println(list.get(0).getReplys().get(0).getReply_text());这一句空指针异常
我知道hibernate在读取一对多关联时会使用延迟加载,我网上也找了找说要读取“一”方同时读取“多”方,就要先用一下“多方”的对象就像这句reply l=list.get(0).getReplys().get(0);
hibernate输出的sql语句也显示的确能读取“多”方的数据
Hibernate: 
    select
        learncommu0_.id as id0_,
        learncommu0_.title as title0_,
        learncommu0_.title_username as title3_0_,
        learncommu0_.title_time as title4_0_,
        learncommu0_.top as top0_,
        learncommu0_.title_hourtime as title6_0_ 
    from
        learncommunicate_title learncommu0_ 
    where
        learncommu0_.top='false' 
    order by
        learncommu0_.title_time desc,
        learncommu0_.title_hourtime desc
Hibernate: 
    select
        replys0_.title_id as title2_1_,
        replys0_.id as id1_,
        replys0_.id as id1_0_,
        replys0_.title_id as title2_1_0_,
        replys0_.reply_text as reply3_1_0_,
        replys0_.reply_username as reply4_1_0_,
        replys0_.shield as shield1_0_,
        replys0_.reply_time as reply6_1_0_,
        replys0_.reply_hourtime as reply7_1_0_ 
    from
        learncommunicate_reply replys0_ 
    where
        replys0_.title_id=?可是为什么还会出现空指针异常呢

解决方案 »

  1.   

     兄弟hibernate是面向的是对象。也就是说你的执行的对象是对类进行的操作。
     你的这个语句:
    Query q=session.createQuery("from learncommunicate_title where top='false' order by title_time desc,title_hourtime desc");仅仅是用session来执行sql语句,这样就不对调用hibernate中的配置文件中的一对多的的语句。也就是说你仅仅是执行了sql语句而已。所以你什么东西也拿不到。
      

  2.   


    Query q=session.createQuery("from learncommunicate_title where top='false' order by title_time desc,title_hourtime desc");
    我代码用的是from title,发帖时忘记改了,不好意思
      

  3.   

    我刚刚改了下测试的代码,改成了:
    Configuration cfg=new Configuration();
    cfg.configure();
    SessionFactory sf=cfg.buildSessionFactory();
    Session session=sf.getCurrentSession();
    Transaction tx=session.beginTransaction();
    title t=(title)session.get(title.class,5);
    List<reply> li=t.getReplys();
    if(!(li==null))System.out.println("非空");
    Iterator it=li.iterator();
    while(it.hasNext())
    {
    reply r=(reply)it.next();
    System.out.println("111");
    }
    tx.commit();输出的结果很奇怪,和learncommunicate_title表中主键为5的数据关联的learncommunicate_reply表中的数据应该只有1个,但是结果却输出了5个111,而且也输出了"非空",既然都输出“非空”了,为什么我具体调用reply对象的getxxx()方法却会抛出空指针异常呢