Class ORMResolver

java.lang.Object
mocka.orm.generator.ORMResolver

@Component public class ORMResolver extends Object
ORMSelector is a facade component responsible for selecting the appropriate ORMCreator and delegating entity creation to it.

It supports multiple ORM frameworks (currently Hibernate and MyBatis) and determines which ORMCreator to use either:

  • automatically, based on entity annotations
  • explicitly, based on a provided ORMType

This class does not create entities itself. It only coordinates and delegates creation requests to the resolved ORMCreator.

  • Constructor Details

    • ORMResolver

      public ORMResolver(org.springframework.beans.factory.BeanFactory beanFactory, ORMProperties ormProperties, List<ORMCreator> ormCreators)
  • Method Details

    • getCreators

      public List<ORMCreator> getCreators()
      Returns all registered ORMCreator instances.
      Returns:
      a list of available ORM creators
      Throws:
      GeneratorException - if no ORM creators are registered
    • create

      public <T> T create(Class<T> clazz, GenerateType generateType)
      Creates an entity instance by automatically detecting the ORM type associated with the given entity class.

      - If the class is annotated with Entity, Hibernate will be used.
      - Otherwise, MyBatis is assumed by default.

      This allows entity creation to be handled differently depending on the ORM framework managing the given entity class.
      Type Parameters:
      T - the entity type
      Parameters:
      clazz - the entity class to instantiate
      generateType - the generation strategy (excluding GenerateType.ALL)
      Returns:
      a new entity instance created using the appropriate ORM
      Throws:
      GeneratorException - if no ORM resolver is available
    • create

      public <T> T create(Class<T> clazz, Map<String,Object> caches, Set<VisitedPath> visited)
      Creates an entity instance by automatically detecting the ORM type associated with the given entity class.

      This method generates not only the specified entity but also all related entities (both parent and child relationships) recursively.
      To prevent infinite recursion and duplicate creations during traversal:

      • caches stores already created instances. If a field create call is invoked for a class that has already been created, the cached instance will be reused instead of creating a new one.
      • visited keeps track of visited entity paths.
      Type Parameters:
      T - the entity type
      Parameters:
      clazz - the root entity class to instantiate
      caches - cache map used to store already created instances
      visited - set of visited paths to prevent infinite recursion
      Returns:
      a fully populated entity instance created using the appropriate ORM
      Throws:
      GeneratorException - if no ORM resolver is available
    • create

      public <T> T create(ORMType ormType, Class<T> clazz, GenerateType generateType)
      Creates an entity instance using the explicitly specified ORMType.
      Unlike create(Class, GenerateType), this method doesn't attempt to auto-detect thr ORM from the entity class. Instead, the caller must provide the target ORMType directly.

      The rest is the same as create(Class, GenerateType).

      Type Parameters:
      T - the type of the entity
      Parameters:
      ormType - the ORM type to use (e.g., ORMType)
      clazz - the entity class to instantiate
      generateType - the generation strategy to apply (e.g., SINGLE, CHILD, PARENT, ALL)
      Returns:
      a new entity instance created using the specified ORM
      Throws:
      GeneratorException - if the given ORM type has no registered resolver
    • create

      public <T> T create(ORMType ormType, Class<T> clazz, Map<String,Object> caches, Set<VisitedPath> visited)
      Creates an entity instance using the generate strategy GenerateType.ALL.
      Unlike create(Class, Map, Set), this method doesn't attempt to auto-detect thr ORM from the entity class. Instead, the caller must provide the target ORMType directly.

      The rest is the same as create(Class, Map, Set).

      Type Parameters:
      T - the type of the entity
      Parameters:
      ormType - the ORM type to use (e.g., ORMType)
      clazz - the root entity class to instantiate
      caches - cache map used to store already created instances
      visited - set of visited paths to prevent infinite recursion
      Returns:
      a fully populated entity instance created using the appropriate ORM
      Throws:
      GeneratorException - if the given ORM type has no registered resolver
    • resolver

      public Map<ORMType,ORMCreator> resolver(ORMProperties ormProperties, List<ORMCreator> ormCreators)
      Resolves and creates a map of ORM resolvers based on configuration and available dependencies. This method determines which ORM frameworks to use by following this priority order:
      1. If multiple ORM types are explicitly configured in application.yaml, creates resolvers for all specified types
      2. If a single ORM type is explicitly configured, creates a resolver for that type only
      3. If no configuration is provided, auto-detects available ORM frameworks by scanning for beans

      Configuration Example (application.yaml):

       mocka:
         orm-type:
            type: [MYBATIS, HIBERNATE]  # Explicitly specify ORM types
       

      Auto-detection:

      When no configuration is provided, the method scans the Spring context for:
      • MyBatis: checks for bean named "sqlSessionFactory"
      • Hibernate: checks for bean named "entityManagerFactory"
      Parameters:
      ormProperties - the ORM configuration properties from application.yaml
      ormCreators - the list of available ORMCreator implementations
      Returns:
      a map of ORM types to their corresponding resolvers
      Throws:
      GeneratorException - if no ORM dependencies are found in the classpath