我正在使用IBM RAD 7.5和WebSphere 7。
我有两个具有OneToMany关系的实体(例如,客户和订单)。当我使用订单创建客户时,两个实体都会被正确保留。我有fetch = FetchType.EAGER但是当我读取Customer实体时它不包含任何订单。
我做错了什么?这是一些代码......
@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;
}
非常感谢任何建议或建议,谢谢!罗布
答案 0 :(得分:1)
你的班级定义对我来说都很好。看起来基础JPA实现不会通过您的查询引入关联对象。您可以像这样明确地提取它们:
Query query = em.createQuery("select distinct c from Customer c left join fetch c.orderList");
customers.addAll(query.getResultLists());
这将使客户加入所有订单并返回正确填充的对象。