我正在使用IBM RAD 7.5和WebSphere 7.我的JPA實體的OneToMany關係中缺少什麼?
我有兩個OneToMany關係的實體(比如Customer和Order)。當我使用訂單創建客戶時,兩個實體都會正確保持。我有fetch = FetchType.EAGER,但是當我讀取Customer實體時,它不包含任何Orders。
我在做什麼錯?下面是一些代碼...
@Entity
@Table(name="CC_CUSTOMER")
public class Customer implements Serializable {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private BigDecimal id;
@OneToMany(cascade=CascadeType.ALL,mappedBy="customer",fetch=FetchType.EAGER)
private List<Order> orderList;
public List<Order> getOrderList() { return this.orderList; }
public void setOrderList(List<Order> orderList) { this.orderList = orderList; }
/*...*/
}
......還有......
@Entity
@Table(name="CC_ORDER")
public class Order implements Serializable {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private long id;
@ManyToOne
@JoinColumn(name="customerid",nullable=false)
private Customer customer;
public Customer getCustomer() { return this.customer; }
public void setCustomer(Customer customer) { this.customer = customer; }
/*...*/
}
我在做什麼錯?
此外,這裏就是我的DAO的樣子......
@Stateless
public class CustomerDAO {
public List<Customer> getAll() {
List<Customer> customers = new ArrayList<Customer>();
EntityManager em = getEntityManager();
try {
Query query = em.createQuery("select c from Customer c");
customers.addAll(query.getResultLists());
} finally {
em.close();
}
}
return customers;
}
任何意見或建議,不勝感激,謝謝! Rob
您如何保存您的實體?...堅持訂單或堅持客戶? – 2012-03-01 17:49:55
我假設你在發佈你的代碼時在'getOrderList'方法中創建了一個類型? – Perception 2012-03-01 17:50:58
我堅持客戶。具體來說:'客戶c =新客戶();訂單o =新訂單(); 。c.getOrderList()添加(O); customerDAO.createCustomer(C);」 – 2012-03-01 17:53:36