A metadata model-based code generation method, system and storage medium
By co-optimizing based on metadata models and AI big data models, the flexibility and security issues of existing code generation tools in enterprise applications are resolved, achieving efficient and secure code generation that is suitable for seamless dynamic adaptation and end-to-end quality control in enterprise applications.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- NINGBO HOLLYSYS INTELLIGENT TECH CO LTD
- Filing Date
- 2026-03-25
- Publication Date
- 2026-07-03
Smart Images

Figure CN122331875A_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of computer technology, and in particular to a code generation method, system, and storage medium based on a metadata model. Background Technology
[0002] In existing technologies, code generation tools are mainly divided into two categories: traditional template-driven tools and basic AI-assisted tools. Traditional template-driven tools rely heavily on fixed template files, lack flexibility, and struggle to dynamically adapt to diverse business needs. Furthermore, they fail to systematically integrate descriptions, structures, and management metadata, resulting in low cross-project reusability. Their ability to handle complex business logic such as multi-table joins and permission checks is also weak. On the other hand, while AI-assisted code generation tools have achieved the conversion from natural language to code in recent years, they face numerous bottlenecks in enterprise-level application deployment: insufficient context awareness can lead to locally correct but globally logically conflicting code; poor domain adaptability to internally developed middleware or domestically produced databases, and high additional fine-tuning and training costs; a lack of built-in verification of code development standards and security, resulting in extremely high manual review costs; and limitations imposed by the enterprise intranet environment hinder direct calls to external large-scale model APIs, while local deployment of large models faces extremely high computational resource barriers. Summary of the Invention
[0003] To address the above problems, this invention proposes a code generation method based on a metadata model, specifically including: Obtain the three-layer metadata model of the target application and a preset template file; parse the three-layer metadata model using a metadata parser to extract data model information, and fill the template file with the data model information to generate an initial code skeleton; call the AI big model to perform collaborative optimization on the initial code skeleton to generate target business code; perform dual-engine security verification on the target business code based on a preset security rule base and the AI big model; after the dual-engine security verification passes, output the final target code file. Furthermore, the three-layer metadata model includes descriptive metadata, structural metadata, and management metadata; wherein, the descriptive metadata is used to define code development specifications and development framework constraints; the structural metadata is used to store database table relationships and entity attributes; and the management metadata is used to record the scheduling strategy of code generation tasks and template version control information.
[0004] Furthermore, the step of parsing the three-layer metadata model based on the metadata parser to extract data model information and filling the data model information into the template file to generate an initial code skeleton includes: dynamically decoupling the template file from the three-layer metadata model; The three-layer metadata model is decomposed into structured data variables through a unified parsing interface; Dynamically load a pluggable template file that matches the current application scenario, and render the structured data variables into the pluggable template file to generate the initial code skeleton.
[0005] Furthermore, the process of calling the AI big model to collaboratively optimize the initial code skeleton and generate target business code includes a two-stage collaborative optimization process: basic stage optimization: the AI big model performs redundant code removal and code formatting alignment operations on the initial code skeleton based on the description metadata to generate semi-finished code; logic enhancement stage: natural language instructions are obtained, and the AI big model combines the natural language instructions with the entity relationships defined in the structure metadata to generate business logic code and inject it into the semi-finished code to generate the target business code.
[0006] Furthermore, the business logic code generated during the logic enhancement phase includes at least index design logic, multi-table join query logic, and caching strategy logic.
[0007] Furthermore, the process of calling the AI big model to collaboratively optimize the initial code skeleton and generate the target business code also includes: constructing global context-aware information for the AI big model based on the three-layer metadata model; the AI big model combining the global context-aware information to avoid code conflict, so as to avoid errors in the generated target business code that are locally correct but globally logically conflicting.
[0008] Furthermore, the dual-engine security verification of the target business code based on the preset security rule base and the AI big model includes: extracting enterprise security specifications recorded in the management metadata and constructing the preset security rule base; triggering code splicing risk detection of the target business code through the template engine based on the preset security rule base; and synchronously calling the AI big model to perform business logic-level security vulnerability scanning and permission verification missing detection on the target business code.
[0009] Furthermore, before calling the AI big model to collaboratively optimize the initial code skeleton, the process includes: obtaining metadata description information of the target enterprise's self-developed framework; using the metadata description information as a lightweight feature to fine-tune the big language model deployed in the enterprise's intranet environment, so that the AI big model can adapt to the unique development framework in the intranet environment.
[0010] The second aspect of this application discloses a system, including: a metadata parsing module, used to obtain a three-layer metadata model of a target application and a preset template file, and to parse the three-layer metadata model to extract data model information; The core engine module is used to populate the data model information into the template file and generate the initial code skeleton; The AI optimization module is used to call a large AI model to collaboratively optimize the initial code skeleton and generate target business code. The security verification module is used to perform dual-engine security verification on the target business code based on a preset security rule base and the AI big model; The code output module is used to output the final target code file after the dual-engine security verification passes.
[0011] A third aspect of this application discloses a computer-readable storage medium having a computer program stored thereon, characterized in that the computer program, when executed by a processor, implements the above-described method.
[0012] In summary, the code generation method and system based on a metadata model provided by this invention integrates a three-layer metadata model (description, structure, and management) and dynamically decouples it from a plug-in template engine. This not only breaks through the technical bottlenecks of poor flexibility and low reusability of traditional static templates, achieving seamless dynamic adaptation across business scenarios and zero-interruption hot updates, but also innovatively introduces a two-stage collaborative optimization mechanism between the AI large model and the metadata model. Metadata provides AI with accurate global context and lightweight enterprise framework constraints, enabling AI to accurately generate complex business logic such as multi-table relationships and caching strategies using natural language, effectively avoiding errors where the generated code is partially correct but globally conflicting. Simultaneously, this invention embeds a dual-engine security verification mechanism based on a metadata rule base and AI vulnerability scanning into the generation pipeline, achieving end-to-end quality control and significantly improving the standardization and security compliance rates of the generated code. This fundamentally overcomes the contradiction between the insufficient flexibility of traditional tools and the uncontrollable quality of existing AI-assisted tools, significantly reducing the cost of manual code supplementation and review, and achieving a comprehensive technical effect of cost reduction and efficiency improvement in the rapid construction of enterprise-level applications. Attached Figure Description
[0013] To more clearly illustrate the specific embodiments of the present invention or the technical solutions in the prior art, the drawings used in the description of the specific embodiments or the prior art will be briefly introduced below. Obviously, the drawings described below are some embodiments of the present invention. For those skilled in the art, other drawings can be obtained from these drawings without creative effort.
[0014] Figure 1 This is a flowchart provided in an exemplary embodiment of the present invention; Figure 2 This is a schematic diagram of the logical architecture of the code generator provided in an exemplary embodiment of the present invention; Figure 3 This is an architectural schematic diagram provided in an exemplary embodiment of the present invention; Detailed Implementation
[0015] To make the objectives, technical solutions, and advantages of the embodiments of the present invention clearer, the technical solutions of the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of the present invention, not all embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of the present invention.
[0016] This embodiment provides a code generation method based on a metadata model, including: Step S10: Obtain the three-tier metadata model of the target application and the preset template file; Step S20: Parse the three-layer metadata model using the metadata parser to extract data model information, and fill the template file with the data model information to generate the initial code skeleton; Step S30: Call the AI large model to perform collaborative optimization on the initial code skeleton to generate the target business code; Step S40: Perform dual-engine security verification on the target business code based on the preset security rule base and the AI big model; after the dual-engine security verification passes, output the final target code file.
[0017] In an enterprise-level low-code development platform, developers need to generate an "order management" module. The system first reads the module's three-layer metadata model and a pre-defined Spring Boot template file. The metadata parser extracts the attribute information of the order entity and populates it into the template, generating a preliminary CRUD (Create, Read, Update, Delete) code skeleton. Subsequently, the system calls an AI-powered big data model to remove redundancy and format the code skeleton based on the metadata, and injects business logic to generate the target business code. Finally, the system performs dual-engine security verification on the generated code using a security rule base and the AI-powered big data model. After confirming that it is error-free, it outputs the final .java code file. This method overcomes the shortcomings of traditional template tools, such as insufficient flexibility and low reusability, and solves the problems of poor domain compatibility and uncontrollable quality in existing AI tools. It improves the efficiency of enterprise-level application code generation from a global perspective and reduces overall development costs.
[0018] Furthermore, in some embodiments, the three-layer metadata model includes descriptive metadata, structural metadata, and management metadata; The descriptive metadata is used to define code development standards and development framework constraints; the structural metadata is used to store database table relationships and entity attributes; and the management metadata is used to record the scheduling strategy for code generation tasks and template version control information. This method achieves systematic management of metadata, solving the problems of scattered metadata and low cross-project reusability in traditional tools. By integrating descriptive, structural, and management metadata through a unified architecture, the reusability bottleneck of traditional tools is overcome, enabling generation strategies to flow efficiently between different projects.
[0019] Furthermore, in some embodiments, the step of parsing the three-layer metadata model using a metadata parser to extract data model information and then filling the template file with the data model information to generate an initial code skeleton includes: The template file is dynamically decoupled from the three-layer metadata model; The three-layer metadata model is decomposed into structured data variables through a unified parsing interface; Dynamically load a pluggable template file that matches the current application scenario, and render the structured data variables into the pluggable template file to generate the initial code skeleton.
[0020] When an enterprise's system architecture upgrades from a monolithic application to a microservices architecture, developers do not need to modify the underlying Java code. They only need to dynamically load custom plug-in templates adapted to the microservices architecture into the system. The system decomposes metadata into structured variables through a unified parsing interface and renders them directly into the new microservice templates. Simultaneously, it achieves zero-disruption hot updates, enabling dynamic adjustments to code generation strategies in the production environment without restarting the entire system, significantly improving the adaptation efficiency for switching between multiple scenarios.
[0021] Furthermore, in some embodiments, the invocation of the AI large model to collaboratively optimize the initial code skeleton and generate target business code includes a two-stage collaborative optimization process: Basic stage optimization: The AI large model performs redundant code removal and code formatting alignment operations on the initial code skeleton based on the description metadata to generate semi-finished code; Logic enhancement stage: Natural language instructions are obtained. The AI big model combines the natural language instructions with the entity relationships defined in the structural metadata to generate business logic code and inject it into the semi-finished code to generate the target business code.
[0022] The code generation pipeline performs a two-stage process: In the basic optimization stage, the AI model reads the code specifications from the descriptive metadata, automatically merges duplicate utility classes generated from templates, and aligns code indentation and blank lines. In the logic enhancement stage, developers input the natural language instruction "Add logic for sorting by price to the product module," and the AI model, combining the field attributes from the structural metadata, automatically generates a complex query method containing price sorting logic in the semi-finished code and supplements it with corresponding validation code.
[0023] The above features effectively solve the serious shortcomings of traditional tools that cannot handle complex business logic (such as multi-table relationships) and require manual addition of a large amount of logic in the generated code, thus significantly improving the actual adoption rate of the generated code.
[0024] Furthermore, in some embodiments, the business logic code generated during the logic enhancement phase includes at least index design logic, multi-table join query logic, and caching strategy logic. This achieves a high degree of automated generation of complex logic code, greatly reducing the workload of manually supplementing the code.
[0025] Furthermore, in some embodiments, the process of calling the AI large model to collaboratively optimize the initial code skeleton and generate the target business code further includes: Based on the three-layer metadata model, global context-aware information is constructed for the AI large model; The AI big model combines the global context-aware information to avoid code conflict, so as to prevent the generated target business code from having errors that are locally correct but globally logically conflicting.
[0026] When generating business code to delete users, AI might directly generate physical delete statements based solely on local logic. However, because the system inputs structural metadata containing relationships between hundreds of tables as a global context to the AI, the AI recognizes the strong foreign key constraint between the user table and the order table. Therefore, the AI proactively avoids direct delete operations and instead generates business code that includes cascading checks or logical delete flag updates. This effectively prevents errors where the AI-generated code is partially correct but globally conflicting, significantly shortening the time required for subsequent code integration and debugging.
[0027] Furthermore, in some embodiments, the dual-engine security verification of the target business code based on a preset security rule base and the AI big data model includes: Extract the enterprise security specifications recorded in the management metadata and construct the preset security rule base; The template engine triggers code splicing risk detection on the target business code based on the preset security rule base; The AI big data model is invoked synchronously to perform security vulnerability scanning and permission verification missing detection on the target business code at the business logic level.
[0028] The system triggers a security process during code generation. First, the template engine retrieves enterprise security specifications from the management metadata and performs static rule verification on the initially generated code. Simultaneously, the AI model performs dynamic semantic scanning on the code, discovering that the generated API interfaces lack permission verification logic. It then automatically adds authentication annotations, ultimately outputting secure and compliant code. This completely solves the industry pain point of uncontrollable quality generation by AI tools. By integrating a rule base with AI vulnerability scanning, the system significantly improves the compliance rate and security compliance rate of the generated code, thereby significantly reducing the cost of manual review.
[0029] Furthermore, in some embodiments, before invoking the AI large model to collaboratively optimize the initial code skeleton, the following steps are also included: Obtain metadata description information of the target company's internal self-developed framework; The metadata description information is used as a lightweight feature to fine-tune the large language model deployed in the enterprise intranet environment, so that the large AI model can be adapted to the unique development framework of the intranet environment.
[0030] Through the above features, it effectively solves multiple challenges such as insufficient support for enterprise internal frameworks for general large models, high cost and long cycle of fine-tuning training, and high threshold of computing power resources for local deployment, enabling AI large models to be deployed and adapted securely and quickly within the enterprise intranet.
[0031] The second aspect of this application discloses a code generation system based on a metadata model, comprising: a metadata parsing module, used to obtain a three-layer metadata model of a target application and a preset template file, and to parse the three-layer metadata model to extract data model information; The core engine module is used to populate the data model information into the template file and generate the initial code skeleton; The AI optimization module is used to call a large AI model to collaboratively optimize the initial code skeleton and generate target business code. The security verification module is used to perform dual-engine security verification on the target business code based on a preset security rule base and the AI big model; The code output module is used to output the final target code file after the dual-engine security verification passes.
[0032] A third aspect of this application discloses a computer-readable storage medium having a computer program stored thereon, wherein the computer program, when executed by a processor, implements the above-described method. For components that are functionally identical or similar, existing mature devices can be used for replacement, and the implementation method is not limited to the structure shown in this specification.
[0033] The present invention can also be implemented in the form of software, hardware, or a combination of software and hardware; wherein the software can be stored in a computer-readable medium and executed by a processor to perform the corresponding function.
[0034] Applicable computer-readable media include, but are not limited to, hard disks, flash memory, read-only memory (ROM), random access memory (RAM), and other media capable of storing program code.
[0035] The execution order of the steps described in the flowchart or logic block diagram can be adjusted or parallelized as needed, provided that it does not affect the implementation of the function.
[0036] The accompanying drawings referenced in this specification are for illustrative purposes only. Their dimensions, scale, or colors may be adjusted according to actual production needs. The reference numerals in the drawings should not be construed as limiting the scope of protection.
[0037] All the technical features disclosed in this invention can be combined in any way to form an interactive or collaborative structure, as long as the combination does not contradict each other or conflict with the technology.
[0038] Although embodiments of the present invention have been described in conjunction with the accompanying drawings, those skilled in the art can make various modifications and variations without departing from the spirit and scope of the present invention, and such modifications and variations all fall within the scope of protection claimed by the present invention.
Claims
1. A method for code generation based on a metadata model, characterized in that, include: Obtain the target application's three-tier metadata model and preset template files; The three-layer metadata model is parsed using a metadata parser to extract data model information, and the data model information is then filled into the template file to generate an initial code skeleton. The initial code skeleton is collaboratively optimized by calling an AI large model to generate the target business code; The target business code is subjected to dual-engine security verification based on a preset security rule base and the AI big model. After the dual-engine security check passes, the final target code file is output.
2. The method of claim 1, wherein, The three-layer metadata model includes descriptive metadata, structural metadata, and management metadata; The descriptive metadata is used to define code development standards and development framework constraints; the structural metadata is used to store database table relationships and entity attributes; and the management metadata is used to record the scheduling strategy and template version control information for code generation tasks.
3. The method of claim 2, wherein, The process of parsing the three-layer metadata model using a metadata parser to extract data model information, and then filling the template file with the data model information to generate an initial code skeleton includes: The template file is dynamically decoupled from the three-layer metadata model; The three-layer metadata model is decomposed into structured data variables through a unified parsing interface; Dynamically load a pluggable template file that matches the current application scenario, and render the structured data variables into the pluggable template file to generate the initial code skeleton.
4. The method of claim 2, wherein, The process of calling the AI big model to collaboratively optimize the initial code skeleton and generate the target business code includes a two-stage collaborative optimization process: Basic stage optimization: The AI large model performs redundant code removal and code formatting alignment operations on the initial code skeleton based on the description metadata to generate semi-finished code; Logic enhancement stage: Natural language instructions are obtained. The AI big model combines the natural language instructions with the entity relationships defined in the structural metadata to generate business logic code and inject it into the semi-finished code to generate the target business code.
5. The method according to claim 4, characterized in that: The business logic code generated during the logic enhancement phase includes at least index design logic, multi-table join query logic, and caching strategy logic.
6. The method according to claim 1, characterized in that, The process of calling the AI big model to collaboratively optimize the initial code skeleton and generate the target business code also includes: Based on the three-layer metadata model, global context-aware information is constructed for the AI large model; The AI big model combines the global context-aware information to avoid code conflict, so as to prevent the generated target business code from having errors that are locally correct but globally logically conflicting.
7. The method according to claim 2, characterized in that, The dual-engine security verification of the target business code based on the preset security rule base and the AI big data model includes: Extract the enterprise security specifications recorded in the management metadata and construct the preset security rule base; The template engine triggers code splicing risk detection on the target business code based on the preset security rule base; The AI big data model is invoked synchronously to perform security vulnerability scanning and permission verification missing detection on the target business code at the business logic level.
8. The method according to claim 1, characterized in that, Before invoking the AI large model to collaboratively optimize the initial code skeleton, the process also includes: Obtain metadata description information of the target company's internal self-developed framework; The metadata description information is used as a lightweight feature to fine-tune the large language model deployed in the enterprise intranet environment, so that the large AI model can be adapted to the unique development framework of the intranet environment.
9. A code generation system based on a metadata model, characterized in that, include: The metadata parsing module is used to obtain the three-layer metadata model of the target application and the preset template file, and to parse the three-layer metadata model to extract data model information. The core engine module is used to populate the data model information into the template file and generate the initial code skeleton; The AI optimization module is used to call a large AI model to collaboratively optimize the initial code skeleton and generate target business code. The security verification module is used to perform dual-engine security verification on the target business code based on a preset security rule base and the AI big model; The code output module is used to output the final target code file after the dual-engine security verification passes.
10. A computer-readable storage medium having a computer program stored thereon, characterized in that, When the computer program is executed by a processor, it implements the method as described in any one of claims 1 to 8.