When are JPAContainer changes committed to DB?

Following problem: I use two different JPAContainer on the same DB_table. One is to display a filtered view of the DB_table inside a Vaadin table component. The other container is used to modify items. I’m using the 2nd one to modify because I sometimes have to modify items that are not currently visible in the filtered list.

Problem: When I change an item in the 2nd container, the changes are not reflected to the first one. Why?
Example that illustrates:


class Customer {
    @OneToMany(mappedBy="customer")
    List<Order orders>;
}

class Order {
    @ManyToOne
    private Customer customer;
}

private void clearCustomerRefInOrder(Object orderId) {

    //clear customer reference in a specific order
    JPAContainer<Order> container = JPAContainerFactory.makeJndi(Order.class);
    container.getItem(orderId).getEntity().setCustomer(null);

    Sysout(container.getItem(orderId).getEntity().getCustomer()); //result: null
    container.commit(); //does not matter if I call this
    container.refresh();
    Sysout(container.getItem(orderId).getEntity().getCustomer()); //result: the customer!!
}

SO, somehow the changes I’m making through the new created container is not written to DB, and thus gone after executing .refresh(). Why?

I just found out that if I add an existing entity again to the table, ONLY then the object is updated in DB.


Order order = container.getItem(orderId).getEntity();
order.setCustomer(null);
container.addEntity(order);

so: is this actually intended to work this way??

If you directly modify the entity, the container can’t know it has been modified, this way to it nothing has changed and the commit will do nothing. And when you call refresh it will reload the original entity from the db (unchanged).

You should use properties to have the container persisting them.

container.getItem(id).getItemProperty("customer").setValue(null);