Questions tagged [jpa]

The Jakarta Persistence API (formerly Java Persistence API) (JPA) is a Java specification for accessing, persisting, and managing data between Java objects/classes and a relational database. JPA was defined as part of the EJB 3.0 specification as a replacement for the EJB 2 CMP Entity Beans specification. JPA is now considered the standard industry approach for Object to Relational Mapping (ORM) in the Java Industry.

The excerpt above is from Wikibooks:

Examples

The current version of the JPA specification is JPA 3.1, released on Apr 06, 2022.

Useful links

Related tags

FAQ

Spring Data JPA Update @Query not updating? While this question is about Spring Data JPA the underlying problem is about understanding the 1st level cache of JPA. See also the many duplicates pointing to that question.

One to many relationship doesn't work Spring boot jpa Again, asked in the context of Spring Data JPA it is really about JPA. It describes a common problem/misunderstanding with regard to bidirectional relationships.

51422 questions
13
votes
2 answers

Create database with JPA?

When I create (using JPA - java persistence api) a persistence unit and then persistence entities they auto create the corresponding tables and fields in the database. Can I also make it to auto create the database (if it doesn't exist)? My objectif…
Koko
  • 131
  • 1
  • 1
  • 3
13
votes
6 answers

Exclude column from resultset in controller | Spring data jpa

I have a Users Entity: public class SpringUsers implements Serializable { private String password; // other fields omitted @Basic @Column(name = "password") public String getPassword() { return password; } public…
baao
  • 71,625
  • 17
  • 143
  • 203
13
votes
1 answer

JPA mapping a map where key is an Enum

I'm trying to create an entity where one of the field is a Map with Enum key: public class MyEntity { @ElementCollection @CollectionTable(name="attributes", joinColumns=@JoinColumn(name="my_entity_id")) @MapKeyColumn(name =…
DruidKuma
  • 2,352
  • 2
  • 17
  • 20
13
votes
1 answer

Defining multiple Address properties in Person per AddressType by a triple join table

I've here a database with a PERSON - ADDRESS - ADDRESS_TYPE relationship maintained by a triple join table PERSON_ADDRESS. The PERSON-ADDRESS relationship is effectively one-to-many. PERSON ID FIRSTNAME LASTNAME -- --------- -------- 1 John …
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
13
votes
1 answer

Storing Enum Values with JPA

Say I have an enum: public enum NotificationType { Store("S"), Employee("E"), Department("D"), All("A"); public String value; NotificationType(String value) { this.value = value; } } I want to store S or E…
Gregg
  • 34,973
  • 19
  • 109
  • 214
13
votes
8 answers

Internal HSQL database complains about privileges

I'm setting up a standalone Java service with an in-process, in-memory HSQL database. Persistence.xml
Bart van Heukelom
  • 43,244
  • 59
  • 186
  • 301
13
votes
1 answer

Why Hibernate sometimes ignores FetchMode.JOIN?

I have an entity with a @ManyToOne relation, which I'd like to retrieve with a single query, thus using @Fetch(FetchMode.JOIN). Sometimes Hibernate doesn't respect that and issues N+1 SELECTs. With sometimes I mean that since I don't know what…
Giovanni Lovato
  • 2,183
  • 2
  • 29
  • 53
13
votes
1 answer

Deleting JPA object fails due to foreign key constraints?

Why would the following query fail due to a foreign key constraint? There is no other way for me to delete the associated data that I am aware of. Query query=em.createQuery("DELETE FROM…
Jay
  • 19,649
  • 38
  • 121
  • 184
13
votes
2 answers

JPA Best Practice: Static Lookup Entities

Imagine, an Event entity references a Status Entity: @Entity @Table(name = "event") public class Event() { @Id @Column(name = "id", nullable = false) private long id; ... @ManyToOne @JoinColumn(name = "status_code", nullable = false) …
Jan
  • 9,397
  • 13
  • 47
  • 52
13
votes
1 answer

@BatchSize a smart or stupid use?

First I'll explain how I understood and use @BatchSize : @BatchSize is made in order to load relations of objects in batch, making less SQL request to the database. This is specially usefull on LAZY @OneToMany relations. However it's even useful on…
Walfrat
  • 5,363
  • 1
  • 16
  • 35
13
votes
4 answers

Select row with most recent date per user with 1 condition in JPA

I have this Entity and I want to list for each device the last event with the property message = 1 @Entity @Table(name = "t_device_event") @NamedQueries(value = { @NamedQuery(name = "DeviceEvent.findWithMessageActive", query = "from…
Nuñito Calzada
  • 4,394
  • 47
  • 174
  • 301
13
votes
6 answers

JPA: which side should be the owning side in a m:n relationship?

Say, for example, I had two entities: Article and Tag (like in a typical blog). Each article can have many tags, and each tag can be used by many articles, so it is a classical m:n relationship. I need to specify an owning side with JPA. But which…
deamon
  • 89,107
  • 111
  • 320
  • 448
13
votes
1 answer

Why "SELECT c" instead of "SELECT * " in JPQL?

Example query in JPQL looks like this: SELECT c FROM Customer c; I read JPA specification, books and I can't find what this c means. In SQL we simply write: SELECT * FROM customer; I know we can use this c as alias i.e. in WHERE clause like…
swch
  • 1,432
  • 4
  • 21
  • 37
13
votes
1 answer

JPA 2.0 Criteria and grouping of Predicates

I encounter problem with Hibernate EntityManager 3.5.3-Final when it comes to composite predicates. Example (not actual code snippet, but the idea should be clear): CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); Predicate…
Jarek Rozanski
  • 780
  • 1
  • 6
  • 13
13
votes
2 answers

SpringDataJPA: custom data mapping with Native Query

public interface UserRepository extends JpaRepository { @Query(value = "SELECT * FROM USERS WHERE EMAIL_ADDRESS = ?0", nativeQuery = true) User findByEmailAddress(String emailAddress); } Let's say I have the code above where I…
topcan5
  • 1,511
  • 8
  • 30
  • 54
1 2 3
99
100