Most of the entities that are related to Order (like FulfillmentGroups) are marked to cascade operations. For instance, this is the annotation for the list of fulfillment groups:
Code: Select all
@OneToMany(mappedBy = "order", targetEntity = FulfillmentGroupImpl.class, cascade = {CascadeType.ALL})
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE, region="blOrderElements")
@AdminPresentationCollection(friendlyName="OrderImpl_Fulfillment_Groups",
tab = Presentation.Tab.Name.FulfillmentGroups, tabOrder = Presentation.Tab.Order.FulfillmentGroups)
protected List<FulfillmentGroup> fulfillmentGroups = new ArrayList<FulfillmentGroup>();
Because of that cascade = {CascadeType.ALL}, this means you can do this:
Code: Select all
FulfillmentGroup fg = fulfillmentGroupService.createEmptyFulfillmentGroup();
cart.getFulfillmentGroups().add(fg);
orderService.save(cart);
And then that fulfillment group will be persisted along with the relationship to the cart. It doesn't really hurt anything if you do the multiple saves (save the fulfillment group, add it to the order, save the order) but it's not really necessary in this case.
Basically, you have to make sure that you are not only saving the entity but also the relationship to the entities. When you add a fulfillment group to an order, you have to save that relationship as well. By the way, since Fulfillment Group has the reverse relationship to Order you can also do this:
Code: Select all
FulfillmentGroup fg = fulfillmentGroupService.createEmptyFulfillmentGroup();
fg.setOrder(cart);
fulfillmentGroupService.save(fg);
Which will actually persist the relationship to the database. However, if you then try to get the list of fulfillment groups for an order and it has already been retrieved, that new fulfillment group won't show up unless you go back to the database to get it.
Anyway, lots of options here it just mainly depends on what makes sense for you. Let us know if we can be of any other help!