0

我有一个 JPA,我正在尝试从 Oracle 数据库中检索数据。加载页面时出现以下错误:

java.lang.IllegalArgumentException:对象:[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] 不是已知的实体类型。

我唯一的实体类是 Order:

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

任何帮助将不胜感激。

编辑:

方法二:

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

1 回答 1

1

你不能坚持 whole 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);
        }
于 2013-10-15T15:32:13.470 回答