
Vlad Mihalcea · 16 min readJava & Kotlin
How to use PostgreSQL pgvector with JPA, Hibernate, and Spring AI
Learn how to map PostgreSQL pgvector columns to JPA entities using Hypersistence Utils, generate embeddings with Spring AI and Ollama, and perform semantic search with HNSW indexes. The guide covers schema setup, embedding generation, and efficient transaction handling.
- Use @Type(PostgreSQLVectorType.class) to map float[] to vector(1024) columns.
- Generate embeddings in bulk with Ollama’s embed(List) to avoid per-call latency.
- Create HNSW indexes via Hibernate scripts to enable fast cosine distance queries.