gpt4 book ai didi

java - hibernate : Strange TypeCast Exception Thrown While retrieving records in Java from postgres database table

转载 作者:行者123 更新时间:2023-11-29 14:23:41 25 4
gpt4 key购买 nike

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

我不知道为什么它会这样提示,作为对象,已经是旅程类型,我做了一个模板化为“旅程”类型的列表,每条记录都应该是旅程。

"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 ;
}

以下是上述旅程模型对应的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);
}

最佳答案

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

1>String qryAllJourney = "来自旅程 j";这将返回所有旅程表记录,没有任何类型转换异常。

或者 - 假设 class journey 有一个合适的构造函数 - 作为一个实际类型安全的 Java 对象:String qryAllJourney = "select new journey(j.id , j.operatingday, j.linename, j.scheduledeparture, j.scheduledeparturestopname) from journey";

如果您需要更多详细信息,请访问此链接。

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

关于java - hibernate : Strange TypeCast Exception Thrown While retrieving records in Java from postgres database table,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11538257/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com