petDiary映射文件里加入:
<many-to-one name="petInfo"/>
petInfo的映射文件:
    <set name="petDiary">
      <key column="pdid"/>
      <one-to-many class="com.moon.model.PetInfo"/>
    </set>

解决方案 »

  1.   

    petInfo中的映射我也添加了的<set name="petDiaries" inverse="true">
                <key>
                    <column name="diary_pet_id" />
                </key>
                <one-to-many class="com.hch.hib.pojo.PetDiary" />
            </set>但是查询的时候就是查不出来petInfo的信息
      

  2.   

    <set name="petDiaries" inverse="true"> 
           <key> 
               <column name="diary_pet_id" /> 
           </key> 
           <one-to-many class="com.hch.hib.pojo.PetInfo" /> 
    </set> 
      

  3.   

    根据日记信息查询宠物信息配置了many-to-one就可以了.
    那么在petDiary的BEAN中就有属性 private PetInfo petInfo;生成get set方法
    String hql="select * from petDiary where PetDiary.id='"+id+"'";
    List list = this.find(hql);//执行查询
    PetInfo petInfo = list.get(0).getPetInfo();//返回PetInfo对象
    petInfo就是你要获取的宠物信息了.以上只是给楼主一个获取信息的方法,没有严格的去写,只是个思路.
      

  4.   

    ..........1种方法 是控制好你的sql 语句 不要把你的查询结果封装在宠物日记的对象里 hibernate 支持各种结果的封装 我习惯放在list里第2种方法 主要是你的结果要显示在什么地方 这种情况主要注意延迟加载就好了 映射部分不需要太复杂你的可以了
    在你的日记对象中 应该有个属性叫宠物  然后get 那个宠物就有信息了 
      

  5.   

    在PefInfo中是否有日记属性
    private Set diary=new HashSet();
    在PetDiary中时候有PetInfo 的对象
    private PetInfo pet=new PetInfo ();如果都有的话 再看报什么异常  异常:org.hibernate.LazyInitializationException
    描述:failed to lazily initialize a collection of role: com.pet.entity.Pet.diary, no session or session was closed
    原因:配置有问题,把hibernate里面的那个:.hbm.xml文件中,配置lazy=false; 这是由于lazy而产生的错误, 
             <set name="diary"  lazy="false">
           <key column="diary_pet_id" />
           <one-to-many class="com.hch.hib.pojo.PetDiary"/>
            </set>  异常:org.hibernate.exception.SQLGrammarException
    描述:could not initialize a collection: [com.pet.entity.Pet.diary#1]
    原因:Sql语句有误,配置文件中的列名错 你再把SQL语句输出来下  看看有什么错误