A fully-featured entity and repository: associations, a named EntityGraph, an enum, audit, indexes, composed Specifications, caching, async, and a custom aggregate query.
The quickstart entities are deliberately minimal. This page shows a realistic one — associations, an enum, an index, a named fetch graph, and audit — and a repository that uses the full query surface.
An enhanced entity
import de.jexcellence.jehibernate.entity.base.UuidEntity;
import jakarta.persistence.*;
import org.hibernate.envers.Audited;
import java.util.ArrayList;
import java.util.List;
@Entity
@Audited // full change history (see Audit)
@Table(
name = "guilds",
indexes = {
@Index(name = "idx_guild_name", columnList = "name", unique = true),
@Index(name = "idx_guild_tier", columnList = "tier")
}
)
@NamedEntityGraph( // fetch members in one query by name
name = "Guild.withMembers",
attributeNodes = @NamedAttributeNode("members")
)
public class Guild extends UuidEntity {
@Column(nullable = false)
private String name;
@Enumerated(EnumType.STRING) // store the name, not the ordinal
@Column(nullable = false, length = 16)
private GuildTier tier = GuildTier.BRONZE;
@ManyToOne(fetch = FetchType.LAZY) // lazy by default; fetch explicitly when needed
@JoinColumn(name = "owner_id")
private PlayerData owner;
@OneToMany(mappedBy = "guild", cascade = CascadeType.ALL, orphanRemoval = true)
private List<GuildMember> members = new ArrayList<>();
protected Guild() {}
public Guild(String name, PlayerData owner) {
this.name = name;
this.owner = owner;
}
public void addMember(GuildMember member) {
members.add(member);
member.setGuild(this);
}
public String getName() { return name; }
public GuildTier getTier() { return tier; }
public void setTier(GuildTier tier) { this.tier = tier; }
public PlayerData getOwner() { return owner; }
public List<GuildMember> getMembers() { return members; }
}
Points worth copying: keep associations LAZY and fetch them on purpose; store enums as STRING; declare the indexes you query on; name a graph for the "load with members" case.
An enhanced repository
A cached repository (hot guild lookups) that uses composed Specifications, the named graph, the query builder, and a custom aggregate. Note the 4.2 inferring constructor — no Class<T> argument.
Composing Specifications
Because a Specification has and, or, and not, you keep a vocabulary of named predicates and combine them per call site:
repo.findAllMatching(predicate) loads the whole table and filters in Java. Use Specifications or query() so the filter runs in SQL; reserve the in-memory predicate for small, cached sets.
Resolving the repository
import static de.jexcellence.jehibernate.repository.query.Specifications.*;
public class GuildRepository extends AbstractCachedRepository<Guild, UUID, UUID> {
public GuildRepository(ExecutorService ex, EntityManagerFactory emf) {
super(ex, emf, Guild::getId);
}
/** Composed specification — reads in SQL, not in memory. */
public List<Guild> activeTopTierGuilds() {
Specification<Guild> topTier = in("tier", List.of(GuildTier.GOLD, GuildTier.DIAMOND));
Specification<Guild> named = isNotNull("name");
return findAll(topTier.and(named));
}
/** Single-query fetch of a guild and its members via the named graph. */
public Optional<Guild> findWithMembers(UUID id) {
return findByIdWithNamedGraph(id, "Guild.withMembers");
}
/** Builder query: largest guilds first, owner joined in. */
public List<Guild> biggestGuilds(int limit) {
return query()
.fetchLeft("owner")
.orderByDesc("tier")
.limit(limit)
.list();
}
/** Custom aggregate the generated API doesn't cover — borrow a session. */
public long totalMemberCount() {
return withReadOnly(ctx -> ctx
.createQuery("select count(m) from GuildMember m", Long.class)
.getSingleResult());
}
/** Off the main thread, then resume on it with your scheduler. */
public CompletableFuture<List<Guild>> topTierAsync() {
return CompletableFuture.supplyAsync(this::activeTopTierGuilds);
}
}
var guilds = jeHibernate.repositories().get(GuildRepository.class);
Guild g = guilds.create(new Guild("Knights", owner));
guilds.findWithMembers(g.getId()).ifPresent(this::render);