I’m using IBM RAD 7.5 and WebSphere 7.
I have two entities with a OneToMany relationship (say, Customer and Order). When I create a Customer with an Order, both entities get persisted correctly. I have fetch=FetchType.EAGER but when I read the Customer entity it does not contain any of the Orders.
What am I doing wrong? Here’s some code …
@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; }
/*...*/
}
… and …
@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; }
/*...*/
}
What am I doing wrong?
Also, here what my DAO looks like…
@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;
}
Any advice or suggestions are greatly appreciated, thanks! Rob
Your class definitions all look fine to me. It would appear that the underlying JPA implementation is not pulling in associated objects via your query. You can pull them in explicitly like this:
That will join the customer on all their orders and return the objects properly populated.