i have two entities Customer and Order (trivial setters and getters excluded)
@Entity
public class Customer {
@GeneratedValue
@Id
private int id;
@OneToMany
List<Order> orderList;
}
@Entity
public class Order {
@GeneratedValue
@Id
private int id;
@ManyToOne
Customer customer;
private boolean paid;
public Order(Customer customer) {
this.customer = customer;
customer.getOrderList().add(this)
}
}
Now i want to set ‘paid = true’ for all the orders of a given customer
Below query seem to do the trick, but I get a feeling it is innefficient and the fact that i stored the reverse relationship in Customer.orderList hints that there should be some other way to do this.
UPDATE Order o SET o.paid = true WHERE EXISTS
(SELECT c.orderList FROM Customer c WHERE o MEMBER OF c.orderList AND c = :customer)
I’m using container managed transactions, glassfish and javaDb. But I’d prefer if improvements could be done in JPA/JPQL domain and not specific to container or db.
private id;?? missed field typeAdd to
@OneToManyannotation,cascade = CascadeType.Allif you are using
cantainer managed transactionthentruewill be saved to DB