Basic concepts of ORM (O/R mappping Object Relational mapping)
In our system, there are a large number of objects that need to be persisted, such objects may be various business documents, or our system configuration information. Other objects that are in-memory use and do not require persistent storage are not part of our discussion. In languages that support object serialization, such as C#,java, we can serialize these objects to disk files or directly to a relational database. The relational database is our most important and safest choice.
When we perform various operations on the business data in the database, we find that many of the insert, Update, and delete operations we do have a lot in common, except that the SQL statements have different transformations based on different objects. So people want to automate the operations of these same parts through one thing, and those different processes, and then do special processing. So we can focus on the individual, special process operations. This is the root cause of ORM.
In the Java world, ORM Frameworks are used earlier and more mature, such as Hibernate and so on. NET in the world, the start is relatively late. But the fear of using open source ORM frameworks results in more complex projects (because these open source tools are good, but each project always has its own special place). So it's not used at the moment). But we can actually design and develop some small, and try to meet their own project ORM.
But no matter what, we all need to have some knowledge of ORM:
One, the inheritance structure of the object:
Inheritance is one of the most important concepts in OO-enabled languages, so our ORM should also support inheritance. In general, there are three modes of inheritance in the ORM World:
1, an inheritance tree maps to a table (one_inheritance_tree_one_table):
All classes that have the same parent class are mapped to a table, and the collection of these class property mappings makes up all the columns of the table, in which case only the bottommost class is mapped. As in the following class structure:
In the class structure above, the parent class has attributes Property1 and Property2, and subclasses Child1 have attribute Property3, and subclass Child2 has attribute Property4.
So if you use the one_inheritance_tree_one_table mapping model, there is only one table in the database.
Class properties |
database table field |
Property1 |
Field1 |
property2</td> |
Field2 |
Property3 |
Field3 |
Property4 |
Field4 |
However, this pattern has a large number of redundancy, for Child1, because there is no Property4 attribute, so the field Field4 is a redundant field. Similarly, for child2,field3 is a redundant field. But the advantages of this model are simple.