首页 > 代码库 > hibernate处理视图问题(记录)

hibernate处理视图问题(记录)

Mark,在使用hibernate处理视图的时候。因为视图没有主键,这个用Myeclipse自动生成的POJO类就有两个。一个类名.java,一个是类名Id.java,而映射文件只有一个。因此造成一个问题是,只要视图中有一个字段有一个为NULL,你查出来的数据就会为NULL。

解决办法:

只要把hibernate逆向生成的文件修改一下即可,直接把视图当成数据库表使用,当然视图是不能插入数据的。

如:

反向生成的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.aircondition.store.pojo.VStockchanpin" table="v_stockchanpin" catalog="aircondition">        <composite-id name="id" class="com.aircondition.store.pojo.VStockchanpinId">            <key-property name="id" type="java.lang.String">                <column name="id" length="50" />            </key-property>            <key-property name="spid" type="java.lang.String">                <column name="spid" length="50" />            </key-property>            <key-property name="spmc" type="java.lang.String">                <column name="spmc" length="100" />            </key-property>            <key-property name="spsl" type="java.lang.Integer">                <column name="spsl" />            </key-property>            <key-property name="ggxh" type="java.lang.String">                <column name="ggxh" length="100" />            </key-property>            <key-property name="spdm" type="java.lang.String">                <column name="spdm" length="100" />            </key-property>            <key-property name="xgr" type="java.lang.String">                <column name="xgr" length="50" />            </key-property>            <key-property name="sfsc" type="java.lang.String">                <column name="sfsc" length="10" />            </key-property>            <key-property name="xgsj" type="java.util.Date">                <column name="xgsj" length="10" />            </key-property>        </composite-id>    </class></hibernate-mapping>

修改成(把复合主键改成主键,把key-property属性改成property属性):

<?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.aircondition.store.pojo.VStockchanpin" table="v_stockchanpin" catalog="aircondition">               <id name="id" type="java.lang.String">                   <column name="id"></column>                   <generator class="guid"></generator>               </id>            <property name="spid" type="java.lang.String">                <column name="spid" length="50" />            </property>            <property name="spmc" type="java.lang.String">                <column name="spmc" length="100" />            </property>            <property name="spsl" type="java.lang.Integer">                <column name="spsl" />            </property>            <property name="ggxh" type="java.lang.String">                <column name="ggxh" length="100" />            </property>            <property name="spdm" type="java.lang.String">                <column name="spdm" length="100" />            </property>            <property name="xgr" type="java.lang.String">                <column name="xgr" length="50" />            </property>            <property name="sfsc" type="java.lang.String">                <column name="sfsc" length="10" />            </property>            <property name="xgsj" type="java.util.Date">                <column name="xgsj" length="10" />            </property>    </class></hibernate-mapping>

相应的pojo也修改一下,直接把XXX.java删除,把带XXXId.java改成XXX.java即可。override的equal方法和hashcode方法,也可删除。