我正在努力解决hibernate问题,因为几小时后在任何网站上找不到任何帮助。我已经阅读了几十个教程,尝试了所有内容,但我总是遇到类强制转换异常或编译错误。
我在数据库的这个特定部分(mysql innodb)中的模式是Plane和Steward。他们分享了Many2Many的关系。
使用Hibernate代码生成和Hibernate工具我生成了所有POJO类和映射文件(包括FlightHasStewardId和FlightHasSteward - > Mapped Composite ID)。
我现在想要在100号航班上宣读所有管家!
注意:阅读其他实体是有效的,只有M2M读取结果在ClassCastEX中。
这是我的输入:
FlightHasSteward的映射:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- Generated Jan 1, 2014 1:52:14 PM by Hibernate Tools 4.0.0 -->
<hibernate-mapping>
<class name="FlightHasSteward" table="Flight_has_Steward" catalog="project3flight">
<composite-id name="id" class="FlightHasStewardId">
<key-property name="flightIdflight" type="int">
<column name="Flight_idflight" />
</key-property>
<key-property name="stewardIdsteward" type="int">
<column name="Steward_idsteward" />
</key-property>
</composite-id>
</class>
</hibernate-mapping>
FlightHasSteward.java:
// default package
// Generated Jan 1, 2014 1:52:14 PM by Hibernate Tools 4.0.0
/**
* FlightHasSteward generated by hbm2java
*/
public class FlightHasSteward implements java.io.Serializable {
private FlightHasStewardId id;
public FlightHasSteward() {
}
public FlightHasSteward(FlightHasStewardId id) {
this.id = id;
}
public FlightHasStewardId getId() {
return this.id;
}
public void setId(FlightHasStewardId id) {
this.id = id;
}
}
FlightHasStewardId.java:
// default package
// Generated Jan 1, 2014 1:52:14 PM by Hibernate Tools 4.0.0
/**
* FlightHasStewardId generated by hbm2java
*/
public class FlightHasStewardId implements java.io.Serializable {
private int flightIdflight;
private int stewardIdsteward;
public FlightHasStewardId() {
}
public FlightHasStewardId(int flightIdflight, int stewardIdsteward) {
this.flightIdflight = flightIdflight;
this.stewardIdsteward = stewardIdsteward;
}
public int getFlightIdflight() {
return this.flightIdflight;
}
public void setFlightIdflight(int flightIdflight) {
this.flightIdflight = flightIdflight;
}
public int getStewardIdsteward() {
return this.stewardIdsteward;
}
public void setStewardIdsteward(int stewardIdsteward) {
this.stewardIdsteward = stewardIdsteward;
}
public boolean equals(Object other) {
if ((this == other))
return true;
if ((other == null))
return false;
if (!(other instanceof FlightHasStewardId))
return false;
FlightHasStewardId castOther = (FlightHasStewardId) other;
return (this.getFlightIdflight() == castOther.getFlightIdflight())
&& (this.getStewardIdsteward() == castOther.getStewardIdsteward());
}
public int hashCode() {
int result = 17;
result = 37 * result + this.getFlightIdflight();
result = 37 * result + this.getStewardIdsteward();
return result;
}
}
Reader.java(我的课程要读):
/* Method to READ all the stewards on specific flight */
public void getStewards(int flightid) {
Session session = factory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
// Query query = session
// .createQuery(" FROM FlightHasSteward as FHS LEFT JOIN FHS.Flight AS F ON F.idFlight = FHS.Flight_idFlight RIGHT JOIN FHS.Steward AS S ON S.idSteward = FHS.Steward_idSteward where FHS.Flight_idFlight = :code");
Query query = session.createQuery(" FROM FlightHasSteward where Flight_idFlight = :code");
query.setParameter("code", flightid);
List<FlightHasStewardId> result = query.list();
final List<FlightHasStewardId> res = (List<FlightHasStewardId>) query.list();
for (final FlightHasStewardId hall_calendar : res) {
System.out.println(hall_calendar.getStewardIdsteward());
}
tx.commit();
} catch (HibernateException e) {
if (tx != null)
tx.rollback();
e.printStackTrace();
} finally {
session.close();
}
}
令人不安的输出:
Exception in thread "main" java.lang.ClassCastException: FlightHasSteward cannot be cast to FlightHasStewardId
at Reader.getStewards(Reader.java:97)
at Manager.main(Manager.java:24)
感谢您对本案的任何帮助,谢谢大家。
答案 0 :(得分:0)
您是从FlightHasSteward
中选择的,因此结果List
将包含这些类型而不是FlightHasStewardId
Query query = session.createQuery(" FROM FlightHasSteward where Flight_idFlight = :code");
query.setParameter("code", flightid);
List<FlightHasSteward> result = query.list();
final List<FlightHasSteward> res = (List<FlightHasSteward>) query.list();
for (final FlightHasSteward steward : res) {
System.out.println(steward.getId().getStewardIdsteward());
}