Last Updated on: (senast uppdaterad på:)
Database interactions in a Java EE application are done through JPA.
- JPA is a specification that defines the management of relational data in a Java application.
- JPA is a specification and needs an implementation
- JPA has concepts that an implementation must cover.
- Entity
through the javax.persistence.Entity class - Field Persistence
All fields in the object are persisted unless annotated with @Transient. - Relationships
JPA specifies how we should manage relationships between different database tables. @OneToOne, @OneToMany, @ManyToOne, @ManyToMany - EntityManager
through the javax.persistence.EntityManager class —
EntityManager contains common Create, Read, Update and Delete (CRUD) operations that are “persisted” to the database.
- Entity
JPA has implementations available such as Hibernate and EclipseLink. Otherwise there is a lot of code to write classes for the above. Benchmarks for the implementations are available.
Hibernate is one of the mature implementations (described below). EclipseLink is another implementation, which is open source,
supports a number of other persistence standards such as Java Architecture for XML Binding (JAXB) and has an XML Representation. EclipseLink has extensions of its own such as @ReadOnly, @Struct while not having an annotation such as @BatchSize which is featured in Hibernate.
Hibernate notes (part 1)
This useful information was found at https://www.baeldung.com/jpa-hibernate-difference
- with @Entity annotation it uses:
@org.hibernate.annotations.Entity - has also annotations that extend the functionality of @Entity:
@Table — specify name of table
@BatchSize — specify batch size when retrieving entities - has few extra features that JPA does not specify that may prove useful in larger applications:
- Customizable CRUD statements with @SQLInsert, @SQLUpdate, @SQLDelete annotations.
- Support for soft deleting
- Immutable entities with @Immutable annotation
Hibernate notes (part 2)
This useful information, specifically the examples page can be found at https://www.tutorialspoint.com/hibernate/hibernate_examples.html
A series offered at tutorialpoint.com/hibernate provides Hibernate learning material. The series start with a Home and comprises readings on:
- ORM Overview
- Hibernate Overview
- Architecture
- Environment
- Configuration
- Sessions
- Persistent Class
- Mapping Files
- Mapping Types
- Hibernate Examples
- O/R Mappings
- Annotations
- Query Language
- Criteria Queries
- Native SQL
- Caching
- Batch Processing
- Interceptors
- ORM
Hibernate notes (part 3)
The useful tutorial is found at: http://www.javawebtutor.com/articles/jpa/jpa-example-using-maven.php
Steps for a Maven JPA application with its persistence dependencies are as below.
- Create the POJO entities classes
- Add the dependencies to the pom.xml file.
Artifact IDs are:- javax.persistence
- hibernate-entitymanager
- mysql-connector-java
- set up persistence.xml file in META-INF folder
- run a test class
Notes about POJO classes while developing with JPA
- the class should implement java.io.Serializable
- POJO entity classes that have fields with one-to-many relationships should be annotated by the annotations that show the relationship. Some of these annotations are @OneToMany and @ManyToOne, @JoinColumn, @mappedBy. A bidirectional relationship between two tables would have annotations @OneToMany and @ManyToOne.
- if the primary key “id” of the entity class does not have the annotation @GeneratedValue(strategy=GenerationType.AUTO) then one can run the EntityManager’s persist with the entity class that assigns id manually. However, if the primary key “id” has the annotation @GeneratedValue(strategy=GenerationType.AUTO) then entity manager’s persist can be used with a persist that does not have the id included.
Other resources through out the development (in order of occurrence):
- A very useful JPA example is at mkyong.com’s https://www.mkyong.com/hibernate/hibernate-one-to-many-relationship-example-annotation
- error: org.hibernate.PersistentObjectException: detached entity passed to persist.
A Stackoverflow solution starts with the question and issue that rises in situations: “I had successfully written my first master child example with hibernate. After few days I took it again and upgraded some libraries. No sure what I did I do but I could never make it run again. Would somebody help my figure out what is wrong in code that is returning following error message: …” - JPA Join Column explained: https://www.baeldung.com/jpa-join-column
- hibernate one-to-many SQL relationship explained: https://www.baeldung.com/hibernate-one-to-many
- one-to-many relationship example: https://www.mkyong.com/hibernate/hibernate-one-to-many-relationship-example-annotation
- @JoinColumn vs. @mappedBy annotations in hibernate: https://www.baeldung.com/jpa-joincolumn-vs-mappedby
- readings on Unidirectional and Bidirectional relationship annotation
- @SQLInsert usage to insert entity in Hibernate with custom SQL