Analysis of the polymorphism processing method in the Gorm framework

Gorm (Grails Object Relational Mapping) is a persistent framework that integrates the Hibernate framework based on the Groovy language.It provides a method to simplify database access and supports the treatment of polymorphism.In GORM, polymorphism refers to a object that can have a variety of different types.This article will analyze the processing method of polymorphism in the Gorm framework and provide examples of Java code. In GORM, the main method of processing polymorphism is to use Gorm's inheritance mapping strategy.Gorm provides three inheritance mapping strategies: single -table inheritance, specific table inheritance and sub -table inheritance. 1. Single table inheritance: Single -table inheritance refers to saving the attributes of all subclasses in one table.This strategy is suitable for small differences between subclasses, and in most cases, it is only queried according to the type.In GORM, set the inheritance of the mapping strategy as a single table inheritance with the `Tableperhierarchy`. The example code is as follows: class Animal { String name } class Dog extends Animal { String breed } class Cat extends Animal { String color } static mapping = { tablePerHierarchy false } 2. Specific table inheritance: Specific table inheritance means that each subclass has a corresponding table, which contains the attributes of all parent and subclasses.This strategy is suitable for the difference between subclasses, and the situation that needs to be queried according to the sub -type.In Gorm, set the inheritance strategy as a specific table inheritance using the inheritance of the inheritance of the inheritance. The example code is as follows: class Animal { String name } class Dog extends Animal { String breed } class Cat extends Animal { String color } static mapping = { tablePerConcreteClass true } 3. Sub -watch inheritance: The sub -meter inheritance means that each subclass has a corresponding table, which only contains the unique attributes of the subclass.The attributes of the parent can be repeatedly stored in the subclass.This strategy is suitable for the difference between subclasses, and the situation that needs to be queried according to the parent or subclass type.In Gorm, use the `Tablepersubclass` to set the inheritance of the mapping strategy as the sub -table inheritance. The example code is as follows: class Animal { String name } class Dog extends Animal { String breed } static mapping = { tablePerSubclass true } It can be seen through the above example code that it is very simple to use the Gorm framework to deal with the polymorphism.Just set the appropriate inheritance mapping strategy in the Domain class to achieve polymorphism. Summarize: The Gorm framework provides a polymorphism processing method by inheriting the mapping strategy.In a single table inheritance strategy, the attributes of all subclasses can be preserved in one table; in the specific table inheritance strategy, each subclass has a corresponding table, which contains the attributes of all parent and subclasses;In the inheritance strategy, each subclass has a corresponding table, which only contains the unique attributes of the subclass. The above is the analysis of the method of processing polymorphism in the Gorm framework and gives the corresponding Java code example.