当前位置: 代码迷 >> J2EE >> hibernate 查询的奇怪异常
  详细解决方案

hibernate 查询的奇怪异常

热度:236   发布时间:2016-04-22 03:17:44.0
hibernate 查询的奇怪错误
表的映射文件
XML code
<class name="com.fishingport.bean.Inportinfo" table="inportinfo" catalog="fishingport">        <id name="inportid" type="java.lang.Integer">            <column name="inportid" />            <generator class="native" />        </id>        <many-to-one name="portinfo" class="com.fishingport.bean.Portinfo" fetch="select">            <column name="portid" not-null="true" />        </many-to-one>        <many-to-one name="boatinfo" class="com.fishingport.bean.Boatinfo" fetch="select">            <column name="boatid" not-null="true" />        </many-to-one>        <property name="inportTime" type="java.util.Date">            <column name="inportTime" length="19" not-null="true" />        </property>    </class>

这张表关联了其他2张表
Java code
int num = ((Integer) this.getHibernateTemplate().find(                "select count(*) from Inportinfo i where i.portinfo.portid="                        + portid).iterator().next()).intValue();

这个查询可以执行
Java code
list = this.getHibernateTemplate().find("from Inportinfo");

这样却报空指针错误

如果换成无关联的表的话以上查询都可以执行

项目框架struts2+spring2+hibernate3.1

在其他项目中还没遇到过这种问题
哪位高手指教一下


------解决方案--------------------
空指针异常的原文发出来吧.
------解决方案--------------------
XML code
<class name="com.fishingport.bean.Inportinfo" table="inportinfo" catalog="fishingport">        <id name="inportid" type="java.lang.Integer">            <column name="inportid" />            <generator class="native" />        </id>        <many-to-one name="portinfo" class="com.fishingport.bean.Portinfo" fetch="select">            <column name="portid" not-null="true" />        </many-to-one>        <many-to-one name="boatinfo" class="com.fishingport.bean.Boatinfo" fetch="select">            <column name="boatid" not-null="true" />        </many-to-one>        <property name="inportTime" type="java.util.Date">            <column name="inportTime" length="19" not-null="true" />        </property>    </class>
------解决方案--------------------
你看一下这三张表中有没有不允许为空的字段值为空了,(数据库中没做限制)
------解决方案--------------------
你看下你的实体bean,inportTime属性是不是Date类型;
如果是,那么将XML配置成
Java code
<class name="com.fishingport.bean.Inportinfo" table="inportinfo" catalog="fishingport">        <id name="inportid" type="java.lang.Integer">            <column name="inportid" />            <generator class="native" />        </id>        <many-to-one name="portinfo" class="com.fishingport.bean.Portinfo" fetch="select">            <column name="portid" not-null="true" />        </many-to-one>        <many-to-one name="boatinfo" class="com.fishingport.bean.Boatinfo" fetch="select">            <column name="boatid" not-null="true" />        </many-to-one>        <property name="inportTime" >        </property>    </class>
  相关解决方案