Introduction
Changelog
Contact
Guides
Getting Started
Entities
Repositories
Queries & Specifications
Configuration & Pooling
Migrations
Spring Boot
Spigot & Paper Plugins
Recipes
Reference
Databases
Troubleshooting
Advanced
Multi-tenancy
Audit Trail
Testing
TrademarkTrademark
Ctrl k
Search…
Get started
Introduction
Changelog
Contact
Guides
Getting Started
Entities
Repositories
Queries & Specifications
Configuration & Pooling
Migrations
Spring Boot
Spigot & Paper Plugins
Recipes
Reference
Databases
Troubleshooting
Advanced
Multi-tenancy
Audit Trail
Testing
TrademarkTrademark© Aveiro. All rights reserved.
Built with Aveiro
Guide

Repositories

CRUD, batch, async, and paginated operations from a single base repository, plus cached repositories and session access.
Updated last month
Entities
Queries & Specifications
A repository is a class extending AbstractCrudRepository<Entity, IdType>. You get a full operation set without writing queries; resolve instances through jeHibernate.repositories().get(...). Since 4.2 the constructor can omit the Class<T> argument — the entity type is inferred.
public class WarpRepository extends AbstractCrudRepository<Warp, Long> {
    public WarpRepository(ExecutorService executor, EntityManagerFactory emf) {
        super(executor, emf);
    }
}

CRUD

Warp warp   = repo.create(new Warp("spawn"));
Optional<Warp> maybe = repo.findById(1L);
Warp orThrow = repo.findByIdOrThrow(1L);        // throws if absent
Warp ensured = repo.findByIdOrCreate(1L, () -> new Warp("spawn"));
warp.setName("hub");
Warp saved  = repo.save(warp);                   // insert or update
repo.delete(1L);
boolean has = repo.exists(1L);
long total  = repo.count();
save inserts when the entity is new and updates otherwise. createOrUpdate(entity, Entity::getId) is the explicit form when you build a detached entity yourself.

Batch

List<Warp> created = repo.createAll(List.of(new Warp("a"), new Warp("b")));
List<Warp> many    = repo.findAllById(List.of(1L, 2L, 3L));
repo.saveAll(updatedList);
repo.deleteAll(List.of(1L, 2L));
Batch operations use Hibernate's JDBC batching (ordered inserts/updates) configured by the pool.

Pagination

List<Warp> firstPage = repo.findAll(0, 20);   // pageNumber, pageSize
For total counts and richer paging, use the query builder's getPage(...) (see Queries & Specifications).

Async — never block the main thread

Every core operation has a CompletableFuture variant. On a Minecraft server, call these off the main thread and resume on it with your scheduler.
On Java 21+ these run on virtual threads automatically.

Cached repositories

For hot, read-mostly lookups (online players, config), extend AbstractCachedRepository<Entity, IdType, Key> to get an in-memory cache backed by Caffeine. The entity type is inferred here too; you only pass the key extractor.

Direct session access

When you need the EntityManager (a custom query, a multi-step unit of work), borrow a session without leaking it:
repo.findByIdAsync(id).thenAccept(opt -> opt.ifPresent(this::show));
repo.createAsync(new Warp("spawn"));
repo.findAllAsync(0, 50).thenAccept(this::render);
repo.deleteAsync(id);
public class PlayerRepository extends AbstractCachedRepository<PlayerData, UUID, UUID> {
    public PlayerRepository(ExecutorService ex, EntityManagerFactory emf) {
        super(ex, emf, PlayerData::getId);   // key extractor
    }
}

Optional<PlayerData> p = players.findByKey(uuid);  // served from cache
players.preload();                                 // warm the cache
players.evictById(uuid);
int size = players.getCacheSize();
var result = repo.withSession(ctx -> ctx
    .createQuery("select w from Warp w where w.name like :p", Warp.class)
    .setParameter("p", "spawn%")
    .getResultList());

// ctx also exposes find/persist/merge/remove, query(Class), nativeQuery(...),
// and getEntityManager() when you need the raw EntityManager.
repo.withReadOnly(ctx -> ctx.find(Warp.class, 1L).orElse(null));