I am facing a strange type cast error ( Compile Time Error ) As soon as I retrieve the data from the journey table it throws following exception as it does not recognize the Object gotten from the list.
I don't know why it is complaining like this, As the object, is already of journey type and I made a list of templatized with "Journey" type, every record is supposed to be journey.
"INFO: HHH000397: Using ASTQueryTranslatorFactory
Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to de.db.journeyTracker.model.journey
"
Code :
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...
}
Following is the Model class for journey.
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 ;
}
Following is hibernate config file corresponding to above journey model
<?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>
and following is the main 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>
Edit:
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)
I tried the following code.. AS you suggested .. But Still it is giving the exception
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);
}