Method, device and server for delayed loading of data between database tables
Patent Information
- Application Number
- CN202211153230.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-09-21
- Publication Date
- 2026-09-22
- Estimated Expiration
- 2042-09-21
AI Technical Summary
[0005]本公开提供了一种数据库表间的数据延迟加载方法、装置及服务器,以解决现有技术中在数据库禁用外键的情况下,只能显性的进行数据库表的数据加载,而加载数据库表中大量的无用信息,造成的非常大的性能消耗的问题
[0021]本公开提供的数据库表间的数据延迟加载方法、装置及服务器,包括:获取数据加载请求,并根据数据加载请求,获取预先配置的第一模型对象和第一数据库表的第一映射关系,以及第二模型对象和第二数据库表的第二映射关系;根据第一映射关系,确定与第一数据库表对应的第一模型对象实例;并根据第二映射关系,确定与第二数据库表对应的第二模型对象实例;根据第一模型对象实例中的预设第一字段,确定根据预设第一字段加载第二模型对象实例中预设第二字段中数据的数据加载函数;在数据加载函数中注入延迟加载属性,得到数据延迟加载函数;数据延迟加载函数用于根据预设第一字段延迟加载预设第二字段中的数据;并根据数据加载请求,返回数据延迟加载函数加载的数据。本方案提供的数据库表间的数据延迟加载方法、装置及服务器中,可以利用模型对象实例,确定数据库表间的数据加载函数,然后在数据加载函数中注入延迟加载属性,得到数据延迟加载函数。实现了数据库禁用外键情境下,数据库表间数据的延迟加载,且只加载需要字段的数据,从而可以减小性能消耗。
Smart Images

