Class ORMCreator

java.lang.Object
mocka.generator.orm.ORMCreator

@Component public class ORMCreator extends Object
ORMCreator is responsible for managing and delegating entity creation to the appropriate ORMResolver based on the configured ORM type(s).

It supports multiple ORM frameworks (Hibernate and MyBatis) and determines which resolver to use either automatically (via entity annotations) or explicitly (via ORMType parameter).

This class is registered as a Spring Component and is constructed with available ORM beans and configuration properties.

  • Constructor Details

    • ORMCreator

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

    • getResolver

      public List<ORMResolver> getResolver()
      Returns all registered ORMResolver implementations.
      Returns:
      a list of available ORM resolvers
      Throws:
      GeneratorException - if no resolver is available
    • 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 type of the entity
      Parameters:
      clazz - the entity class to instantiate
      generateType - the generation strategy to apply (GenerateType except 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 type of the entity
      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,ORMResolver> resolver(ORMProperties ormProperties, List<ORMResolver> ormResolvers)
      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
      ormResolvers - the list of available ORMResolver implementations
      Returns:
      a map of ORM types to their corresponding resolvers
      Throws:
      GeneratorException - if no ORM dependencies are found in the classpath