接续上贴:有两个表  学生表 student   班级表 team   学生表里面有一个字段team_id引用team表
在jsp页面中调用 Iterator it=studentDAO.findAll().iterator(); 的时候报空指针异常。但是通过 findbyID 函数没有问题,相关源代码如下:student 的 model类
package dao;public class Student implements java.io.Serializable { // Fields private String id;
private Team team;
private String name; // Constructors /** default constructor */
public Student() {
} /** full constructor */
public Student(Team team, String name) {
this.team = team;
this.name = name;
} // Property accessors public String getId() {
return this.id;
} public void setId(String id) {
this.id = id;
} public Team getTeam() {
return this.team;
} public void setTeam(Team team) {
this.team = team;
} public String getName() {
return this.name;
} public void setName(String name) {
this.name = name;
}}team的model类
package dao;import java.util.HashSet;
import java.util.Set;/**
 * Team generated by MyEclipse Persistence Tools
 */public class Team implements java.io.Serializable { // Fields private String id;
private String address;
private Set students = new HashSet(0); // Constructors /** default constructor */
public Team() {
} /** minimal constructor */
public Team(String address) {
this.address = address;
} /** full constructor */
public Team(String address, Set students) {
this.address = address;
this.students = students;
} // Property accessors public String getId() {
return this.id;
} public void setId(String id) {
this.id = id;
} public String getAddress() {
return this.address;
} public void setAddress(String address) {
this.address = address;
} public Set getStudents() {
return this.students;
} public void setStudents(Set students) {
this.students = students;
}}student DAO类  负责增删改查等等
package dao;import java.util.Iterator;
import java.util.List;import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Transaction;
import org.hibernate.classic.Session;
import org.hibernate.criterion.Example;/**
 * Data access object (DAO) for domain model class Student.
 * 
 * @see dao.Student
 * @author 
 */public class StudentDAO extends BaseHibernateDAO {
private static final Log log = LogFactory.getLog(StudentDAO.class);
// property constants
public static final String NAME = "name"; public void save(Student transientInstance) {
Session session=(Session)this.getSession();
Transaction tx=session.beginTransaction();
log.debug("saving Student instance");
try {
session.save(transientInstance);
tx.commit();
log.debug("save successful");
} catch (RuntimeException re) {
log.error("save failed", re);
tx.rollback();
throw re;
} finally{
session.close();
}
} public void delete(Student persistentInstance) {
log.debug("deleting Student instance");
try {
getSession().delete(persistentInstance);
log.debug("delete successful");
} catch (RuntimeException re) {
log.error("delete failed", re);
throw re;
}
} public Student findById(java.lang.String id) {
log.debug("getting Student instance with id: " + id);
try {
Student instance = (Student) getSession().get("dao.Student", id);
return instance;
} catch (RuntimeException re) {
log.error("get failed", re);
throw re;
}
} public List findByExample(Student instance) {
log.debug("finding Student instance by example");
try {
List results = getSession().createCriteria("dao.Student").add(
Example.create(instance)).list();
log.debug("find by example successful, result size: "
+ results.size());
return results;
} catch (RuntimeException re) {
log.error("find by example failed", re);
throw re;
}
} public List findByProperty(String propertyName, Object value) {
log.debug("finding Student instance with property: " + propertyName
+ ", value: " + value);
try {
String queryString = "from Student as model where model."
+ propertyName + "= ?";
Query queryObject = getSession().createQuery(queryString);
queryObject.setParameter(0, value);
return queryObject.list();
} catch (RuntimeException re) {
log.error("find by property name failed", re);
throw re;
}
} public List findByName(Object name) {
return findByProperty(NAME, name);
} public List findAll() {
log.debug("finding all Student instances");
try {
String queryString = "from Student";
Query queryObject = getSession().createQuery(queryString);
return queryObject.list();
} catch (RuntimeException re) {
log.error("find all failed", re);
throw re;
}
} public Student merge(Student detachedInstance) {
log.debug("merging Student instance");
try {
Student result = (Student) getSession().merge(detachedInstance);
log.debug("merge successful");
return result;
} catch (RuntimeException re) {
log.error("merge failed", re);
throw re;
}
} public void attachDirty(Student instance) {
log.debug("attaching dirty Student instance");
try {
getSession().saveOrUpdate(instance);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
} public void attachClean(Student instance) {
log.debug("attaching clean Student instance");
try {
getSession().lock(instance, LockMode.NONE);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
}

public static void main(String[]args){
StudentDAO studao=new StudentDAO();
List list=studao.findAll();

}
}
team 的DAO类略
*.hbm.xml配置文件如下:student.hbm.xml<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><hibernate-mapping>
    <class name="dao.Student" table="student" catalog="topszk" lazy="true">
        <id name="id" type="java.lang.String">
            <column name="id" length="32" />
            <generator class="uuid.hex" />
        </id>
        <many-to-one name="team" class="dao.Team" fetch="join">
            <column name="team_id" length="32" not-null="true" />
        </many-to-one>
        <property name="name" type="java.lang.String">
            <column name="name" length="45" not-null="true" />
        </property>
    </class>
</hibernate-mapping>
team.hbm.xml<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- 
    Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
    <class name="dao.Team" table="team" catalog="topszk">
        <id name="id" type="java.lang.String">
            <column name="id" length="32" />
            <generator class="uuid.hex" />
        </id>
        <property name="address" type="java.lang.String">
            <column name="address" length="45" not-null="true" />
        </property>
        <set name="students" inverse="true" cascade="all">
            <key>
                <column name="team_id" length="32" not-null="true" />
            </key>
            <one-to-many class="dao.Student" />
        </set>
    </class>
</hibernate-mapping>

解决方案 »

  1.   

    二楼的帖子 发重复了,没有找到修改帖子的地方。  这一贴接一楼的帖子下面是一个jsp 文件执行了一个操作
    <%@ page language="java" import="java.util.*,dao.*" pageEncoding="utf-8"%><%

    StudentDAO sdao=new StudentDAO();


    Iterator it=sdao.findAll().iterator();%>结果在执行 Iterator it=sdao.findAll().iterator();  的时候报告空指针异常,但是如果通过public Student findById(java.lang.String id) {
    log.debug("getting Student instance with id: " + id);
    try {
    Student instance = (Student) getSession().get("dao.Student", id);
    return instance;
    } catch (RuntimeException re) {
    log.error("get failed", re);
    throw re;
    }
    }
    这个方法获取对象,并且关联获取team对象,很正常,但是用 Iterator 或者list 获取的时候,老是报告空指。
      

  2.   

    多谢各位的帮助,取出 Student 对象时候,由于设置对 班级类(Team)的延迟加载,导致取不到关联类的属性,出现空指针异常 在Team类中的映射文件中加上 lazy="false"就可以了,但是考虑到大量数据时候的性能问题,还得考虑用icesky888 师傅的建议。再次感谢 icesky888师傅给出的建议,以及jiangguanghe185师傅的关注。结贴。