An entity object information synchronization method, device and storage medium
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-12-16
- Publication Date
- 2026-03-31
AI Technical Summary
[0024]本发明能解决实体模型字段重复,以及当业务发生变化导致字段发生变化时,开发人员只需要专注原始模型,而无需关心其他实体对象,其他实体对象可以在编译时同步原始模型字段变化,实现将原始模型的字段快速复制到增强模型中。能够很大程度减少重复代码和出现字段不一致等情况,同时可提高代码的清晰度、提升项目的可维护性和可扩展性。此外,能适用于所有IntelliJ IDE开发的Java项目,使用简单,只需添加几个注解即可使用。
Smart Images

Figure CN116303759B_ABST
Abstract
Description
Technical Field
[0001] This invention belongs to the field of computer development technology, and in particular relates to a method, device and storage medium for synchronizing entity object information. Background Technology
[0002] As programming becomes increasingly industrialized, various programming architectures have emerged (such as MVC, MVP, DDD, etc.). Along with these architectures, when developing Java projects, after code is layered, POJOs will contain entity objects such as VO, BO, PO, DO, DTO, and DP. Traditional development processes generally fall into two categories: first, defining the fields of the corresponding entity object PO in the table, and then copying these field codes into the VO, BO, DTO, and other entity objects. While defining a base entity object allows other entity objects to inherit from it, due to Java's single inheritance limitation, duplicate field definitions can still occur.
[0003] These entity objects often have a large number of duplicate fields. When business changes lead to the addition, modification, or deletion of fields, multiple entity objects need to be modified simultaneously. Otherwise, inconsistencies in field types or names can occur, a problem that becomes particularly prominent during periods of continuous requirement changes. Failure to maintain model data consistency can result in dirty data and bugs during use; these bugs are often subtle and difficult to troubleshoot.
[0004] Currently, there is no perfect technical solution to the above problems. For example, MapStruct, starting from the data, can solve the problem of data transformation by specifying source and target model fields when two objects have inconsistent fields. However, it still cannot solve problems such as duplicate fields in the entity model itself and potential inconsistencies between fields. Summary of the Invention
[0005] The technical objective of this invention is to provide a method, device, and storage medium for synchronizing entity object information, in order to solve the technical problem of a large number of repeatedly defined fields in entity objects.
[0006] To solve the above problems, the technical solution of the present invention is as follows:
[0007] A method for synchronizing entity object information includes the following steps:
[0008] Define the original model, which includes a base table, paginated queries, and fields / variable types with business meaning;
[0009] Define the enhancement model, which consists of the fields / variables that need to be added;
[0010] Obtain the field / variable definition syntax tree nodes of the original model, scan the annotations of the enhanced model through the annotation processor, modify the field / variable definition syntax tree nodes of the original model according to the attributes in the enhanced model annotations, and add the modified field / variable definition syntax tree nodes to the class definition syntax tree nodes of the enhanced model.
[0011] Specifically, the base table field / variable types include user creation, user update, creation time, and update time; the pagination query field / variable types include page number, page size, and fuzzy search keywords; and the field / variable types with business meaning include name, unique key, code, and logical deletion flag.
[0012] Preferably, three annotations are predefined before using the annotation processor: @Model, @ModelField, and @EnhanceModel.
[0013] When defining the original model, use @Model to annotate the class of the original model, and use @ModelField to annotate the fields of the original model;
[0014] When defining an enhancement model, use the @EnhanceModel annotation on the class of the enhancement model; if the fields of the enhancement model itself require field names or table field names, use the @ModelField annotation on the fields of the enhancement model itself.
[0015] The specific steps for defining the enhanced model are as follows: defining the field / variable types of the copied portion of the original model; defining the fields of the ignored portion of the original model; defining the Swagger annotation for whether to copy the fields of the original model; defining the JSR303 annotation for whether to copy the fields of the original model; and defining the annotation for not copying the fields of the copied portion of the original model.
[0016] Specifically, the annotator processor execution includes the following steps.
[0017] Scan the list of TypeElements annotated with @EnhanceMode, iterate through the list of TypeElements, and process the classes annotated with @EnhanceMode in turn;
[0018] Based on the `from` attribute under `@EnhanceMode`, the original model's TypeElement is obtained, and the list of VariationElements for the original model's fields is retrieved from the TypeElement. Fields to be ignored are filtered based on the `ignoreFields` attribute under `@EnhanceMode`. Swagger-related annotations are ignored based on the `enableSwagger` attribute under `@EnhanceMode`. JSR303 annotations are ignored based on the `enableJSR` attribute under `@EnhanceMode`. Certain specified annotations are ignored based on the `disableAnnotation` attribute under `@EnhanceMode`. Static variables for field names and table field names are generated based on the `@ModeField` annotation defined on the fields.
[0019] In the process of adding the modified field / variable definition syntax tree node to the class definition syntax tree node of the enhanced model, if there is a field with the same name between the original model and the enhanced model, it is determined that if the enhanced model has the same field name, the field of the enhanced model is used directly; if the enhanced model does not have the same field name, a compilation error occurs, and the definition of the field is redundant in the enhanced model.
[0020] More preferably, the method of synchronizing entity object information using an IntelliJ IDEA external plugin is supported, with the following steps: obtaining the original model through the from attribute; obtaining the fields with @Mode Field in the original model; adding the original model fields with @Mode Field to the enhanced model; and adding static variables for field names and table field names.
[0021] An electronic device includes a processor and a memory, the memory storing computer instructions executable on the processor, and the processor invoking the computer instructions in the memory to execute the entity object information synchronization method described above.
[0022] A storage medium for storing computer instructions, wherein, when the computer instructions are executed, the device on which the storage medium is located executes the entity object information synchronization method described above.
[0023] Because the present invention adopts the above technical solution, it has the following advantages and positive effects compared with the prior art:
[0024] This invention solves the problem of duplicate fields in entity models. When business changes cause field changes, developers only need to focus on the original model without worrying about other entity objects. Other entity objects can synchronize the original model's field changes at compile time, enabling rapid copying of fields from the original model to the enhanced model. This significantly reduces code duplication and inconsistencies, while improving code clarity, maintainability, and scalability. Furthermore, it is compatible with all Java projects developed using IntelliJ IDE, is easy to use (requiring only a few annotations), and can be used simply by adding a few annotations. Attached Figure Description
[0025] Various other advantages and benefits will become apparent to those skilled in the art upon reading the following detailed description of preferred embodiments. The accompanying drawings are for illustrative purposes only and are not intended to limit the invention.
[0026] Figure 1 This is a flowchart of an entity object information synchronization method according to the present invention.
[0027] Figure 2 This is a flowchart illustrating the definition model of the present invention;
[0028] Figure 3 This is a flowchart illustrating the implementation of the annotation processor of the present invention.
[0029] Figure 4 This is a flowchart illustrating the implementation process of the plugin of the present invention;
[0030] Figure 5 This is a schematic diagram of the business model of the present invention;
[0031] Figure 6 This is a schematic diagram of another business model of the present invention. Detailed Implementation
[0032] To more clearly illustrate the technical solutions in the embodiments of the present invention or the prior art, the specific implementation methods of the present invention will be described below with reference to the accompanying drawings. Obviously, the accompanying drawings described below are merely some embodiments of the present invention. For those skilled in the art, other drawings and other implementation methods can be obtained based on these drawings without any creative effort.
[0033] To keep the drawings concise, only the parts relevant to the invention are shown schematically in each figure, and they do not represent the actual structure of the product. Furthermore, for ease of understanding, in some figures, only one of components with the same structure or function is shown schematically, or only one is labeled. In this document, "one" can mean not only "only one" but also "more than one".
[0034] The present invention provides a method, apparatus, and storage medium for synchronizing entity object information, in conjunction with the accompanying drawings and specific embodiments. The advantages and features of the present invention will become clearer from the following description and claims.
[0035] Example
[0036] See Figures 1 to 5 This embodiment provides a method for synchronizing entity object information, enabling the enhanced model to automatically copy fields from the original model. When fields in the original model change, the enhanced model can also change synchronously without modification. The method includes the following steps:
[0037] First, see Figure 1 and Figure 2 First, an original model needs to be defined. When defining the original model, the `@Model` annotation is used on the class of the original model, and the `@ModelField` annotation is used on the fields of the original model. After adding `@ModelField`, when the enhanced model depends on the original model, in addition to copying the fields, static variables for the field names and table field names are generated. The original model is the defined base class, which is itself a POJO, and its fields can be copied by the enhanced model. Specifically, the fields in the original model can be some basic table field types, including but not limited to: `id`, `createUser` (create user), `updateUser` (update user), `createDate` (create time), `updateDate` (update time), etc. They can also be included in paginated data queries such as `pageNo` (page number), `pageSize` (page size), `searchKey` (fuzzy search keyword), etc. Additionally, they can be some basic types with business meaning, including but not limited to: `name` (name), `key` (unique key), `code` (encoding), `deleteFlag` (logical deletion flag), etc. In this embodiment, the original model can be any POJO type. As long as the developers believe that there are properties that can be reused by other POJOs, it can be defined as the original model.
[0038] Next, see Figure 1 and Figure 2Define an enhancement model, which represents the fields / variables that need to be added. The enhancement model itself is a POJO, but it can be modified using the Annotation Processor (APT) tool to change the Abstract Syntax Tree (AST), copying the fields defined in the original model into the enhancement model. Simultaneously, the original fields of the enhancement model are unaffected by compilation. When defining the enhancement model, use the `@EnhanceModel` annotation on the class of the enhancement model. If the enhancement model's own fields require field names or table field names, then use the `@ModelField` annotation on the fields themselves.
[0039] Specifically, the steps include: defining the field / variable types to copy from the original model; defining the fields to ignore from the original model; defining whether to copy the fields of the original model using Swagger annotations; defining whether to copy the fields of the original model using JSR303 annotations; and defining whether to copy the fields of the original model using annotations that do not copy.
[0040] The @EnhanceMode attribute is explained in Table 1.
[0041] Table 1
[0042]
[0043] See Figure 1 and Figure 3 During project compilation, the annotation processor (APT) scans the code for annotations that enhance the model. Based on the attributes in the enhanced model annotations, it first obtains the field / variable definition syntax tree nodes of the original model, then modifies the original model's field / variable definition syntax tree nodes according to the annotation attribute definitions, and finally adds the modified syntax tree nodes to the class definition syntax tree nodes of the enhanced model. Fields added after APT processing support annotations such as Lombok and MapStruct. For example, adding Lombok's @Getter and @Setter annotations will generate corresponding getter() and setter() methods for the new fields. Similarly, adding MapStruct's @Mapper annotation will generate new field transformations during object data transformations.
[0044] The original model and the enhanced model can come from different modules, as long as they can be accessed via Maven dependencies. If, during the process of adding modified field / variable definition syntax tree nodes to the class definition syntax tree nodes of the enhanced model, there are fields with the same name in the original model and the enhanced model, then if the enhanced model has the same field name, the field from the enhanced model will be used directly, without copying the field from the original model. If the enhanced model does not have the same field name, but multiple dependent original models have the same field name, a compilation error will occur, and the user can redundantly define that field in the enhanced model.
[0045] Specifically, see Figure 3 APT can be used to scan, compile, and process annotations at compile time. The detailed steps are as follows: Scan the list of TypeElements annotated with `@EnhanceMode`, iterate through the TypeElement list, and process classes annotated with `@EnhanceMode` in sequence; based on the `from` attribute under `@EnhanceMode`, obtain the TypeElement of the original model, and retrieve the list of VariationElements for the original model fields from the TypeElements; filter out ignored fields based on the `ignoreFields` attribute under `@EnhanceMode`; determine whether to ignore Swagger-related annotations based on the `enableSwagger` attribute under `@EnhanceMode`; determine whether to ignore JSR303 annotations based on the `enableJSR` attribute under `@EnhanceMode`; ignore certain specified annotations based on the `disableAnnotation` attribute under `@EnhanceMode`; generate static variables for field names and table field names based on the `@ModeField` annotation defined on the fields.
[0046] For a better option, see Figure 4 Because APT scans, compiles, and processes Java annotations at compile time, it cannot recognize and process annotations such as @Model, @ModelField, and @EnhanceModel when writing code in IntelliJ IDEA. To support developers in normal development within IntelliJ IDEA, this patent provides a plugin and has released it on the plugin marketplace. Developers only need to download and install the plugin onto their IntelliJ IDEA installation. After the plugin is installed in IntelliJ IDEA, the plugin's processing logic steps are as follows: obtain the original model through the `from` attribute; obtain the fields in the original model that contain @ModelField; add the original model fields containing @ModelField to the enhanced model; add static variables for field names and table field names. Simultaneously, to allow developers to see the corresponding POJO fields and variables in the IntelliJ IDEA StructureToolwindow, we implemented the IntelliJ IDEA StructureViewExtension interface, with processing logic consistent with the plugin's processing logic.
[0047] See Figure 5 and Figure 6 The following example illustrates a practical application of this embodiment. For instance, in the e-commerce industry, parameters need to be used to send multiple services such as products, orders, and instances, resulting in a large number of repetitive parameters. Furthermore, due to the need for entity object conversion between different domains, there are also numerous parameter assignment operations. Figure 5 As shown, storing large fields such as detailed product descriptions separately improves query performance. In such a scenario, the product details should include fields for basic product information and extended product information. For example... Figure 6 As shown, when querying data, in addition to the fields originating from the basic product information and extended product information, pagination basic information may also be required. Therefore, this embodiment supports fields from multiple original models for the enhanced model.
[0048] First, define the original model and determine which product fields can be used as original model field information:
[0049] 1. Basic product information: Products have a product ID (itemID), a product title (title), and a product price (price). This basic information may be stored in the product table.
[0050] 2. Product Extended Information: To improve query performance, large fields such as product descriptions are stored separately. This information may be stored in the product extended table.
[0051] Therefore, developers can define two primitive models for a product: ItemBasic and ItemExtra.
[0052] In addition, users need pagination information when querying, so it is also necessary to define the original pagination model BasePage.
[0053] Next, regarding the definition and processing of the enhanced model, when developing product business logic, developers need to use the corresponding DTO entity object, defined as ItemDTO. ItemDTO requires the field information under ItemBasic and ItemExtra, but does not require the corresponding Swagger annotations, JSR303 annotations, or @Length annotations on the fields, nor does it require the extraField information under ItemExtra. When a user initiates a query request, the defined ItemQuery entity, in addition to requiring the field information under ItemBasic and ItemExtra, also needs the corresponding pagination information. When developing order business logic, the order DTO entity object OrderDTO may need the field information under ItemBasic, but it also has its own field information, such as order number and order name.
[0054] As shown in the example above, when a field needs to be changed, the title needs to be changed to name; or when requirements change, a unique item number (itemNo) needs to be added to the product, and the order also needs to record the product number. In this embodiment, only the original model ItemBasic needs to be modified by changing the title to name and adding a new itemNo field; the enhanced model will automatically synchronize the field changes.
[0055] In this embodiment, the fields generated by the enhanced model support both Lombok and MapStruct functionalities. Therefore, developers can also apply Lombok and MapStruct annotations when defining enhanced models. Lombok application: Add the @Data annotation to the ItemDetailQuery enhanced model. MapStruct application: Fields generated by the enhanced model can also use MapStruct for attribute assignment. Furthermore, if inconsistent field names occur, the field names can be retrieved by generating static variables in this embodiment.
[0056] Suppose there's a requirement where, when a user places an order, there's no place to input the order name, so we want to assign the product name to the order name. When performing data transformation in MapStruct, this needs to be done by writing code. When writing the code, we can obtain the field names using `ItemDTO.itemName_field` and `OrderDTO.orderName_field`.
[0057] Furthermore, this embodiment also provides an electronic device, which includes a processor and a memory. The memory stores computer instructions that can be executed on the processor, and the processor is used to call the computer instructions in the memory to execute the entity object information synchronization method as described above.
[0058] In addition, this embodiment also provides a storage medium for storing computer instructions, wherein the computer instructions control the device where the storage medium is located to execute the entity object information synchronization method described above when the computer instructions are running.
[0059] The embodiments of the present invention have been described in detail above with reference to the accompanying drawings, but the present invention is not limited to the above embodiments. Even if various changes are made to the present invention, if these changes fall within the scope of the claims of the present invention and their equivalents, they shall still fall within the protection scope of the present invention.
Claims
1. An entity object information synchronization method, characterized by, The method comprises the following steps: defining an original model, which is a basic table, a paging query, and a field / variable type with business meaning; defining an enhanced model, which is a field / variable that needs to be added; obtaining a field / variable definition syntax tree node of the original model, scanning an annotation of the enhanced model through an annotation processor, modifying the field / variable definition syntax tree node of the original model according to an attribute in the annotation of the enhanced model, and adding the modified field / variable definition syntax tree node to a class definition syntax tree node of the enhanced model; wherein, before using the annotation processor, three annotations are defined in advance, namely, @Model, @Model Field, and @Enhance Model; when the original model is defined, the @Model is annotated on the class of the original model, and the @Model Field is annotated on the field of the original model; when the enhanced model is defined, the @Enhance Model is annotated on the class of the enhanced model; if the field name or the table field name of the field of the enhanced model is needed, the @Model Field is annotated on the field of the enhanced model; the definition of the enhanced model specifically comprises the following steps: defining a field / variable type of a part of the original model to be copied; defining a field of a part of the original model to be ignored; defining whether to copy the swagger annotation of the field of the original model; defining whether to copy the JSR303 annotation of the field of the original model; defining that a part of the field annotation of the original model is not copied; the annotation processor comprises the following steps when executed: scanning a Type Element list with the @Enhance Model annotation, traversing the Type Element list, and processing the classes with the @Enhance Model annotation one by one; based on the from attribute under the @Enhance Model, obtaining the Type Element of the original model, obtaining a Variable Element list of the field of the original model from the Type Element, filtering the ignored fields according to the ignoreFields attribute under the @Enhance Model, judging whether to ignore the swagger-related annotations according to the enable Swagger attribute under the @Enhance Model, judging whether to ignore the JSR303 annotations according to the enable JSR attribute under the @Enhance Model, ignoring a part of the specified annotations according to the disable Annotation attribute under the @Enhance Model, and generating a field name static variable and a table field name static variable according to the @Model Field annotation defined on the field.
2. The entity object information synchronization method of claim 1, wherein The base table field / variable type includes creating user, updating user, creating time and updating time; The paging query field / variable type includes page number, page size and fuzzy search keyword; The field / variable type with business meaning includes name, unique key, code and logical deletion flag.
3. The method of claim 1, wherein If there is a same name field between the original model and the enhanced model in the process of adding the modified field / variable definition syntax tree node to the class definition syntax tree node of the enhanced model, it is judged that if the enhanced model has the same field name, the field of the enhanced model is directly used; if the enhanced model does not have the same field name, and there is a same name field in the plurality of original models, a compilation error is generated, and the definition of the redundant field of the enhanced model is compiled.
4. The method of claim 1, wherein, Further preferably, the entity object information synchronization method is supported to be implemented by using an external plug-in of IntelliJ IDEA, and the steps are as follows: the original model is obtained through a from attribute; the field with @Model Field in the original model is obtained; the original model field with @Model Field is added to the enhanced model; A field name static variable and a table field name static variable are added.
5. An electronic device, comprising: The electronic device includes a processor and a memory, the memory stores computer instructions executable on the processor, and the processor is configured to call the computer instructions in the memory to execute the entity object information synchronization method in any one of claims 1-4.
6. A storage medium, characterized by The storage medium is configured to store computer instructions, wherein the computer instructions control the device where the storage medium is located to execute the entity object information synchronization method in any one of claims 1-4 when the computer instructions are executed.
Citation Information
Patent Citations
Method and device for realizing domain driving model
CN107741847A
Code transformation
US20090064091A1