我昨天遇到了一个问题,就是我一对多中我有两个实体,一个是客户(customer)和订单(order)我做了映射,对客户的增,查询都可以但是对与删除操作怎么不删除不了,源代码如下:客户实体:
@Stateless
@Remote ({CustomerManager.class})
public class CustomerManagerBean implements CustomerManager {

@PersistenceContext
protected EntityManager em;

public void add(String name, String password, String email) {
Customer c = new Customer();
c.setName(name);
c.setPassword(password);
c.setEmail(email);

em.persist(c); } public void delete(Integer id) {
Customer customer = em.find(Customer.class, id);
/*Set s = customer.getOrders();
Iterator iterator = s.iterator(); 
while(iterator.hasNext()){
OrderItem item = (OrderItem)iterator.next();
em.remove(item);
}
customer.setOrders(null);*/
em.remove(customer);
} public List getAll() {
List l = em.createQuery("from Customer").getResultList();
em.clear();
return l;
} public boolean isLogin(String name, String password) {
Query q = em.createQuery("select c from Customer c where c.name = '"+name+"'and c.password = '"
+password+"'");
List l = q.getResultList();
if(l.size()>0)
return true;
return false;
} public Customer getById(String id) {
Customer c = em.find(Customer.class, new Integer(id));
c.getOrders().size();
return c;
}}订单实体:
@Entity
@Table(name = "order", schema = "dbo", catalog = "bookstore")
public class Order implements java.io.Serializable { // Fields private Integer id;
private Customer customer;
private Float totalPrice;
private Set<OrderItem> orderItems = new HashSet<OrderItem>(0); // Constructors /** default constructor */
public Order() {
} /** minimal constructor */
public Order(Integer id) {
this.id = id;
} /** full constructor */
public Order(Integer id, Customer customer, Float totalPrice,
Set<OrderItem> orderItems) {
this.id = id;
this.customer = customer;
this.totalPrice = totalPrice;
this.orderItems = orderItems;
} // Property accessors
@Id
@Column(name = "Id", unique = true, nullable = false)
@GeneratedValue
public Integer getId() {
return this.id;
} public void setId(Integer id) {
this.id = id;
} @ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "CustomerId")
public Customer getCustomer() {
return this.customer;
} public void setCustomer(Customer customer) {
this.customer = customer;
} @Column(name = "TotalPrice", precision = 53, scale = 0)
public Float getTotalPrice() {
return this.totalPrice;
} public void setTotalPrice(Float totalPrice) {
this.totalPrice = totalPrice;
} @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "order")
public Set<OrderItem> getOrderItems() {
return this.orderItems;
} public void setOrderItems(Set<OrderItem> orderItems) {
this.orderItems = orderItems;
}}调用客户的SeesionBean如下:
public void delete(Integer id) {
Customer customer = em.find(Customer.class, id);
em.remove(customer);
}