How do you implement a native query in JPA?
Run native SQL in JPA using EntityManager or Spring Data @Query. Covers parameters, DTO and projection mapping, pagination, updates and SQL injection safety.
Spread Your Knowledge. Questions with answers, tutorials and articles, newest first.
Run native SQL in JPA using EntityManager or Spring Data @Query. Covers parameters, DTO and projection mapping, pagination, updates and SQL injection safety.
JPA offers three ways to map a class hierarchy to tables. See code for each, the resulting schema, performance trade-offs and when to pick which.
JPA maps entity hierarchies with @Inheritance and three strategies. See the schema each one creates and how to choose between them.
@Embeddable maps a value class into the table of the entity that owns it. See embedded value objects, column overrides and composite keys with working examples.
Learn JPA cascade types, orphanRemoval and a working Order and OrderItem example, plus which relationships should cascade and which should not.
@ManyToOne maps many child entities to one parent via a foreign key. See fetch types, @JoinColumn, bidirectional mapping and N+1 pitfalls.
@JoinColumn maps the foreign key column of a JPA association. See many-to-one, one-to-many, one-to-one, composite keys and when to use @JoinTable instead.
How @ManyToMany and @JoinTable map a many-to-many relationship in JPA, with unidirectional and bidirectional examples, cascade rules and fetching advice.
Map a JPA one-to-many relationship the right way: bidirectional @OneToMany and @ManyToOne, cascade, orphanRemoval and lazy fetching with examples.
Map one-to-one relationships in JPA using a foreign key, mappedBy or a shared primary key with @MapsId, and avoid common lazy loading pitfalls.
@GeneratedValue lets JPA generate primary keys. Compare the AUTO, IDENTITY, SEQUENCE, TABLE and UUID strategies and learn which one fits your database.
Learn how to define simple, generated and composite primary keys in a JPA entity, and how to choose between IDENTITY, SEQUENCE and UUID.
@Table maps a JPA entity to a specific table and sets its schema, unique constraints and indexes. Examples use Jakarta Persistence and Hibernate.
JPA is a specification and Hibernate is an implementation of it. See how they relate, how Spring Data JPA fits in and when to use Hibernate-only features.
Learn how FetchType.LAZY works in JPA and Hibernate, why LazyInitializationException happens and how to fetch data with entity graphs, JOIN FETCH and DTOs.
The JPA EntityManager manages the persistence context and entity lifecycle. This guide covers CRUD, dirty checking, queries and thread safety.
Learn JPA CRUD with EntityManager and Spring Data JpaRepository: persist, find, update through dirty checking, delete, pagination and transaction handling.
JPA repositories are interfaces that Spring Data JPA implements for you, giving CRUD, query methods and paging with no boilerplate.
@Entity makes a Java class a managed JPA entity mapped to a table. Learn the requirements, lifecycle states and the difference from @Table.
Learn how to define an entity class in JPA with @Entity and @Id, customize columns, generate keys and map relationships on Jakarta Persistence.