我正在使用Eclipse juno IDE。
我有一个客户端 - 服务器应用程序。在服务器端我有一个实体(旅行) 我有另一个处理JPA查询的类。我正在收到数据库中的数据 但是当我试图将它作为向量发送给客户端时,我得到了一个例外 客户端,“无法将pack.db.Travels转换为java.util.vector”
这是我的代码: 的实体:
package pack.db;
import java.io.Serializable;
import java.sql.Date;
import java.sql.Time;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Travels implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE)
@Column(name="id")
private int id;
@Column(name="taxi_number")
private String taxiNumber;
@Column(name="travel_date")
private Date travelDate;
@Column(name="travel_time")
private Time travelTime;
@Column(name="cost")
private Double travelCost;
public Travels() {
super();
}
public void setNumber(String number)
{
this.taxiNumber = number;
}
public void setDate(Date date)
{
this.travelDate = date;
}
public void setTime(Time time)
{
this.travelTime = time;
}
public void setCost(Double cost)
{
this.travelCost = cost;
}
}
QueryClass
public Vector retrieveAllTravelsData(Date[] travelDate, Time[] travelTime) {
List<Object[]> allTravels = (List<Object[]>)em.createQuery("SELECT t FROM Travels t WHERE t.travelDate between ?1 and ?2 and " +
"t.travelTime between ?3 and ?4")
.setParameter(1, travelDate[0])
.setParameter(2, travelDate[1])
.setParameter(3, travelTime[0])
.setParameter(4, travelTime[1]).getResultList();
return (Vector) allTravels;
}
所以我想做的是将“allTravels”作为向量发送到客户端,因为 我需要在客户端填充JTABLE。所以我试着投出返回数据 从查询到OBJECT [](因为JTABLE的构造函数需要对象[] []为行)并发送它..但我仍然得到客户端的异常,说 “无法将pack.db.Travel转换为java.util.Vector”..我不认为我需要添加 客户端的旅行实体..那么如何将数据发送给客户?
更具体一点..我有这个代码与JDBC实现
public Vector retrieveAllTravelsData(Date[] travelDate, Time[] travelTime) {
Vector rows_data = new Vector();
String sql = "SELECT * FROM taxis.travels " + " WHERE travel_date BETWEEN ? AND ? AND travel_time BETWEEN ? AND ?";
try {
statement = (PreparedStatement) connection.prepareStatement(sql);
statement.setDate(1, travelDate[0]);
statement.setDate(2, travelDate[1]);
statement.setTime(3, travelTime[0]);
statement.setTime(4, travelTime[1]);
rs = statement.executeQuery();
ResultSetMetaData meta = rs.getMetaData();
int cols_count = meta.getColumnCount();
while (rs.next()) {
Vector record = new Vector();
for (int i = 0; i < cols_count; i++) {
record.add(rs.getString(i+1));
}
rows_data.addElement(record);
}
} catch (SQLException e) {
while (e != null) {
e.printStackTrace();
e = e.getNextException();
}
}
return rows_data;
在这里,我可以从每列中获取每个列数据,将其保存为记录,然后将其放入Vector中。那么JPA如何实现呢?有可能吗?
答案 0 :(得分:2)
将对象转换为另一个类并不会神奇地改变对象的类型。它只允许将它作为一个更具体的类引用。因此,仅当列表确实是Vector时,才能将List转换为Vector。
getResultList()
返回List
。这就是javadoc所说的。返回的具体类取决于JPA提供程序,但我非常确定它们都没有返回Vector
,因为Vector是一个不再使用的类,因为Java 1.2。
此外,此特定查询不会返回Object[]
,而是Travels
的实例(应该命名为Travel
,BTW)。
所以方法应该是:
public List<Travel> retrieveAllTravelsData(Date[] travelDate, Time[] travelTime) {
List<Travel> allTravels = (List<Travel>) em.createQuery("SELECT t FROM Travel t WHERE t.travelDate between ?1 and ?2 and " +
"t.travelTime between ?3 and ?4")
.setParameter(1, travelDate[0])
.setParameter(2, travelDate[1])
.setParameter(3, travelTime[0])
.setParameter(4, travelTime[1]).getResultList();
return allTravels;
}
服务器不应该关心客户端需要Vector来满足旧的Swing类。如果你真的需要在客户端使用Vector,那么从返回的列表中创建一个:
Vector<Travel> travelsAsVector = new Vector(travelsAsList);
答案 1 :(得分:0)
好的,我这样解决:
public Vector retrieveAllTravelsData(Date[] travelDate, Time[] travelTime) {
javax.persistence.Query q = (javax.persistence.Query)em.createNativeQuery("SELECT *
from Travels WHERE travel_date between ?1 and ?2")
.setParameter(1, travelDate[0])
.setParameter(2, travelDate[1])
.setParameter(3, travelTime[0])
.setParameter(4, travelTime[1]);
List<Object[]> result = (List<Object[]>)q.getResultList();
Vector rows = new Vector();
for (int i = 0 ; i < result.size(); i++)
{
Vector rec = new Vector();
for (int j = 0; j < columnCount; j++)
{
rec.add(result.get(i)[j].toString()); // returns the specific column value and add it to the vector
}
rows.addElement(rec);
}
return rows;
}