奇妙なタイプのキャストエラー(コンパイル時エラー)に直面しています。ジャーニーテーブルからデータを取得するとすぐに、リストから取得したオブジェクトを認識しないため、次の例外がスローされます。
なんでこんなに文句を言っているのかわからないけど、オブジェクトはすでにジャーニータイプで、「ジャーニー」タイプでテンプレート化したリストを作ったので、すべてのレコードがジャーニーになっているはずです。
"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...
}
以下は、旅のモデルクラスです。
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 ;
}
以下は、上記のジャーニーモデルに対応する休止状態の設定ファイルです
<?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);
}