首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2EE开发 >

hibernate 查询的奇怪异常

2012-01-26 
hibernate 查询的奇怪错误表的映射文件XML codeclass namecom.fishingport.bean.Inportinfo tablein

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> 

热点排行