0
import java.util.Date;

public class Pojo {

private int stopID;
private String stopCD;
private String stopName;
private String stopAlias1;
private String stopAlias2;
private String stopAlias3;
private String stopName_Reg_Lang1;
private String stopName_Reg_Lang2;
private String lastUpdatedBy;
private String delFlag;
private String createRoleID;
private String updateRoleID;
private String createUserID;
private String stopNameRoad;
private String audioFile;
private int stopTypeID;
private double latitude;
private double longitude;
private int radius;
private int latRounded1;
private int longRounded1;
private int latRounded2;
private int longRounded2;
private int latRounded3;
private int longRounded3;
private int jurisdiction;
private int districtID;
private int stateID;
private String mobile;
private int cityType;
private int fontType;
private int cityNameID;
private Date createTime;
private Date lastUpdatedTime;

//setters and getters
}

DAO 类方法

public JSONObject getSaveToDatabase(List dataHolder) throws ParseException {
List list= new ArrayList();
     Session sessionOracle=null;
     Pojo Db=new Pojo();
     sessionOracle=BaseDAO.getHibernateSession().openSession();
     org.hibernate.Transaction txOracle=null;

     //list contains record
     Db.setStopID((int)list.get(0));
     System.out.println(Db.getStopID());

     try{

         txOracle=sessionOracle.beginTransaction();
         System.out.println(sessionOracle.isOpen());
         sessionOracle.save(Db);
         txOracle.commit();
         System.out.println("Data is inserted");
          }catch(Exception i){
         txOracle.rollback();
         i.printStackTrace();
         System.out.println("Exception in POI ------"+i.toString());
     }

还有一个例外:

Hibernate: insert into POI_M_STOP (stop_id, stop_name, stop_alias_name1, stop_alias_name2, stop_alias_name3, stop_cd, stop_type_id, jurisdiction, mobile_number, del_flag, create_user_id, latitude, longitude, create_time, last_updated_time, district_id, state_id, lat_rounded, lat_rounded_2, lat_rounded_3, long_rounded, long_rounded_2, long_rounded_3, audio_file, stop_name_road, city_type, city_name_id, STOP_NAME_REG_LANG1, STOP_NAME_REG_LANG2, LAST_UPDATED_BY, CREATE_ROLE_ID, UPDATED_ROLE_ID, RADIUS_FOR_DETECTION, FONT_TYPE) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String

当我在没有休眠的情况下使用上述查询并通过在 JDBC 中插入查询时,数据被正确插入。我只是检查 id 列和“session.save(obj)”和“transaction.commit();--line(1)”。在第 (1) 行,它抛出了上述异常。因为“stop_id”在应用程序中是 int 类型,而 db 中对应的“id”是 int 类型。

XXXX.hbm.xml

<hibernate-mapping>
<class name="excel.apsrtc.pojo.Pojo" table="POI_M_STOP">
<id name="stopID" type="int">
<column name="stopID"></column>
</id>

<property name="stopName" type="java.lang.String" column="stop_name"></property>
    <property name="stopAlias1" type="java.lang.String" column="stop_alias_name1"></property>
    <property name="stopAlias2" type="java.lang.String" column="stop_alias_name2"></property>
    <property name="stopAlias3" type="java.lang.String" column="stop_alias_name3"></property>
    <property name="stopCD" type="java.lang.String" column="stop_cd"></property>
    <property name="stopTypeID" type="int"  column="stop_type_id"></property>
    <property name="jurisdiction" type="java.lang.String"  column="jurisdiction"></property>
    <property name="mobile" type="java.lang.String"  column="mobile_number"></property>
    <property name="delFlag" type="java.lang.String"  column="del_flag"></property>
    <property name="createUserID" type="java.lang.String"  column="create_user_id"></property>
    <property name="latitude" type="double"  column="latitude"></property>
    <property name="longitude" type="double"  column="longitude"></property>
    <property name="createTime" type="java.util.Date"><column name="create_time" /></property>
    <property name="lastUpdatedTime" type="java.util.Date"><column name="last_updated_time" /></property>
    <property name="districtID" type="int"  column="district_id"></property>
    <property name="stateID" type="int"  column="state_id"></property>
     <property name="latRounded1" type="int"  column="lat_rounded"></property>
    <property name="latRounded2" type="int"  column="lat_rounded_2"></property>
    <property name="latRounded3" type="int"  column="lat_rounded_3"></property>
    <property name="longRounded1" type="int"  column="long_rounded"></property>
    <property name="longRounded2" type="int"  column="long_rounded_2"></property>
    <property name="longRounded3" type="int"  column="long_rounded_3"></property>
    <property name="audioFile" type="java.lang.String"  column="audio_file"></property>
    <property name="stopNameRoad" type="java.lang.String"  column="stop_name_road"></property>
    <property name="cityType" type="int"  column="city_type"></property> 
    <property name="cityNameID" type="java.lang.String"  column="city_name_id"></property>
    <property name="stopName_Reg_Lang1" type="java.lang.String" column="STOP_NAME_REG_LANG1"></property>
    <property name="stopName_Reg_Lang2" type="java.lang.String" column="STOP_NAME_REG_LANG2"></property>
    <property name="lastUpdatedBy" type="java.lang.String" column="LAST_UPDATED_BY"></property>
    <property name="createRoleID" type="java.lang.String" column="CREATE_ROLE_ID"></property>
    <property name="updateRoleID" type="java.lang.String" column="UPDATED_ROLE_ID"></property>
    <property name="radius" type="java.lang.String" column="RADIUS_FOR_DETECTION"></property>
    <property name="fontType" type="java.lang.String" column="FONT_TYPE"></property>


</class>
</hibernate-mapping>

请帮我。提前致谢。

4

1 回答 1

0

您的实体类和映射文件之间存在一些不匹配,例如fontType/ FONT_TYPEfontType字段的类型是int,而在 hbm 文件中则映射到java.lang.String.

请查找任何其他类似的不匹配并修复它们。之后,您不应再收到该错误。

于 2013-11-11T21:15:23.557 回答