Questions tagged [java-persistence-api]

The 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.

The Java Persistence API provides Java developers with an object/relational mapping facility for managing relational data in Java applications. Java Persistence consists of four areas:

  • The Java Persistence API
  • The query language
  • The Java Persistence Criteria API
  • Object/relational mapping metadata
11 questions
1054
votes
16 answers

JPA EntityManager: Why use persist() over merge()?

EntityManager.merge() can insert new objects and update existing ones. Why would one want to use persist() (which can only create new objects)?
Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
161
votes
7 answers

Which annotation should I use: @IdClass or @EmbeddedId

The JPA (Java Persistence API) specification has 2 different ways to specify entity composite keys: @IdClass and @EmbeddedId. I'm using both annotations on my mapped entities, but it turns out to be a big mess to people who aren't very familiar…
sakana
  • 4,251
  • 5
  • 25
  • 20
12
votes
1 answer

Spring jpa criteriaBuilder search in a list

I have a book class with a list of authors: @Entity @Table(name = "book") public class Book extends Content { @ManyToMany(fetch = FetchType.LAZY) private List authors; ...} Now, this is my BookSpecifications class: public static…
CVV
  • 451
  • 2
  • 7
  • 21
5
votes
3 answers

Java persistence mapped superclass with optional properties

I'm using the javax.persistence package to map my Java classes. I have entities like these: public class UserEntity extends IdEntity { } which extends a mapped superclass named IdEntity: @MappedSuperclass public class IdEntity extends…
henrik
  • 1,558
  • 3
  • 14
  • 29
1
vote
2 answers

JPA: Resolve race condition for write after read

I want to store unique entities with their hashes. An entity consists of a generated id and an hash value which must be unique. I do this with this code: @Stateless @LocalBean public class srvcEntity { @PersistenceContext(unitName = "mine") …
Ben
  • 366
  • 1
  • 13
1
vote
1 answer

Namespace prefix sap for aggregation-role on Property is not defined

I am trying to view metadata using URL localhost:8080/a_final/a.svc/$metadata. This is JPA based project and uses olingo OData . I am getting below error - There is no clear answer on web. It would be good if I can know about the mistake.
Grv
  • 41
  • 2
  • 7
1
vote
2 answers

Insertable object in a class with @IdClass annotation

Is it possible to do a cascade save in Section.class? I create Section object and add new questions without id. When I try save it I get error: org.postgresql.util.PSQLException: ERROR: insert or update on table "question_to_section" violates…
luke
  • 13
  • 4
1
vote
2 answers

JPA : Entity extend with entity

How can I extend an entity with another entity but both of them referring to the same table ? Is it possible ? The structure is something like this : @Entity @Table(name = "users") @NamedQuery(name="User.findAll", query="SELECT u FROM User…
Mrye
  • 699
  • 3
  • 10
  • 31
1
vote
1 answer

JPA+HIBERNATE: cartesian product in Many-to-Many relationship

I try to explain the problem. I have an entity with ManyToMany relationship @Entity @Table(name="TABLE1") public class Table1 implements Serializable { ... //bi-directional many-to-many association to Table1 @ManyToMany @JoinTable( …
Fra83
  • 511
  • 1
  • 5
  • 15
0
votes
1 answer

Criteria API join: java.lang.IllegalArgumentException: Unable to resolve attribute

I'm trying to make a simple join operation via jpa criteria api, but I'm getting an error: java.lang.IllegalArgumentException: Unable to resolve attribute [Companies] against path at…
Nesquik27
  • 234
  • 1
  • 7
  • 18
0
votes
2 answers

How to get a specific "row" from a DB using JPA (EclipseLink) when the primary key is Auto Incremented?

Say I have this table CREATE TABLE person( person_id INT AUTO_INCREMENT PRIMARY KEY, first_name VARCHAR(50), last_name VARCHAR(50), x_cordinates INT, y_cordinates INT ); In the past I have used Person person =…
Nesquikk M
  • 121
  • 3
  • 12