Class ORMCreator
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 Summary
ConstructorsConstructorDescriptionORMCreator(org.springframework.beans.factory.BeanFactory beanFactory, ORMProperties ormProperties, List<ORMResolver> ormResolvers) -
Method Summary
Modifier and TypeMethodDescription<T> TCreates an entity instance by automatically detecting the ORM type associated with the given entity class.<T> Tcreate(Class<T> clazz, GenerateType generateType) Creates an entity instance by automatically detecting the ORM type associated with the given entity class.<T> TCreates an entity instance using the generate strategyGenerateType.ALL.<T> Tcreate(ORMType ormType, Class<T> clazz, GenerateType generateType) Creates an entity instance using the explicitly specifiedORMType.Returns all registeredORMResolverimplementations.resolver(ORMProperties ormProperties, List<ORMResolver> ormResolvers) Resolves and creates a map of ORM resolvers based on configuration and available dependencies.
-
Constructor Details
-
ORMCreator
public ORMCreator(org.springframework.beans.factory.BeanFactory beanFactory, ORMProperties ormProperties, List<ORMResolver> ormResolvers)
-
-
Method Details
-
getResolver
Returns all registeredORMResolverimplementations.- Returns:
- a list of available ORM resolvers
- Throws:
GeneratorException- if no resolver is available
-
create
Creates an entity instance by automatically detecting the ORM type associated with the given entity class.
- If the class is annotated with
This allows entity creation to be handled differently depending on the ORM framework managing the given entity class.Entity, Hibernate will be used.
- Otherwise, MyBatis is assumed by default.
- Type Parameters:
T- the type of the entity- Parameters:
clazz- the entity class to instantiategenerateType- the generation strategy to apply (GenerateTypeexceptGenerateType.ALL)- Returns:
- a new entity instance created using the appropriate ORM
- Throws:
GeneratorException- if no ORM resolver is available
-
create
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:cachesstores 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.visitedkeeps track of visited entity paths.
- Type Parameters:
T- the type of the entity- Parameters:
clazz- the root entity class to instantiatecaches- cache map used to store already created instancesvisited- 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
Creates an entity instance using the explicitly specifiedORMType.
Unlikecreate(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 instantiategenerateType- 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 strategyGenerateType.ALL.
Unlikecreate(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 instantiatecaches- cache map used to store already created instancesvisited- 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:- If multiple ORM types are explicitly configured in application.yaml, creates resolvers for all specified types
- If a single ORM type is explicitly configured, creates a resolver for that type only
- 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 typesAuto-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.yamlormResolvers- the list of availableORMResolverimplementations- Returns:
- a map of ORM types to their corresponding resolvers
- Throws:
GeneratorException- if no ORM dependencies are found in the classpath
-