Repository navigation
feat(mybatis): add repository extension points for aggregates with child tables - #73
Merged
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Background
GenericRepositoryImplmaps one aggregate to one row:E → P, one mapper call,P → E. An aggregate with child entities, such as an order with its lines, did not fit. A subclass had to overridecreate,update,find,findAll,pageandremove, and re-implement the optimistic-lock check and the event publishing that the base class does around them. This came up when starting the order context of theddk-mallreference application.Changes
GenericRepositoryImpl, called around the root table's reads and writes in the same transaction:@TableField(exist = false). TheE → Pconverter fills it,afterInsert/afterUpdatewrite it to the child table, andafterLoadreads it back for theP → Econverter. The aggregate is still saved and loaded as a whole; the application layer does not see the child table.afterLoadreceives all roots of one query, so children can be fetched with a singleINquery instead of one query per root.Existing repositories are unaffected: the methods do nothing unless overridden.
背景
GenericRepositoryImpl把一个聚合映射成一行:E → P,调用一次 Mapper,P → E。带子实体的聚合(例如订单和订单行)放不进这个模型。子类只能把create、update、find、findAll、page、remove全部覆盖,再把基类围绕它们做的乐观锁校验和事件发布重写一遍。这个问题是在开始做ddk-mall参考应用的订单上下文时遇到的。改动
GenericRepositoryImpl新增四个默认为空的 protected 方法,在根表读写的前后调用,与根表在同一个事务里,调用时机见上文。@TableField(exist = false)的字段带上子对象:E → P的转换器填好它,afterInsert/afterUpdate把它写进子表,afterLoad把它读回来交给P → E的转换器。聚合仍然作为一个整体保存和加载,应用层看不到子表。afterLoad一次拿到本次查询的全部根对象,子表可以用一条IN查询取回,而不是每个根对象查一次。现有的仓储不受影响:这些方法不覆盖就什么都不做。