本人初学者,刚接触Hibernate。我定义了两个持久化类:一个Student类,一个Teacher类,Student类有一个Teacher类属性。然后将这两个类之间做了N-1单向映射(也就是说,多个student对应一个teacher),其中Student对应student表,Teacher对应teacher表,student表中有一列teach_id的外键列,用于参照teacher表。public class Student
{
private long stu_id;
private Teacher teacher;
………………………………
………………………………
(省略了setter和getter方法)
}public class Teacher
{
private long teacher_id;
private String teacher_name ;
………………………………
………………………………
(省略了setter和getter方法)
}然后我根据stud_id进行查询,使用HQL语句"from Student s where s.stu_id=xx",从返回的list中取出了Student实例s,然后用s的getTeacher()方法取得s相关联的Teacher实例t,从而调用t.getTeacher_id()和getTeacher_name()取得t的teacher_id和teacher_name值。可是结果是能取到teacher_id的值,但是teacher_name的值是null?是不是我的HQL语句不对?如果HQL语句不对,该怎么用呢?万分感谢!

解决方案 »

  1.   

    你在student里面加了set集合吗
    Set<teacher> set =new Set<teacher>();
    然后在你的student.cfg.xml里面配上many-to-one就可以了然后把lazy设为true
      

  2.   

    1、在student类中添加
    private Teacher teacher
    为其生存 set、get
    2、在student.hbm.xml中添加 many-to-one 元素
    配置其 name column class layzy cascade等熟悉
      

  3.   

    lz最好把两个类的配置文件贴出来,不过建议使用annotition,简单明了。
      

  4.   

      使用延迟加载和级联set,  直接可以通过对象点出来。 
      

  5.   

    有 annotition 简单一点, 
     
     Student类内 
     
    private Teacher teacher ;
    get 方法上写
    @MangToOne 这种是1-N 单项关联。
      

  6.   

    teacher_name 是非主键值  调不到吧   我觉得是