Questions about Managing Bidirectional Relationships with Ebean #3215
Replies: 1 comment 1 reply
-
This is saying that you want to use IDENTITY regardless of whether it is the best choice for the database being used. The recommendation is to not do that but instead not specify a strategy (leaving it to AUTO) or not have any
Using
Remove these everywhere. The defaults for fetch are actually correct for Ebean, the recommendation is to not explicitly specify
The recommendation is to not specify
Move the With that, we note the above that on the
I'd suggest that not using orphanRemoval is correct here because of the Use of
I do not like this way of thinking at all. When we fetch an Order back from the database, what we actually have is a SNAPSHOT of the Order AS AT a given time [where time is more transaction time and depends on transaction isolation level but typically it is the time that the query was started]. When we modify our Order setting some properties etc we are modifying our snapshot / version / copy of the Order in memory. When we save back to the database we are applying our changes to the database. We use optimistic locking to tell us when something else modified the database in the meantime. We use pessimistic locking to prevent something else from modifying the rows we want. Our application side in-memory Order and OrderItem are always only going to represent a snapshot / copy of the data as at a given time [but generally this is all fine and ok]. The mental model I'd prefer is something like, there are 2 cases: A) Read only queries. B) Transaction where we update and can also read A) Read only queries
B) Transaction where we update and can also read
We only need our in-memory state of Order, OrderItem to reflect the changes that we want to see when we save() them. We otherwise do not really care about their in-memory state beyond that.
This is the classic example where we ideally do: Fetch the OrderItem, change its Order, save the OrderItem ... to produce the Rather than Fetch Order A with all its OrderItems, Fetch Order B with all its OrderItems, remove the OrderItem from the Order A items, add it to the Order B items, save Order A, save Order B. This approach is a bad approach because it does a whole lot of unnecessary work. We should not take this approach just because we want our in-memory model to be in sync with the database etc. |
Beta Was this translation helpful? Give feedback.
-
Hi all. I am hoping for some inputs regarding best practices when modeling and working with relationships in ebean. Thank you for taking the time to look into this.
Please consider the following case.
One-to-Many
/Many-to-One
relationship betweenOrder
table (One) andOrderItem
table (Many) where eachOrderItem
record has a foreign key to anOrder
record.Order
.OrderItem
, including moving an item from oneOrder
to another.Order
, i.e. whatOrderItem
s are in theOrder
at any time. I would like the state to reflect even the changes toOrderItem
s that has not yet been saved back to databases.From my understanding, the mapped classes should look roughly as seen below.
Order.items
in sync in memory when updatingOrderItem
s without writing back to database and refreshing the beans? Is there any additional mappings I have to do to achieve this?Beta Was this translation helpful? Give feedback.
All reactions