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
14
votes
5 answers

JPA: How do I specify the table name corresponding to a class at runtime?

(note: I'm quite familiar with Java, but not with Hibernate or JPA - yet :) ) I want to write an application which talks to a DB2/400 database through JPA and I have now that I can get all entries in the table and list them to System.out (used…
Thorbjørn Ravn Andersen
  • 73,784
  • 33
  • 194
  • 347
14
votes
5 answers

what is better: getSingleResult, or getResultList JPA

i need to retrieve single row from table, and i was interested what approach is better. On the one side getSingleResult is designed for retrieving single result, but it raises exception. Does this method have benefit in performance related to…
Andrey
  • 810
  • 1
  • 9
  • 20
14
votes
3 answers

Reading from multiple Db's with same Persistence Unit?

I need some help to configure several connection's to multiple db's using the same Persistence unit. They all have the same schema. Therefore I want to use the same Persistence unit/ DAO's etc and dont want to have to setup 10 EntityManagers, 10…
Michael W
  • 3,515
  • 8
  • 39
  • 62
14
votes
2 answers

Using IN clause in a native sql query

We are trying to dynamically generate an IN clause for a native sql query to return a JPA entity. Hibernate is our JPA provider. Our code looks something like this. @NamedQuery( name="fooQuery", queryString="select f from Foo f where…
VerrDon Mason
14
votes
2 answers

ORDER BY "CASE column" in JPA

I want to ORDER BY the case statement, is it possible? How can I do it? SELECT new com.systemname.to.UserDataHolder(u.userId, CASE WHEN (u.place.cityId = :cityId) THEN 1 WHEN (u.place.stateId = :stateId) THEN 2 ELSE 3 END) FROM User u ORDER…
Gondim
  • 3,038
  • 8
  • 44
  • 62
14
votes
1 answer

Hibernate JPA: How to control "Not-Found" behavior when lazy loading a ManyToOne relation?

In older xml based hibernate, one can have this config , but in JPA annotations, there seems to be nothing similar. My problem is that our company has SQL REFERENCE definitions like this: authorId INT NOT NULL…
Visus Zhao
  • 1,144
  • 2
  • 12
  • 25
14
votes
7 answers

How to properly determine whether an "exists" JPA Criteria Query clause returned true or false?

I don't know how to perform a JPA criteria query that returns with a boolean output. The goal is to have a criteria query that looks like this when rendered on Oracle: select 1 from dual where exists ( ... ); The where exists (...) part I performed…
Edy Bourne
  • 5,679
  • 13
  • 53
  • 101
14
votes
1 answer

NamedQuery: IllegalArgumentException (Query not found) after externalizing entities

I have successfully used javax.persistence.NamedQuery in combination with JPA2. The named queries are defined at the top of the entity class files, and are used from Stateless EJBs (entity facade). Now I had to extract the entity class files into a…
Hank
  • 4,597
  • 5
  • 42
  • 84
14
votes
2 answers

Generate DDL for JPA 2.0 with EclipseLink

I've created my model classes with JPA 2.0 annotations. At runtime, I will use EclipseLink 2.2.0, but I've been careful to use only pure JPA annotations in my model classes. Now, how do I generate the schema DDL for the database ? I want to use the…
Leonel
  • 28,541
  • 26
  • 76
  • 103
14
votes
4 answers

JPA: please help understanding "join fetch"

I have the following entity structure: Business --> Campaign --> Promotion, where ONE Business can have MANY Campaigns and ONE Campaign can have MANY Promotions. Both one-to-many relations are declared as LAZY. One place in my code, I need to…
Tong Wang
  • 1,602
  • 4
  • 29
  • 36
14
votes
5 answers

JPA Repository.findById() returns null but the value is exist on db

I'm developing Spring boot project, using JPA. What I wanna know is repository.findById(id) method returns null, whereas data is available in the DB. Functions save() and findAll() are working fine. When I ran the same code on junit test…
Jin-guk Park
  • 221
  • 1
  • 3
  • 12
14
votes
5 answers

How can I obtain the first row from a SELECT operation using JPQL?

I have a database with data about some tests, I want to order them decently using their attribute DATE and take only the first one of all of them. I mean, the equivalent TOP 1 of SQL in JPQL. Thanks for your help!
14
votes
5 answers

Query to get nextval from sequence with Spring JPA

I have a repository with which I am trying to get next value from sequence. The sequence name I need to be parameterized. The repository query looks a like: @Query(value = "SELECT ?1.NEXTVAL from dual;", nativeQuery = true) String…
Ashish Chauhan
  • 486
  • 2
  • 8
  • 22
14
votes
2 answers

PostgreSQL throws "column is of type jsonb but expression is of type bytea" with JPA and Hibernate

This is my entity class that is mapped to a table in postgres (9.4) I am trying to store metadata as jsonb type in the database @Entity @Table(name = “room_categories”) @TypeDef(name = “jsonb”, typeClass = JsonBinaryType.class) public class…
Arihant Jain
  • 141
  • 1
  • 1
  • 3
14
votes
1 answer

Content gets appended to DDL files generated via JPA script generation instead of being replaced

Since updating to Spring Boot 2, I noticed a change in behavior in the way the DDL files are generated when using the: spring.jpa.properties.javax.persistence.schema-generation.scripts.* options. Previously, in Spring Boot 1.5, every time I was…
edoDev
  • 551
  • 1
  • 4
  • 20