Hibernate:抛出Strange TypeCast异常从postgres数据库表中检索Java中的记录

时间:2012-07-18 09:27:28

标签: java database hibernate postgresql

我面临一个奇怪的类型转换错误(编译时错误) 一旦我从旅程表中检索数据,它就会抛出异常,因为它无法识别从列表中获取的对象。

我不知道为什么会这样抱怨,作为对象,已经是旅行类型了,我列出了一个用“Journey”类型模板化的列表,每个记录都应该是旅程。

"INFO: HHH000397: Using ASTQueryTranslatorFactory
Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to de.db.journeyTracker.model.journey
"

代码:

            public static void findalljourney(Session session )
    {

        String qryAllJourney = "SELECT j.id , j.operatingday, j.linename,  j.scheduledeparture, j.scheduledeparturestopname FROM journey j";

        Query query = session.createQuery(qryAllJourney);

        List<journey> allJourney;
        allJourney = query.list();                      

        for (int i = 0; i < allJourney.size(); i++) 
        {  

            String JourneyId =   allJourney.get(i).getJourneyid().toString(); /// TypeCast Exception Occured...
        }  

以下是旅程的Model类。

    public class journey 
{               
      int id ;
      String journeyid ;
      String linename ;
      Date operatingday ;
      Date realtimearrival;
      String  realtimearrivalstopname;
      String realtimearrivalstopnumber ;
      Date realtimedeparture ;
      String realtimedeparturestopname ;
      String realtimedeparturestopnumber;
      Date schedulearrival ;
      String schedulearrivalstopname ;
      String schedulearrivalstopnumber ;
      Date scheduledeparture ;
      String scheduledeparturestopname ;
      String  scheduledeparturestopnumber ;
      int arrivaldelay ;
      String direction ;
      String journeyoperator ;
      String schedulevehicletype ;
      String  vehicleid ;
      Double  distancerealtime;
      int  maximumspeedrealtime ;
      Boolean calledatallstops ;
      int departuredelay ;
      int maximumdelay; 
      int minimumdelay ;
      String phonenumber ;
      int comment_id ;
      Boolean arrivaldelayprojection ;
      Boolean  departuredelayprojection;
      Boolean maximumdelayprojection ;
      Boolean  maximumspeedrealtimedataincomplete;
      Boolean  minmaxdelaydataincomplete ;
      Boolean minimumdelayprojection ;
      Boolean realtimearrivalprojection ;
      Boolean  realtimedepartureprojection ;
       }

以下是与上述旅程模型对应的hibernate配置文件

<?xml version="1.0"?>
<hibernate-mapping>
    <class name="de.db.journeyTracker.model.journey" table="JOURNEY">
        <id name="id" type="int">
            <column name="ID" />
            <generator class="assigned" />
        </id>
        <property name="journeyid" type="java.lang.String">
            <column name="JOURNEYID" />
        </property>
        <property name="linename" type="java.lang.String">
            <column name="LINENAME" />
        </property>
        <property name="operatingday" type="java.util.Date">
            <column name="OPERATINGDAY" />
        </property>
        <property name="realtimearrival" type="java.util.Date">
            <column name="REALTIMEARRIVAL" />
        </property>
        <property name="realtimearrivalstopname" type="java.lang.String">
            <column name="REALTIMEARRIVALSTOPNAME" />
        </property>
        <property name="realtimearrivalstopnumber" type="java.lang.String">
            <column name="REALTIMEARRIVALSTOPNUMBER" />
        </property>
        <property name="realtimedeparture" type="java.util.Date">
            <column name="REALTIMEDEPARTURE" />
        </property>
        <property name="realtimedeparturestopname" type="java.lang.String">
            <column name="REALTIMEDEPARTURESTOPNAME" />
        </property>
        <property name="realtimedeparturestopnumber" type="java.lang.String">
            <column name="REALTIMEDEPARTURESTOPNUMBER" />
        </property>
        <property name="schedulearrival" type="java.util.Date">
            <column name="SCHEDULEARRIVAL" />
        </property>
        <property name="schedulearrivalstopname" type="java.lang.String">
            <column name="SCHEDULEARRIVALSTOPNAME" />
        </property>
        <property name="schedulearrivalstopnumber" type="java.lang.String">
            <column name="SCHEDULEARRIVALSTOPNUMBER" />
        </property>
        <property name="scheduledeparture" type="java.util.Date">
            <column name="SCHEDULEDEPARTURE" />
        </property>
        <property name="scheduledeparturestopname" type="java.lang.String">
            <column name="SCHEDULEDEPARTURESTOPNAME" />
        </property>
        <property name="scheduledeparturestopnumber" type="java.lang.String">
            <column name="SCHEDULEDEPARTURESTOPNUMBER" />
        </property>
        <property name="arrivaldelay" type="int">
            <column name="ARRIVALDELAY" />
        </property>
        <property name="direction" type="java.lang.String">
            <column name="DIRECTION" />
        </property>
        <property name="journeyoperator" type="java.lang.String">
            <column name="JOURNEYOPERATOR" />
        </property>
        <property name="schedulevehicletype" type="java.lang.String">
            <column name="SCHEDULEVEHICLETYPE" />
        </property>
        <property name="vehicleid" type="java.lang.String">
            <column name="VEHICLEID" />
        </property>
        <property name="distancerealtime" type="java.lang.Double">
            <column name="DISTANCEREALTIME" />
        </property>
        <property name="maximumspeedrealtime" type="int">
            <column name="MAXIMUMSPEEDREALTIME" />
        </property>
        <property name="calledatallstops" type="java.lang.Boolean">
            <column name="CALLEDATALLSTOPS" />
        </property>
        <property name="departuredelay" type="int">
            <column name="DEPARTUREDELAY" />
        </property>
        <property name="maximumdelay" type="int">
            <column name="MAXIMUMDELAY" />
        </property>
        <property name="minimumdelay" type="int">
            <column name="MINIMUMDELAY" />
        </property>
        <property name="phonenumber" type="java.lang.String">
            <column name="PHONENUMBER" />
        </property>
        <property name="comment_id" type="int">
            <column name="COMMENT_ID" />
        </property>
        <property name="arrivaldelayprojection" type="java.lang.Boolean">
            <column name="ARRIVALDELAYPROJECTION" />
        </property>
        <property name="departuredelayprojection" type="java.lang.Boolean">
            <column name="DEPARTUREDELAYPROJECTION" />
        </property>
        <property name="maximumdelayprojection" type="java.lang.Boolean">
            <column name="MAXIMUMDELAYPROJECTION" />
        </property>
        <property name="maximumspeedrealtimedataincomplete" type="java.lang.Boolean">
            <column name="MAXIMUMSPEEDREALTIMEDATAINCOMPLETE" />
          </property>
          <property name="minmaxdelaydataincomplete" type="java.lang.Boolean">
              <column name="MINMAXDELAYDATAINCOMPLETE" />
          </property>
          <property name="minimumdelayprojection" type="java.lang.Boolean">
              <column name="MINIMUMDELAYPROJECTION" />
          </property>
          <property name="realtimearrivalprojection" type="java.lang.Boolean">
              <column name="REALTIMEARRIVALPROJECTION" />
          </property>
          <property name="realtimedepartureprojection" type="java.lang.Boolean">
              <column name="REALTIMEDEPARTUREPROJECTION" />
            </property>
         </class>
    </hibernate-mapping>

以下是主要的hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
                                         "http://hibernate.sourceforge.net/hibernate-
configuration-3.0.dtd">
<hibernate-configuration>
 <session-factory >
  <!-- Database connection settings -->
  <property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/newTestDB</property>
  <property name="connection.driver_class">org.postgresql.Driver</property>
  <property name="connection.username">postgres</property>
  <property name="connection.password">usman123</property>
  <property name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
  <!-- SQL dialect - generate SQL for a particular database -->
  <property name="dialect">org.hibernate.dialect.PostgreSQLDialect</property>
  <!-- Echo all executed SQL statements -->
  <property name="show_sql">false</property>
  <!-- Automatic schema creation (begin) === 
<property name="hibernate.hbm2ddl.auto">create</property>  
 Enable Hibernate's automatic session context management -->
  <property name="current_session_context_class">thread</property>
  <!-- Mapping resource configuration files -->
  <mapping resource="de/db/journeyTracker/model/TestTable.hbm.xml"/>
  <mapping resource="de/db/journeyTracker/model/gpspos.hbm.xml"/>
  <mapping resource="de/db/journeyTracker/model/journey.hbm.xml"/>
  <mapping resource="de/db/journeyTracker/model/trip.hbm.xml"/>


    </session-factory> </hibernate-configuration>

编辑:

Exception in thread "main" java.lang.ClassCastException: java.lang.Integer cannot be cast to de.db.journeyTracker.model.journey
    at de.db.journeyTracker.model.journey.findalljourney(journey.java:323)

我尝试了以下代码..正如你所建议的那样..但它仍然是给出异常

           List<Object[]> allJourney;
        allJourney = query.list();
        int Count = 0;
        for (Object[] Journey : allJourney)                 
        {                           

           String strJourneyId =  ((journey) Journey[0]).getJourneyid().toString();
           String strOperatingDay = ((journey) Journey[0]).getOperatingday().toString();
           String strLineName = ((journey) Journey[0]).getLinename();
           String strSchedualeDeparture = ((journey) Journey[0]).getScheduledeparture().toString();
           String strSchedualeDepartureStopName = ((journey) Journey[0]).getScheduledeparturestopname();


          System.out.println(strJourneyId + " " + strOperatingDay + " " + strLineName + " " + strSchedualeDeparture + " " + strSchedualeDepartureStopName);
        }

3 个答案:

答案 0 :(得分:2)

在Hibernate中使用HSQL查询 试试这种类型的查询

1&gt; String qryAllJourney =“from journey j”; 这将返回所有旅程表记录,没有任何类型转换异常。

或者 - 假设类旅程具有适当的构造函数 - 作为实际的类型安全Java对象: 字符串qryAllJourney =“从旅程中选择新旅程(j.id,j.operatingday,j.linename,j.scheduledeparture,j.scheduledeparturestopname)”;

如果您想了解更多详情,请浏览此链接。

http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/queryhql.html

答案 1 :(得分:1)

此查询:

SELECT j.id , j.operatingday, j.linename,  
       j.scheduledeparture, j.scheduledeparturestopname 
FROM journey j

未选择“旅程实例列表”。而是选择List of Object数组。数组元素与选择列表中的值匹配:

   [0] = j.id
   [1] = j.operatingday
   ...

请改用以下代码:

   SELECT j FROM journey j

但是如果你想坚持使用返回的数组,请转到:

  List<Object[]>  allJourney = query.list(); 
  //do what ever you need to do with result:
  for (Object[] row: allJourney) {
      Integer id = (Integer) row[0];
      Date operatingday = (Date) row[1];
  }

答案 2 :(得分:0)

这应该是一个hsql查询!

String qryAllJourney = "SELECT j.id , j.operatingday, j.linename, j.scheduledeparture, j.scheduledeparturestopname FROM journey j";

它应该是这样的;

String qryAllJourney = "from Journey";