AutoSkill hibernate_generic_loader_with_optional_fetch
Create a generic Java method using Hibernate/JPA Criteria API to load all entities of a given type, conditionally applying a left-join fetch on a specified field to optimize loading and prevent N+1 issues.
install
source · Clone the upstream repo
git clone https://github.com/ECNU-ICALK/AutoSkill
Claude Code · Install into ~/.claude/skills/
T=$(mktemp -d) && git clone --depth=1 https://github.com/ECNU-ICALK/AutoSkill "$T" && mkdir -p ~/.claude/skills && cp -r "$T/SkillBank/ConvSkill/english_gpt3.5_8_GLM4.7/hibernate_generic_loader_with_optional_fetch" ~/.claude/skills/ecnu-icalk-autoskill-hibernate-generic-loader-with-optional-fetch && rm -rf "$T"
manifest:
SkillBank/ConvSkill/english_gpt3.5_8_GLM4.7/hibernate_generic_loader_with_optional_fetch/SKILL.mdsource content
hibernate_generic_loader_with_optional_fetch
Create a generic Java method using Hibernate/JPA Criteria API to load all entities of a given type, conditionally applying a left-join fetch on a specified field to optimize loading and prevent N+1 issues.
Prompt
Role & Objective
Act as a Java/Hibernate expert. Write a generic method
loadAllData that retrieves all entities of a specified type using the JPA Criteria API.
Operational Rules & Constraints
- The method signature must be
.public <T> List<T> loadAllData(Class<T> type, String fieldName) - Use
to obtain a session.HibernateUtil.getSessionFactory().openSession() - Use
andCriteriaBuilder
to construct the query.CriteriaQuery - Conditional Fetching Logic:
- If the
argument is not an empty String, apply a left-join fetch usingfieldName
.root.fetch(fieldName, JoinType.LEFT) - If
is empty, load data without applying any specific fetch (standard load).fieldName
- If the
- Ensure the generic type
is handled correctly in the CriteriaQuery construction (e.g., usingT
).criteria.select(root) - Return a
.List<T>
Anti-Patterns
- Do not use EntityGraph or query hints (
); use the Criteria APIsetHint
method.fetch - Do not hardcode specific entity class names (e.g., AlphaClass, BetaClass) into the method logic.
- Do not use HQL or native SQL strings; use the Criteria API.
- Do not throw exceptions for empty
; handle it gracefully by skipping the fetch.fieldName
Triggers
- modify loadAllData to use left join fetch
- generic hibernate loader with optional fetch
- java hibernate load all with join argument
- create a generic method with optional field fetch
- prevent n+1 with optional criteria fetch