我有一个JPA,我试图从Oracle数据库中检索数据。加载页面时出现以下错误:
java.lang.IllegalArgumentException:Object:[com.ProEJT.Order@1f7be7b,com.ProEJT.Order @ 1fa8988,com.ProEJT.Order @ 15b44d6,com.ProEJT.Order @ 1044daf,com.ProEJT.Order @ 1882d18,com.ProEJT.Order @ 389922,com.ProEJT.Order @ 1cda81e,com.ProEJT.Order @ d86c58,com.ProEJT.Order @ b1404f,com.ProEJT.Order @ f5331a,com.ProEJT.Order @ 1ee145b, com.ProEJT.Order@17a687b,com.ProEJT.Order @ 16181be,com.ProEJT.Order @ 8d12e4,com.ProEJT.Order @ 1d35bf2,com.ProEJT.Order @ 1398044,com.ProEJT.Order @ 141d19,com。 ProEJT.Order@825459,com.ProEJT.Order @ 6fb3d6,com.ProEJT.Order @ 1602bbc,com.ProEJT.Order @ d75cf7,com.ProEJT.Order @ 1186cf9]不是已知的实体类型。
我唯一的实体类是订单:
package com.ProEJT;
import java.sql.Date;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity(name="Orders")
@Table(name="Orders")
public class Order
{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
int id;
private Date created;
private String sender;
private String receiver;
private String input;
private String output;
private String state;
public Order() {
}
public Order(int id, Date date, String sender, String receiver, String input, String output, String state)
{
this.id = id;
this.created = date;
this.sender = sender;
this.receiver = receiver;
this.input = input;
this.output = output;
this.state = state;
}
public int getId()
{
return id;
}
public void setId(int id)
{
this.id = id;
}
public Date getCreated()
{
return created;
}
public void setCreated(Date date)
{
this.created = date;
}
public String getSender()
{
return sender;
}
public void setSender(String sender)
{
this.sender = sender;
}
public String getReceiver()
{
return receiver;
}
public void setReceiver(String receiver)
{
this.receiver = receiver;
}
public String getInput()
{
return input;
}
public void setInput(String string)
{
this.input = string;
}
public String getOutput()
{
return output;
}
public void setOutput(String string)
{
this.output = string;
}
public String getState()
{
return state;
}
public void setState(String state)
{
this.state = state;
}
}
我用来检索数据的方法如下:
public List<Order> getOrderList()
{
factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
EntityManager em = factory.createEntityManager();
try
{
em.getTransaction().begin();
Query q = em.createQuery("Select o from Orders o");
@SuppressWarnings("unchecked")
List<Order> orders = q.getResultList();
List<Order> orders2 = new ArrayList<Order>();
Iterator<Order> iterator = orders.iterator();
while(iterator.hasNext())
{
Order order = (Order)iterator.next();
order.setId(this.id);
order.setCreated(this.created);
order.setSender(this.sender);
order.setReceiver(this.receiver);
order.setInput(this.input);
order.setOutput(this.output);
order.setState(this.state);
orders2.add(order);
}
em.persist(orders2);
em.getTransaction().commit();
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
em.close();
}
return orders;
}
非常感谢任何帮助。
编辑:
方法第2步:
public List<Order> getOrderList()
{
factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
EntityManager em = factory.createEntityManager();
em.getTransaction().begin();
Query q = em.createQuery("Select o from Order o");
@SuppressWarnings("unchecked")
List<Order> orders = q.getResultList();
em.persist(orders);
em.getTransaction().commit();
return orders;
}
答案 0 :(得分:1)
您无法保留整个orders2
,因为ArrayList
不是JPA感知类。可能你应该将每个元素保留在循环中:
while(iterator.hasNext())
{
Order order = (Order)iterator.next();
order.setId(this.id);
order.setCreated(this.created);
order.setSender(this.sender);
order.setReceiver(this.receiver);
order.setInput(this.input);
order.setOutput(this.output);
order.setState(this.state);
orders2.add(order);
em.persist(order);
}