Figure CN115495167B_ABST
Abstract
Description
Technical Field
[0001] This disclosure relates to computer technology, and in particular to a method, apparatus, and server for lazy loading of data between database tables. Background Technology
[0002] Currently, databases introduce foreign keys to add an extra layer of foreign key constraints between tables, reducing data redundancy. Lazy loading allows data to be created only when it's actually used, thus improving program execution efficiency. However, current industry practices often restrict the use of foreign keys. Therefore, how to achieve relationships between database tables and define lazy loading when foreign keys are disabled is a problem that urgently needs to be solved.
[0003] In existing technologies, with foreign keys disabled, SQLAlchemy in Python can be used to define lazy loading based on relationships, allowing database tables to be loaded only when needed. This enables explicit loading of database tables.
[0004] However, the above method explicitly loads the database table, that is, loads all the information in the database table. Loading a large amount of irrelevant information in the database table will cause a very large performance consumption. Summary of the Invention
[0005] This disclosure provides a method, apparatus, and server for lazy loading data between database tables, to solve the problem in the prior art where, when foreign keys are disabled in the database, only explicit data loading of database tables is possible, resulting in a large amount of useless information being loaded from the database tables, causing significant performance overhead.
[0006] According to a first aspect of this disclosure, a method for lazy loading data between database tables is provided, comprising:
[0007] Obtain a data loading request, and based on the data loading request, obtain a pre-configured first mapping relationship between a first model object and a first database table, and a second mapping relationship between a second model object and a second database table;
[0008] Based on the first mapping relationship, determine the first model object instance corresponding to the first database table; and based on the second mapping relationship, determine the second model object instance corresponding to the second database table.
[0009] Based on the preset first field in the first model object instance, determine the data loading function that loads data from the preset second field in the second model object instance based on the preset first field;
[0010] A lazy loading attribute is injected into the data loading function to obtain a data lazy loading function; the data lazy loading function is used to lazy load data in the preset second field according to the preset first field; and returns the data loaded by the data lazy loading function according to the data loading request.
[0011] According to a second aspect of this disclosure, a data lazy loading apparatus between database tables is provided, comprising:
[0012] The acquisition unit is used to acquire a data loading request, and according to the data loading request, acquire a pre-configured first mapping relationship between a first model object and a first database table, and a second mapping relationship between a second model object and a second database table;
[0013] The determining unit is configured to determine a first model object instance corresponding to the first database table based on the first mapping relationship; and to determine a second model object instance corresponding to the second database table based on the second mapping relationship.
[0014] The loading unit is used to determine, based on a preset first field in the first model object instance, a data loading function that loads data from a preset second field in the second model object instance according to the preset first field.
[0015] The loading unit is further configured to inject a lazy loading attribute into the data loading function to obtain a data lazy loading function; the data lazy loading function is configured to lazy load data in the preset second field according to the preset first field; and return the data loaded by the data lazy loading function according to the data loading request.
[0016] According to a third aspect of this disclosure, an electronic device is provided, including a memory and a processor; wherein,
[0017] The memory is used to store computer programs;
[0018] The processor is configured to read a computer program stored in the memory and execute the data lazy loading method between database tables as described in the first aspect, based on the computer program in the memory.
[0019] According to a fourth aspect of this disclosure, a computer-readable storage medium is provided, wherein computer-executable instructions are stored therein, and when a processor executes the computer-executable instructions, the data lazy loading method between database tables as described in the first aspect is implemented.
[0020] According to a fifth aspect of this disclosure, a computer program product is provided, including a computer program that, when executed by a processor, implements the data lazy loading method between database tables as described in the first aspect.
[0021] The data lazy loading method, apparatus, and server between database tables disclosed herein include: obtaining a data loading request; and, based on the data loading request, obtaining a pre-configured first mapping relationship between a first model object and a first database table, and a second mapping relationship between a second model object and a second database table; determining a first model object instance corresponding to the first database table based on the first mapping relationship; and determining a second model object instance corresponding to the second database table based on the second mapping relationship; determining a data loading function that loads data from a preset second field in the second model object instance based on a preset first field in the first model object instance; injecting a lazy loading attribute into the data loading function to obtain a data lazy loading function; the data lazy loading function being used to lazy load data from a preset second field based on the preset first field; and returning the data loaded by the data lazy loading function according to the data loading request. In the data lazy loading method, apparatus, and server between database tables provided in this solution, a model object instance can be used to determine the data loading function between database tables, and then a lazy loading attribute can be injected into the data loading function to obtain the data lazy loading function. This achieves lazy loading of data between database tables in a database scenario where foreign keys are disabled, and only loads data from the required fields, thereby reducing performance consumption. Attached Figure Description
[0022] To more clearly illustrate the technical solutions in the embodiments of this disclosure or the prior art, the drawings used in the description of the embodiments or the prior art will be briefly introduced below. Obviously, the drawings described below are only some embodiments of this disclosure. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.
[0023] Figure 1 A flowchart illustrating a method for lazy loading data between database tables, as shown in an exemplary embodiment of this disclosure;
[0024] Figure 2 A flowchart illustrating a method for lazy loading data between database tables, as shown in another exemplary embodiment of this disclosure;
[0025] Figure 3 This is a schematic diagram illustrating a data lazy loading process between database tables, as shown in an exemplary embodiment of this disclosure.
[0026] Figure 4 A structural diagram of a data lazy loading apparatus between database tables, as illustrated in an exemplary embodiment of this disclosure;
[0027] Figure 5 A structural diagram illustrating a data lazy loading apparatus between database tables, as shown in another exemplary embodiment of this disclosure;
[0028] Figure 6 This is a structural diagram of an electronic device illustrated in an exemplary embodiment of the present disclosure. Detailed Implementation
[0029] Currently, databases introduce foreign keys to add a layer of foreign key constraints between tables, reducing data redundancy. By introducing lazy loading, objects can be created only when they are actually used, thus improving program execution efficiency. Therefore, it's possible to rely on the definition of foreign keys at the database level to establish object relationships, and then load objects according to the lazy loading configuration when needed, thereby achieving lazy loading between database tables. However, current industry practices often restrict the use of foreign keys. In a technical context where foreign keys are disabled, lazy loading between database tables cannot be directly implemented at the framework level. Therefore, how to achieve relationships between database tables and define lazy loading in the context of disabling foreign keys is a problem that urgently needs to be solved.
[0030] In existing technologies, when foreign keys are disabled, Python's SQLAlchemy can be used to define various strategies such as lazy loading and preloading based on relationships, applicable to various scenarios including select, join, and subselect. However, the model definition depends on the definition of foreign keys. It's possible to load relevant database tables only when needed, or to explicitly load database tables.
[0031] However, the above method explicitly loads the database table, that is, loads all the information in the database table. Loading a large amount of irrelevant information in the database table will cause a very large performance consumption.
[0032] In existing technologies, even with foreign keys disabled, Django ORM can still be used. Django is a Python web framework that has a built-in Object Relational Mapping (ORM) implementation, which, compared to SQLAlchemy, directly maps data tables to Python model classes in a simpler and more direct way. Relationships between models are also implemented by defining foreign keys, making lazy loading impossible.
[0033] However, while Django ORM is ready to use out of the box, its shortcomings are more obvious: First, it is heavily dependent on the Django framework and cannot be used in external ecosystems; second, it relies more on foreign keys and cannot achieve lazy loading of database tables.
[0034] To address the aforementioned technical issues, the solution provided in this disclosure utilizes model object instances to determine data loading functions between database tables. Then, lazy loading attributes are injected into these data loading functions to obtain lazy loading functions. This achieves framework-independent, general-purpose lazy loading of data between database tables in scenarios where foreign keys are disabled, loading only the necessary fields and thus reducing performance overhead.
[0035] The technical solutions of this disclosure and how they solve the aforementioned technical problems will be described in detail below with specific embodiments. These specific embodiments can be combined with each other, and the same or similar concepts or processes may not be repeated in some embodiments. The embodiments of this disclosure will now be described with reference to the accompanying drawings.
[0036] Figure 1 This is a flowchart illustrating a method for lazy loading data between database tables, as shown in an exemplary embodiment of this disclosure.
[0037] like Figure 1 As shown, the data lazy loading method between database tables provided in this embodiment includes:
[0038] Step 101: Obtain a data loading request, and based on the data loading request, obtain the first mapping relationship between the pre-configured first model object and the first database table, and the second mapping relationship between the second model object and the second database table.
[0039] The method provided in this disclosure can be executed by an electronic device with computing capabilities, such as a computer.
[0040] Specifically, depending on the actual situation, the first mapping relationship between the first model object and the first database table, and the second mapping relationship between the second model object and the second database can be pre-configured using ORM.
[0041] ORM, in particular, can link object-oriented concepts with the concept of tables in a database during programming, enabling mutual persistence and instantiation between objects in the program and relational database tables.
[0042] Specifically, the electronic device can obtain a data loading request and obtain the first mapping relationship and the second mapping relationship based on the data loading request.
[0043] In this context, both the first database table and the second database table represent table definitions on the database.
[0044] The first model object is used to represent the object-oriented model representation of the first database table.
[0045] Similarly, the second model object is used to represent the object-oriented model representation of the second database table.
[0046] The first mapping relationship is used to characterize the correspondence between the attributes in the first model object and the fields in the first database table.
[0047] Similarly, the second mapping relationship is used to characterize the correspondence between attributes in the second model object and fields in the second database table.
[0048] Step 102: Determine the first model object instance corresponding to the first database table based on the first mapping relationship; and determine the second model object instance corresponding to the second database table based on the second mapping relationship.
[0049] Specifically, based on the first mapping relationship, the ORM can be used to serialize and instantiate the first model object to determine the first model object instance corresponding to the first database table. The first database table can then be controlled through operations on the first model object instance.
[0050] Similarly, based on the second mapping relationship, the ORM can be used to serialize and instantiate the second model object to determine the second model object instance corresponding to the second database table. The second database table can then be controlled through operations on the second model object instance.
[0051] Step 103: Determine the data loading function that loads data from the preset second field in the second model object instance based on the preset first field in the first model object instance.
[0052] The preset first field is a field in the first model object instance that is pre-set according to the actual situation.
[0053] The preset second field is a field in the second model object instance that is pre-set according to the actual situation.
[0054] Specifically, partial functions in Python can be used to define a data loading function. The first and second preset fields are fixed parameters of this data loading function. This function can then load data from the second database table based on the first preset field from the first database table, using the second preset field from the second database table. Therefore, only the necessary fields are loaded, thus reducing performance overhead.
[0055] Partial application, a concept in functional programming, refers to the process in computer science of fixing some parameters of a multivariate function and returning a function that can accept the remaining parameters.
[0056] Specifically, this embodiment does not limit the number of preset second fields. Specifically, data from multiple preset second fields in a second model object instance can be loaded based on a preset first field.
[0057] Step 104: Inject the lazy loading attribute into the data loading function to obtain the data lazy loading function; the data lazy loading function is used to lazy load data in the preset second field according to the preset first field; and return the data loaded by the data lazy loading function according to the data loading request.
[0058] Specifically, you can use the `@property` property in Python to inject lazy-loading attributes into the data loading function, thus creating a lazy-loading function. This function can be used to lazy-load data from a second predetermined field based on a first predetermined field. This transforms attribute calls into function calls, allowing data to be loaded from relevant database tables only when needed, achieving the purpose of lazy loading and reducing performance overhead.
[0059] Furthermore, based on the data loading request, it can return the data loaded by the lazy loading function.
[0060] The data lazy loading method between database tables provided in this disclosure includes: obtaining a data loading request; and, based on the data loading request, obtaining a pre-configured first mapping relationship between a first model object and a first database table, and a second mapping relationship between a second model object and a second database table; determining a first model object instance corresponding to the first database table based on the first mapping relationship; and determining a second model object instance corresponding to the second database table based on the second mapping relationship; determining a data loading function that loads data from a preset second field in the second model object instance based on a preset first field in the first model object instance; injecting a lazy loading attribute into the data loading function to obtain a data lazy loading function; the data lazy loading function is used to lazy load data from a preset second field based on the preset first field; and returning the data loaded by the data lazy loading function according to the data loading request. In this method, a model object instance can be used to determine a data loading function between database tables, and then a lazy loading attribute can be injected into the data loading function to obtain the data lazy loading function. This achieves lazy loading of data between database tables in a database scenario where foreign keys are disabled, and only loads data from the required fields, thereby reducing performance overhead.
[0061] Figure 2 This is a flowchart illustrating a method for lazy loading data between database tables, which is another exemplary embodiment of this disclosure.
[0062] like Figure 2 As shown, the data lazy loading method between database tables provided in this embodiment includes:
[0063] Step 201: Obtain a data loading request, and based on the data loading request, obtain the first mapping relationship between the pre-configured first model object and the first database table, and the second mapping relationship between the second model object and the second database table.
[0064] Specifically, the principle and implementation of step 201 are similar to those of step 101, and will not be repeated here.
[0065] Step 202: Based on the first mapping relationship, the first model object is serialized and instantiated to obtain a first model object instance corresponding to the first database table; based on the second mapping relationship, the second model object is serialized and instantiated to obtain a second model object instance corresponding to the second database table.
[0066] Specifically, using an ORM approach, the first model object can be serialized and instantiated based on the correspondence between its attributes and the fields of the first database table within the first mapping relationship. This yields a first model object instance corresponding to the first database table. Furthermore, operations on this first model object instance can then be used to manipulate the first database table.
[0067] Similarly, using an ORM approach, the second model object can be serialized and instantiated based on the correspondence between its attributes and the fields of the second database table within the second mapping relationship, thus obtaining a second model object instance corresponding to the second database table. Furthermore, operations on this second model object instance can then be used to manipulate the second database table.
[0068] Step 203: Associate the preset first field with the preset third field in the second model object instance; wherein the data in the preset first field is the same as the data in the preset third field, and the preset third field is a unique identifier in the second model object instance.
[0069] The third field is a pre-set field in the second model object instance, which is set according to the actual situation.
[0070] The third field can be a unique identifier within the second model object instance, used to uniquely identify each record in each row of the second model object instance. It must not be identical to other records or be empty. Examples include identity information and license plate information. The data in the third field is identical to the data in the first field.
[0071] Specifically, a preset first field in the first model object instance can be associated with a preset third field in the second model object instance. Furthermore, this allows for the definition of a foreign key in a database through model object instances.
[0072] Foreign keys are used to join two database tables, keeping their data synchronized. Typically, a foreign key consists of one or more fields from a database table, and a foreign key in one table points to the primary key of another. The database table containing the foreign key is called the child table, and the table it points to is the primary table. Data in the child table is constrained by the primary table; when inserting or updating data in the child table, the value of the foreign key must exist in the primary key of the primary table.
[0073] In a database table, a primary key uniquely identifies each record. Common primary keys include identity information, license plate information, etc. A primary key must contain unique values, cannot have duplicates, and must not be null.
[0074] Step 204: Based on the association between the preset first field and the preset third field, determine the data loading function that loads data from the preset second field based on the preset first field.
[0075] Specifically, a data loading function can be defined based on the relationship between a preset first field and a preset third field, using partial functions in Python. The preset first field and preset second field are fixed parameters of this data loading function. This data loading function can then be used to load data from a preset second field in a second database table based on the preset first field in a first database table.
[0076] Step 205: Inject the lazy loading attribute into the data loading function to obtain the data lazy loading function.
[0077] Specifically, you can use Python's `@property` property to inject lazy-loading properties into the data loading function, thus creating a lazy-loading function. This allows calling the property to become calling the function, enabling you to load data from the relevant database tables only when needed, thereby achieving lazy loading and reducing performance overhead.
[0078] Specifically, lazy loading, also known as deferred loading, refers to postponing the SELECT query on related objects according to set delay rules when performing join queries on database tables, in order to reduce the pressure on the database.
[0079] Step 206: Encapsulate the data lazy loading function according to a preset method; the encapsulated data lazy loading function is used to lazy load data in a preset second field according to a preset first field; and returns the data loaded by the encapsulated data lazy loading function according to the data loading request.
[0080] Specifically, the actual calling object of the data lazy loading function is the memory, which is not user-friendly. Therefore, the data lazy loading function can be further encapsulated, exposing the model object to the business logic for easier use. The encapsulated data lazy loading function can be used to lazy load data from a preset second field based on a preset first field.
[0081] Furthermore, based on the data loading request, it can return the data loaded by the encapsulated data lazy loading function.
[0082] Step 207: After loading the data using the data lazy loading function, cache the loaded data.
[0083] Specifically, after loading data using a lazy loading function, the loaded data can be cached. The next time the data is loaded, it doesn't need to be retrieved from the database table again; the cached data can be used directly. This approach reduces performance overhead and improves data retrieval speed.
[0084] Specifically, this solution utilizes partial functions to reduce redundant parameter passing and solidify some known parameters. While reducing the generality of the function, it improves its applicability. The object returned by the partial function is also a function, and there is no performance overhead if it is not called. Therefore, based on the setting of partial functions, the loading (association) of a specified object can be solidified into the parameters of the partial function and called when needed, achieving the characteristic of lazy loading. Combining Python's property feature, exposing the associated object is transformed into a normal attribute, and then adding object caching technology further improves performance.
[0085] Figure 3 This is a schematic diagram illustrating a data lazy loading process between database tables, as shown in an exemplary embodiment of this disclosure.
[0086] like Figure 3 As shown, step one involves defining the mapping relationship between model objects and database tables using a pre-defined model object mapper. Step two involves defining the instantiation and serialization logic of model objects from database tables using a pre-defined model object store. These two steps then yield the model objects and the model object store.
[0087] Step 3: Based on the preset primary key of the external associated object, a lazy-loaded partial function object is generated using the partial function injector. Then, the lazy property is injected into the model object using the lazy property loader and model object accessor. Simultaneously, the associated model object is injected as a property of the model object using the associated property injector. This results in the lazy-loaded partial function object, the lazy property, and the model object.
[0088] Ultimately, the business uses the model object and performs lazy loading on demand.
[0089] The model object accessor is used to encapsulate the conversion between database tables and model objects.
[0090] The partial function injector is used to inject partial functions and bind them to associated object parameters.
[0091] The lazy property injector is used to inject lazy-loaded functions.
[0092] Among them, the associated property injector is used to inject associated object properties and increase caching capabilities, etc.
[0093] The model object mapper is used to map database tables and model objects defined using the ORM framework.
[0094] Among them, the model object is used to represent the object-oriented model representation of the database table.
[0095] Among them, database tables are used to represent the table definitions on the actual database.
[0096] Specifically, firstly, based on the usage of the ORM framework, the mapping between database tables and model objects can be defined.
[0097] Then, a pre-defined model storage can be used to perform instantiation and serialization conversions between database tables and model objects. For example, desired data can be constructed into model objects based on a pre-defined unique identifier (i.e., a pre-defined primary key). Alternatively, model objects can be serialized and directly stored in the database. Model creation and retrieval logic can be implemented through the model storage.
[0098] Then, based on the partial function, the delay of the associated model can be injected, thereby realizing the injection of the model's delay attribute.
[0099] The partial function approach described above actually calls the memory, which is not user-friendly for business logic. Therefore, we can further encapsulate properties within the model object for easier use. Consequently, specific business logic will directly trigger the retrieval of the model object, and there will be no performance penalty if no object is called.
[0100] The loaded data can also be cached. If a cache exists, it can be used directly. If it's determined that the data isn't cached, the injected partial function is called to load the actual model object. Caching data reduces performance overhead and improves data retrieval speed.
[0101] Figure 4 This is a structural diagram of a data lazy loading apparatus between database tables, illustrating an exemplary embodiment of the present disclosure.
[0102] like Figure 4 As shown, the data lazy loading device 400 between database tables provided in this disclosure includes:
[0103] The acquisition unit 410 is used to acquire a data loading request, and according to the data loading request, acquire a pre-configured first mapping relationship between a first model object and a first database table, and a second mapping relationship between a second model object and a second database table;
[0104] The determining unit 420 is used to determine the first model object instance corresponding to the first database table according to the first mapping relationship; and to determine the second model object instance corresponding to the second database table according to the second mapping relationship;
[0105] The loading unit 430 is used to determine, based on the preset first field in the first model object instance, a data loading function that loads data from the preset second field in the second model object instance according to the preset first field.
[0106] The loading unit 430 is also used to inject a lazy loading attribute into the data loading function to obtain a data lazy loading function; the data lazy loading function is used to lazy load data in a preset second field according to a preset first field; and returns the data loaded by the data lazy loading function according to the data loading request.
[0107] Figure 5 This is a structural diagram of a data lazy loading device between database tables, illustrating another exemplary embodiment of the present disclosure.
[0108] like Figure 5 As shown, based on the above embodiments, the data lazy loading device 500 between database tables provided in this disclosure further includes a loading unit 430 that includes:
[0109] The association module 431 is used to associate a preset first field with a preset third field in the second model object instance; wherein the data in the preset first field is the same as the data in the preset third field, and the preset third field is a unique identifier in the second model object instance;
[0110] The function determination module 432 is used to determine the data loading function that loads data from the preset second field based on the association between the preset first field and the preset third field.
[0111] The determining unit 420 is specifically used to serialize and instantiate the first model object according to the first mapping relationship to obtain the first model object instance corresponding to the first database table.
[0112] The determining unit 420 is specifically used to serialize and instantiate the second model object according to the second mapping relationship to obtain the second model object instance corresponding to the second database table.
[0113] The loading unit 430 is also used to encapsulate the data deferred loading function according to a preset method; the encapsulated data deferred loading function is used to deferred load data in a preset second field according to a preset first field.
[0114] Loading unit 430 is also used to cache the loaded data after loading the data using the data lazy loading function.
[0115] Figure 6 This is a structural diagram of a server shown as an exemplary embodiment of the present disclosure.
[0116] like Figure 6 As shown, the server provided in this embodiment includes:
[0117] Memory 601;
[0118] Processor 602; and
[0119] Computer programs;
[0120] The computer program is stored in memory 601 and configured to be executed by processor 602 to implement any of the above-described methods for lazy loading data between database tables.
[0121] This embodiment also provides a computer-readable storage medium having a computer program stored thereon, the computer program being executed by a processor to implement any of the above-described methods for lazy loading data between database tables.
[0122] This embodiment also provides a computer program product, including a computer program that, when executed by a processor, implements any of the above-described methods for lazy loading data between database tables.
[0123] Those skilled in the art will understand that all or part of the steps of the above-described method embodiments can be implemented by hardware related to program instructions. The aforementioned program can be stored in a computer-readable storage medium. When executed, the program performs the steps of the above-described method embodiments; and the aforementioned storage medium includes various media capable of storing program code, such as ROM, RAM, magnetic disks, or optical disks.
[0124] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention, and not to limit them; although the present invention has been described in detail with reference to the foregoing embodiments, those skilled in the art should understand that modifications can still be made to the technical solutions described in the foregoing embodiments, or equivalent substitutions can be made to some or all of the technical features; and these modifications or substitutions do not cause the essence of the corresponding technical solutions to deviate from the scope of the technical solutions of the embodiments of the present invention.
Claims
1. A method for lazy loading data between database tables, characterized in that, include: Obtain a data loading request, and based on the data loading request, obtain a pre-configured first mapping relationship between a first model object and a first database table, and a second mapping relationship between a second model object and a second database table; Based on the first mapping relationship, determine the first model object instance corresponding to the first database table; and based on the second mapping relationship, determine the second model object instance corresponding to the second database table. Associating a preset first field in the first model object instance with a preset third field in the second model object instance; wherein the data in the preset first field is the same as the data in the preset third field, and the preset third field is a unique identifier in the second model object instance; Based on the association between the preset first field and the preset third field, determine the data loading function that loads data from the preset second field in the second model object instance based on the preset first field; A lazy loading attribute is injected into the data loading function to obtain a data lazy loading function; the data lazy loading function is used to lazy load data in the preset second field according to the preset first field; and returns the data loaded by the data lazy loading function according to the data loading request.
2. The method according to claim 1, characterized in that, The step of determining the first model object instance corresponding to the first database table based on the first mapping relationship includes: Based on the first mapping relationship, the first model object is serialized and instantiated to obtain a first model object instance corresponding to the first database table.
3. The method according to claim 1, characterized in that, The step of determining the second model object instance corresponding to the second database table based on the second mapping relationship includes: Based on the second mapping relationship, the second model object is serialized and instantiated to obtain a second model object instance corresponding to the second database table.
4. The method according to claim 1, characterized in that, Also includes: The data lazy loading function is encapsulated according to a preset method; the encapsulated data lazy loading function is used to lazy load data in the preset second field according to the preset first field.
5. The method according to any one of claims 1-4, characterized in that, After loading the data using the data lazy loading function, the loaded data is cached.
6. A data lazy loading device between database tables, characterized in that, include: The acquisition unit is used to acquire a data loading request, and according to the data loading request, acquire a pre-configured first mapping relationship between a first model object and a first database table, and a second mapping relationship between a second model object and a second database table; The determining unit is configured to determine a first model object instance corresponding to the first database table based on the first mapping relationship; and to determine a second model object instance corresponding to the second database table based on the second mapping relationship. The loading unit is used to associate a preset first field in the first model object instance with a preset third field in the second model object instance; wherein the data in the preset first field is the same as the data in the preset third field, and the preset third field is a unique identifier in the second model object instance; Based on the association between the preset first field and the preset third field, determine the data loading function that loads data from the preset second field in the second model object instance based on the preset first field; The loading unit is further configured to inject a lazy loading attribute into the data loading function to obtain a data lazy loading function; the data lazy loading function is configured to lazy load data in the preset second field according to the preset first field; and return the data loaded by the data lazy loading function according to the data loading request.
7. A server, characterized in that, Includes memory and processor; among which, The memory is used to store computer programs; The processor is configured to read the computer program stored in the memory and execute the method described in any one of claims 1-5 according to the computer program in the memory.
8. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores computer-executable instructions, which, when executed by a processor, implement the method described in any one of claims 1-5.
9. A computer program product, comprising a computer program, characterized in that, When the computer program is executed by a processor, it implements the method described in any one of claims 1-5.
Citation Information
Patent Citations
Control method and device for non-relational database
CN107122435A
Field-to-field join constraints
US20060136470A1