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.
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:
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));