Questions tagged [jpa]

The Java Persistence API (JPA) is a Java specification for accessing, persisting, and managing data between Java objects/classes and a relational database. It is part of the EJB 3.0 specification and is the industry standard approach for Object to Relational Mapping (ORM).

From Wikibooks:

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. JPA is now considered the standard industry approach for Object to Relational Mapping (ORM) in the Java Industry.

Examples

The current version of the JPA specification is JPA 2.1 (JSR 338), released on 22 April 2013. It includes several improvements, such as type-converter and stored procedures.

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.

45932 questions
992
votes
15 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
  • 297,790
  • 101
  • 558
  • 777
846
votes
6 answers

What is difference between CrudRepository and JpaRepository interfaces in Spring Data JPA?

What is the difference between CrudRepository and JpaRepository interfaces in Spring Data JPA? When I see the examples on the web, I see them there used kind of interchangeably. What is the difference between them? Why would you want to use one…
kseeker
  • 8,503
  • 3
  • 13
  • 7
698
votes
31 answers

How to fix the Hibernate "object references an unsaved transient instance - save the transient instance before flushing" error

I receive following error when I save the object using Hibernate object references an unsaved transient instance - save the transient instance before flushing
Tushar Ahirrao
  • 10,773
  • 16
  • 61
  • 95
680
votes
22 answers

What's the difference between JPA and Hibernate?

I understand that JPA 2 is a specification and Hibernate is a tool for ORM. Also, I understand that Hibernate has more features than JPA 2. But from a practical point of view, what really is the difference? I have experience using iBatis and now…
Anthony
  • 27,490
  • 32
  • 129
  • 238
609
votes
16 answers

Difference between FetchType LAZY and EAGER in Java Persistence API?

I am a newbie to Java Persistence API and Hibernate. What is the difference between FetchType.LAZY and FetchType.EAGER in Java Persistence API?
leon
  • 8,725
  • 18
  • 57
  • 73
557
votes
8 answers

JPA JoinColumn vs mappedBy

What is the difference between: @Entity public class Company { @OneToMany(cascade = CascadeType.ALL , fetch = FetchType.LAZY) @JoinColumn(name = "companyIdRef", referencedColumnName = "companyId") private List branches; …
Mike
  • 17,033
  • 22
  • 85
  • 113
515
votes
16 answers

Hibernate throws MultipleBagFetchException - cannot simultaneously fetch multiple bags

Hibernate throws this exception during SessionFactory creation: org.hibernate.loader.MultipleBagFetchException: cannot simultaneously fetch multiple bags This is my test case: Parent.java @Entity public Parent { @Id …
blow
  • 11,391
  • 23
  • 66
  • 104
435
votes
4 answers

Create the perfect JPA entity

I've been working with JPA (implementation Hibernate) for some time now and each time I need to create entities I find myself struggling with issues as AccessType, immutable properties, equals/hashCode, ... . So I decided to try and find out the…
Stijn Geukens
  • 14,803
  • 7
  • 60
  • 100
337
votes
19 answers

The JPA hashCode() / equals() dilemma

There have been some discussions here about JPA entities and which hashCode()/equals() implementation should be used for JPA entity classes. Most (if not all) of them depend on Hibernate, but I'd like to discuss them JPA-implementation-neutrally (I…
MRalwasser
  • 14,580
  • 14
  • 95
  • 134
325
votes
8 answers

Why does JPA have a @Transient annotation?

Java has the transientkeyword. Why does JPA have @Transient instead of simply using the already existing java keyword?
deamon
  • 78,414
  • 98
  • 279
  • 415
318
votes
10 answers

What is the easiest way to ignore a JPA field during persistence?

I'm essentially looking for a "@Ignore" type annotation with which I can stop a particular field from being persisted. How can this be achieved?
m2o
  • 5,605
  • 6
  • 25
  • 23
269
votes
8 answers

Hibernate SessionFactory vs. JPA EntityManagerFactory

I am new to Hibernate and I'm not sure whether to use a Hibernate SessionFactory or a JPA EntityManagerFactory to create a Hibernate Session. What is the difference between these two? What are the pros & cons of using each of those?
elpisu
  • 2,695
  • 3
  • 13
  • 4
268
votes
19 answers

PersistentObjectException: detached entity passed to persist thrown by JPA and Hibernate

I have a JPA-persisted object model that contains a many-to-one relationship: an Account has many Transactions. A Transaction has one Account. Here's a snippet of the code: @Entity public class Transaction { @Id @GeneratedValue(strategy =…
Paul Sanwald
  • 9,766
  • 4
  • 38
  • 58
267
votes
17 answers

Setting default values for columns in JPA

Is it possible to set a default value for columns in JPA, and if, how is it done using annotations?
homaxto
  • 4,734
  • 6
  • 33
  • 45
253
votes
4 answers

Confusion: @NotNull vs. @Column(nullable = false) with JPA and Hibernate

When they appear on a field/getter of an @Entity, what is the difference between them? (I persist the Entity through Hibernate). What framework and/or specification each one of them belongs to? @NotNull is located within…
rapt
  • 10,528
  • 29
  • 92
  • 134
1
2 3
99 100