Package mocka.orm.generator
Class ORMResolver
java.lang.Object
mocka.orm.generator.ORMResolver
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 Summary
ConstructorsConstructorDescriptionORMResolver(org.springframework.beans.factory.BeanFactory beanFactory, ORMProperties ormProperties, List<ORMCreator> ormCreators) -
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 registeredORMCreatorinstances.resolver(ORMProperties ormProperties, List<ORMCreator> ormCreators) Resolves and creates a map of ORM resolvers based on configuration and available dependencies.
-
Constructor Details
-
ORMResolver
public ORMResolver(org.springframework.beans.factory.BeanFactory beanFactory, ORMProperties ormProperties, List<ORMCreator> ormCreators)
-
-
Method Details
-
getCreators
Returns all registeredORMCreatorinstances.- Returns:
- a list of available ORM creators
- Throws:
GeneratorException- if no ORM creators are registered
-
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 entity type- Parameters:
clazz- the entity class to instantiategenerateType- the generation strategy (excludingGenerateType.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 entity type- 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
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.yamlormCreators- the list of availableORMCreatorimplementations- Returns:
- a map of ORM types to their corresponding resolvers
- Throws:
GeneratorException- if no ORM dependencies are found in the classpath
-