实体类:
Cartes.java
public class Cartes implements java.io.Serializable { // Fields private String cid;
private String cname;
private Integer type;
private Float price;
private String cremork;
private Set elists = new HashSet(0); // Constructors /** default constructor */
public Cartes() {
} /** minimal constructor */
public Cartes(String cname, Integer type, Float price) {
this.cname = cname;
this.type = type;
this.price = price;
} /** full constructor */
public Cartes(String cname, Integer type, Float price, String cremork,
Set elists) {
this.cname = cname;
this.type = type;
this.price = price;
this.cremork = cremork;
this.elists = elists;
} // Property accessors public String getCid() {
return this.cid;
} public void setCid(String cid) {
this.cid = cid;
} public String getCname() {
return this.cname;
} public void setCname(String cname) {
this.cname = cname;
} public Integer getType() {
return this.type;
} public void setType(Integer type) {
this.type = type;
} public Float getPrice() {
return this.price;
} public void setPrice(Float price) {
this.price = price;
} public String getCremork() {
return this.cremork;
} public void setCremork(String cremork) {
this.cremork = cremork;
} public Set getElists() {
return this.elists;
} public void setElists(Set elists) {
this.elists = elists;
}}
Elist.java
public class Elist implements java.io.Serializable { // Fields private String eid;
private Cartes cartes;
private People people;
private Users users;
private Timestamp etime;
private Integer enum_; // Constructors /** default constructor */
public Elist() {
} /** full constructor */
public Elist(Cartes cartes, People people, Users users, Timestamp etime,
Integer enum_) {
this.cartes = cartes;
this.people = people;
this.users = users;
this.etime = etime;
this.enum_ = enum_;
} // Property accessors public String getEid() {
return this.eid;
} public void setEid(String eid) {
this.eid = eid;
} public Cartes getCartes() {
return this.cartes;
} public void setCartes(Cartes cartes) {
this.cartes = cartes;
} public People getPeople() {
return this.people;
} public void setPeople(People people) {
this.people = people;
} public Users getUsers() {
return this.users;
} public void setUsers(Users users) {
this.users = users;
} public Timestamp getEtime() {
return this.etime;
} public void setEtime(Timestamp etime) {
this.etime = etime;
} public Integer getEnum_() {
return this.enum_;
} public void setEnum_(Integer enum_) {
this.enum_ = enum_;
}映射文件:
Cartes.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="com.eshop.entity.Cartes" table="cartes" catalog="eshop">
        <id name="cid" type="java.lang.String">
            <column name="cid" length="10" />
            <generator class="native" />
        </id>
        <property name="cname" type="java.lang.String">
            <column name="cname" length="20" not-null="true" />
        </property>
        <property name="type" type="java.lang.Integer">
            <column name="type" not-null="true" />
        </property>
        <property name="price" type="java.lang.Float">
            <column name="price" precision="12" scale="0" not-null="true" />
        </property>
        <property name="cremork" type="java.lang.String">
            <column name="cremork" length="200" />
        </property>
        <set name="elists" inverse="true">
            <key>
                <column name="cid" length="10" not-null="true" />
            </key>
            <one-to-many class="com.eshop.entity.Elist" />
        </set>
    </class>
</hibernate-mapping>
Elist.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="com.eshop.entity.Elist" table="elist" catalog="eshop">
        <id name="eid" type="java.lang.String">
            <column name="eid" length="20" />
            <generator class="native" />
        </id>
        <many-to-one name="cartes" class="com.eshop.entity.Cartes" fetch="select">
            <column name="cid" length="10" not-null="true" />
        </many-to-one>
        <many-to-one name="people" class="com.eshop.entity.People" fetch="select">
            <column name="pid" not-null="true" />
        </many-to-one>
        <many-to-one name="users" class="com.eshop.entity.Users" fetch="select">
            <column name="uid" length="20" not-null="true" />
        </many-to-one>
        <property name="etime" type="java.sql.Timestamp">
            <column name="etime" length="19" not-null="true" />
        </property>
        <property name="enum_" type="java.lang.Integer">
            <column name="enum" not-null="true" />
        </property>
    </class>
</hibernate-mapping>
实现类:
public class CartesDaoImpl implements CartesDao {
private HibernateTemplate hibernateTemplate;

public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
this.hibernateTemplate = hibernateTemplate;
}
/**
 * 根据类型查询
 */
public List<Cartes> getCartesByType(Integer type) {
String hql ="from Cartes c where c.type="+type;
List<Cartes> list = hibernateTemplate.find(hql);
if(list!=null && list.size()>0){
return list;
}
return null;
}}
测试类:
public class Test { /**
 * @param args
 */
public static void main(String[] args) {
CartesDao cartesDao = new CartesDaoImpl();
List<Cartes> list = cartesDao.getCartesByType(1);
}}
运行的异常:
Exception in thread "main" java.lang.NullPointerException
at com.eshop.dao.impl.CartesDaoImpl.getCartesByType(CartesDaoImpl.java:21)
at com.eshop.test.Test.main(Test.java:16)
请高手指导,小弟不胜感激!!!hibernate异常exception测试

解决方案 »

  1.   

    没有初始化HibernateTemplate,所以报了空指针异常
      

  2.   

    CartesDao cartesDao = new CartesDaoImpl();
    后面调用下setHibernateTemplate
      

  3.   

    Exception in thread "main" java.lang.NullPointerException
    at com.eshop.dao.impl.CartesDaoImpl.getCartesByType(CartesDaoImpl.java:21)楼猪要学会看bug  
    第一行  空指针异常  一般就是没有实例化和注入没有注入进去(非注水猪肉)
    第二行 在 eshop文件里的 dao包里的 imp.CartesDaoImpl 里的 getCartesByType方法 也就是说空指针是这里 然后检测你里面的代码 涉及到空指针的也只有···我靠 老大 你代码没贴全啊  记得给分
      

  4.   


    CartesDao cartesDao = new CartesDaoImpl();
    后面调用下public static void main(String[] args) {
    CartesDao cartesDao = new CartesDaoImpl();
    cartesDao.setHibernateTemplate();// 这里要初始化一个template 
    List<Cartes> list = cartesDao.getCartesByType(1);
    }