当我运行一个应该从数据库下载项目的方法时,将出现以下错误:
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: com.dke.ps.Tables.Item cannot be cast to com.dke.ps.Items.Item
at com.dke.ps.Shop.Shop.loadItems(Shop.java:161)
at com.dke.ps.Shop.Shop.init(Shop.java:122)
at com.dke.ps.Shop.Shop.<init>(Shop.java:60)
这是我的课程com.dke.ps.Tables.Item -
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.dke.ps.Tables;
import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.xml.bind.annotation.XmlRootElement;
/**
*
* @author michal
*/
@Entity
@Table(name = "item")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "Item.findAll", query = "SELECT i FROM Item i")
, @NamedQuery(name = "Item.findByItemid", query = "SELECT i FROM Item i WHERE i.itemid = :itemid")
, @NamedQuery(name = "Item.findByName", query = "SELECT i FROM Item i WHERE i.name = :name")
, @NamedQuery(name = "Item.findByDescription", query = "SELECT i FROM Item i WHERE i.description = :description")
, @NamedQuery(name = "Item.findByIcon", query = "SELECT i FROM Item i WHERE i.icon = :icon")
, @NamedQuery(name = "Item.findByType", query = "SELECT i FROM Item i WHERE i.type = :type")
, @NamedQuery(name = "Item.findByPrice", query = "SELECT i FROM Item i WHERE i.price = :price")})
public class Item implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "itemid")
private Integer itemid;
@Column(name = "name")
private String name;
@Column(name = "description")
private String description;
@Column(name = "icon")
private String icon;
@Column(name = "type")
private Integer type;
@Column(name = "price")
private Integer price;
public Item() {
}
public Item(Integer itemid) {
this.itemid = itemid;
}
public Integer getItemid() {
return itemid;
}
public void setItemid(Integer itemid) {
this.itemid = itemid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getIcon() {
return icon;
}
public void setIcon(String icon) {
this.icon = icon;
}
public Integer getType() {
return type;
}
public void setType(Integer type) {
this.type = type;
}
public Integer getPrice() {
return price;
}
public void setPrice(Integer price) {
this.price = price;
}
@Override
public int hashCode() {
int hash = 0;
hash += (itemid != null ? itemid.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof Item)) {
return false;
}
Item other = (Item) object;
if ((this.itemid == null && other.itemid != null) || (this.itemid != null && !this.itemid.equals(other.itemid))) {
return false;
}
return true;
}
@Override
public String toString() {
return "com.dke.ps.Tables.Item[ itemid=" + itemid + " ]";
}
}
还有我的com.dke.ps.Items.Item
**
* General class for an item containing common information sutch as id, name,
* description, path to an icon, price and type of an item.
* @author valecvit
* @author koresmi1
*/
public abstract class Item
{
/**
* Unique id of an item.
*/
public int itemid;
/**
* Name of an item.
*/
public String name;
/**
* Description of an item.
*/
public String description;
/**
* Relative path to item image.
*/
public String icon;
/**
* Type of an item.
*/
public int type;
/**
* Price of an item.
*/
public int price;
}
我真的不知道问题出在哪里。有人可以向我解释这个例外吗?
顺便说一句
是getDbItems()方法:
ArrayList<Item> dbItems = new ArrayList<>();
EntityTransaction entr=em.getTransaction();
entr.begin();
TypedQuery<Item> query = em.createQuery("SELECT i FROM Item i", Item.class);
dbItems = new ArrayList<Item>(query.getResultList());
em.getTransaction().commit();
此次展览中显示:
listOfDbItems = server.getDbItems();
listOfUsersItems = server.getPurchasedItems(user);
dlmItems.clear();
int numberOfItemsInDb = listOfDbItems.size();
for (int i = 0; i < numberOfItemsInDb; i++)
{
dlmItems.addElement((listOfDbItems.get(i)).name);
}
第一个类是通过Persistence - 数据库中的实体类和我创建的第二个类创建的。如果我将数组列表更改为ArrayList dbItems,则会抛出错误:找不到适合ArrayList的构造函数。构造函数ArrayList.ArrayList(int)不适用
感谢您的帮助!
答案 0 :(得分:0)
问题在于&#34; listOfDbItems.get(i)&#34;返回com.dke.ps.Tables.Item而&#34; dlmItems&#34;期待&#34; com.dke.ps.Items.Item&#34;。您应该更改DefaultListModel dlmItems = new DefaultListModel()以接受com.dke.ps.Items.Item,或者可以在添加到dlmItem之前将Tables.Item对象转换为Items.Item对象。