Introduction
Overview
The
Java Persistence API (JPA) is the Java standard for mapping Java
objects to a relational database. Even though proprietary mapping
products like Hibernate and TopLink still exist, they are now focused on
providing their functionality through the JPA API, allowing all
applications to be portable across JPA implementations.
JPA
permits the developer to work directly with objects rather than with
SQL statements. The JPA implementation is typically called persistence
provider.
The
mapping between Java objects and database tables is defined via
persistence metadata. The JPA provider will use the persistence metadata
information to perform the correct database operations.
JPA
typically defines the metadata via annotations in the Java class.
Alternatively the metadata can be defined via XML or a combination of
both. A XML configuration overwrites the annotations.
JPA defines a SQL-like Query language for static and dynamic queries.
Most JPA persistence provider offer the option to create automatically the database schema based on the metadata.
Entity
A
class which should be persisted in a database it must be annotated with
javax.persistence.Entity. Such a class is called Entity. JPA will
create a table for the entity in your database. Instances of the class
will be a row in the table.
All
entity classes must define a primary key, must have a non-arg
constructor and or not allowed to be final. Keys can be a single field
or a combination of fields.
JPA allows to auto-generate the primary key in the database via the @GeneratedValue annotation.
By
default, the table name corresponds to the class name. You can change
this with the addition to the annotation @Table(name="NEWTABLENAME").
Persistence of fields
The
fields of the Entity will be saved in the database. JPA can use either
your instance variables (fields) or the corresponding getters and
setters to access the fields. You are not allowed to mix both methods.
If you want to use the setter and getter methods the Java class must
follow the Java Bean naming conventions. JPA persists per default all
fields of an Entity, if fields should not be saved they must be marked
with @Transient.
By
default each field is mapped to a column with the name of the field.
You can change the default name via @Column (name="newColumnName").
The following annotations can be used.
Table 1. Annotations for fields / getter and setter
@Id
|
Identifies the unique ID of the database entry
|
@GeneratedValue
|
Together with ID defines that this value is generated automatically.
|
@Transient
|
Field will not be saved in database
|
Relationship Mapping
JPA
allows to define relationships between classes, e.g. it can be defined
that a class is part of another class (containment). Classes can have
one to one, one to many, many to one, and many to many relationships
with other classes.
A
relationship can be bidirectional or unidirectional, e.g. in a
bidirectional relationship both classes store a reference to each other
while in an unidirectional case only one class has a reference to the
other class. Within a bidirectional relationship you need to specify the
owning side of this relationship in the other class with the attribute
"mappedBy", e.g. @ManyToMany(mappedBy="attributeOfTheOwningClass".
Table 2. Relationship annotations
@OneToOne
|
@OneToMany
|
@ManyToOne
|
@ManyToMany
|
Entity Manager
The
entity manager javax.persistence.EntityManager provides the operations
from and to the database, e.g. find objects, persists them, remove
objects from the database, etc. In a JavaEE application the entity
manager is automatically inserted in the web application. Outside JavaEE
you need to manage the entity manager yourself.
Entities
which are managed by an Entity Manager will automatically propagate
these changes to the database (if this happens within a commit
statement). If the Entity Manager is closed (via close()) then the
managed entities are in a detached state. If synchronize them again with
the database an Entity Manager provides the merge() method.
Persistence Units
The
EntityManager is created by the EntitiyManagerFactory which is
configured by the persistence unit. The persistence unit is described
via the file "persistence.xml" in the directory META-INF in the source
folder. A set of entities which are logically connected will be grouped
via a persistence unit. "persistence.xml" defines the connection data to
the database, e.g. the driver, the user and the password.
JPA Architecture
- Persistence: The javax.persistence.Persistence class contains static helper methods to obtain EntityManagerFactory instances in a vendor-neutral fashion.
- EntityManagerFactory: The javax.persistence.EntityManagerFactory class is a factory for EntityManager s.
- EntityManager : The javax.persistence.EntityManager is the primary JPA interface used by applications. Each EntityManager manages a set of persistent objects, and has APIs to insert new objects and delete existing ones. When used outside the container, there is a one-to-one relationship between an EntityManager and an EntityTransaction. EntityManagers also act as factories for Query instances.
- Entity: Entites are persistent objects that represent datastore records.
- EntityTransaction: Each EntityManager has a one-to-one relation with a single javax.persistence.EntityTransaction. EntityTransactions allow operations on persistent data to be grouped into units of work that either completely succeed or completely fail, leaving the datastore in its original state. These all-or-nothing operations are important for maintaining data integrity.
- Query : The javax.persistence.Query interface is implemented by each JPA vendor to find persistent objects that meet certain criteria. JPA standardizes support for queries using both the Java Persistence Query Language (JPQL) and the Structured Query Language (SQL). You obtain Query instances from an EntityManager.
JPA Features
- Simple POJO Persistence
- No vendor-specific interfaces or classes
- Supports rich domain models
- No more anemic domain models
- Multiple inheritance strategies
- Polymorphic Queries
- Lazy loading of associations
- Rich Annotation Support
- Pluggable persistence providers
JPA vs. Hibernate
JPA
is a framework for managing relational data in Java applications, while
Hibernate is a specific implementation of JPA (so ideally, JPA and
Hibernate cannot be directly compared). In other words, Hibernate is one
of the most popular frameworks that implements JPA. Hibernate
implements JPA through Hibernate Annotation and EntityManager libraries
that are implemented on top of Hibernate Core libraries. Both
EntityManager and Annotations follow the lifecycle of Hibernate. The
newest JPA version (JPA 2.0) is fully supported by Hibernate 3.5. JPA
has the benefit of having an interface that is standardized, so the
developer community will be more familiar with it than Hibernate. On the
other hand, native Hibernate APIs can be considered more powerful
because its features are a superset of that of JPA.
Nice info regarding java persistence api My sincere thanks for sharing this post Please Continue to share this kind of post
ReplyDeleteJava Training in Chennai
Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a Java developer learn from Java Training in Chennai. or learn thru Java EE Online Training India . Nowadays Java has tons of job opportunities on various vertical industry.
Deletenice blog has been shared by you. before i read this blog i didn't have any knowledge. but now i got some knowledge so keep on sharing such kind of an interesting blogs.
ReplyDeletejava training in chennai
Really it is a fantastic blog.You are a skilled blogger.I read your blog completely it is very nice.Thanks for sharing.keep sharing more blogs.
ReplyDeleteCore Java Online Training
Great articles, first of all Thanks for writing such lovely Post! Earlier I thought that posts are the only most important thing on any blog. Software Testing Training in Bangalore
ReplyDeleteJava Training in Chennai
I love this post.We share give the now amazing post.I understand all fantastic info.We will have shared learning it amazing post.Python Online Training | Learn Python Online
ReplyDeletevery nice blog.YOU should clearly explain the different between blog and website.Thanks for sharing.keep sharing more blogs.If anybody want to build your website.
ReplyDeleteWhite Label Website Builder
Wonderful post.I learn so many things from your blog.
ReplyDeleteNo.1 Selenium Training Institute in Chennai | Selenium Training in Chennai | Software Testing Training in Chennai
Thanks to share your useful message about JPA features
ReplyDeleteYour blog was impressive; After reading your post I am inspired with your post writing style & how continuously you describe this topic. thanks for taking the time to discuss this.. No.1 CCNA Training in Chennai | No.1 CCNP Training in Chennai | Six Sigma Training in Chennai
ReplyDelete"Great blog created by you. I read your blog, its best and useful information. You have done a great work. Super blogging and keep it up.php jobs in hyderabad.
ReplyDelete"
Excellent blog..thanks to share your valuable tricks..
ReplyDeletebest IOS training in bangalore.
Interesting and informative article.. very useful to me.. thanks for sharing your wonderful ideas.. please keep on updating..
ReplyDeleteMat Lab Project Center in Chennai | IEEE Mat Lab Projects in Velachery
You have provided really awesome blog for learners. Then check it once through Devops Online Training Hyderabad for more information.
ReplyDelete• Nice and good article. It is very useful for me to learn and understand easily. Thanks for sharing your valuable information and time. Please keep updating. Power Bi Online Training course
ReplyDeleteAppreciation for really being thoughtful and also for deciding on
ReplyDeletecertain marvelous guides most people really want to be aware of.
java training in chennai
java training in bangalore
Thanks for sharing this valuable information to our vision. You have posted a trust worthy blog keep sharing.
ReplyDeleteCloud Computing Project Center in Chennai | IEEE Cloud Computing Projects in Velachery
Artificial intelligence Training in noida
ReplyDeleteArtificial intelligence Training in noida-Artificial Intelligence Training in Noida, Artificial Intelligence Training classes in Noida, Artificial Intelligence Training classes in Noida, Artificial Intelligence Training
by Real time ARTIFICIAL INTELLIGENCE Experts, Big-Data and ARTIFICIAL INTELLIGENCE Certification Training in Noida
WEBTRACKKER TECHNOLOGY (P) LTD.
C - 67, sector- 63, Noida, India.
F -1 Sector 3 (Near Sector 16 metro station) Noida, India.
+91 - 8802820025
0120-433-0760
0120-4204716
EMAIL: info@webtrackker.com
Website: www.webtrackker.com
Our Other Courses:
artificial intelligence Training in noida
SAS Training Institute in Delhi
SAS Training in Delhi
SAS Training center in Delhi
Sap Training Institute in delhi
Sap Training in delhi
Best Sap Training center in delhi
Best Software Testing Training Institute in delhi
Software Testing Training in delhi
Software Testing Training center in delhi
Best Salesforce Training Institute in delhi
Salesforce Training in delhi
Salesforce Training center in delhi
Best Python Training Institute in delhi
Python Training in delhi
Best Android Training Institute In delhi
Best Python Training center in delhi
Android Training In delhi
best Android Training center In delhi
ReplyDeleteBest Solidworks training institute in noida
SolidWorks is a solid modeling computer-aided design (CAD) and computer-aided engineering (CAE) computer program that runs on Microsoft Windows. SolidWorks is published by Dassault Systems. Solid Works: well, it is purely a product to design machines. But, of course, there are other applications, like aerospace, automobile, consumer products, etc. Much user friendly than the former one, in terms of modeling, editing designs, creating mechanisms, etc.
Solid Works is a Middle level, Main stream software with focus on Product development & this software is aimed at Small scale & Middle level Companies whose interest is to have a reasonably priced CAD system which can support their product development needs and at the same time helps them get their product market faster.
Company Address:
WEBTRACKKER TECHNOLOGY (P) LTD.
C-67,Sector-63,Noida,India.
E-mail: info@webtracker.com
Phone No: 0120-4330760 ,+91-880-282-0025
webtrackker.com/solidworks-training-Course-institute-in-noida-delhi
Data science Training Institute in Noida
ReplyDeleteWebtrackker Data science Training Institute in Noida Accelerate your career in data science by starting from basics in Statistics, Data Management and Analytics to advanced topics like Neural Networks, Machine Learning and Big Data.
http://webtrackker.com/Best-Data-Science-Training-Institute-in-Noida.php
Data science Training Institute in Noida
OUR OTHER COURCES
SAS Training center in Delhi
Best Software Testing Training Institute in delhi
Best Salesforce Training Institute in delhi
Best Python Training Institute in delhi
3D Animation Training in Noida
ReplyDeleteBest institute for 3d Animation and Multimedia
Best institute for 3d Animation Course training Classes in Noida- webtrackker Is providing the 3d Animation and Multimedia training in noida with 100% placement supports. for more call - 8802820025.
3D Animation Training in Noida
Company Address:
Webtrackker Technology
C- 67, Sector- 63, Noida
Phone: 01204330760, 8802820025
Email: info@webtrackker.com
Website: http://webtrackker.com/Best-institute-3dAnimation-Multimedia-Course-training-Classes-in-Noida.php
Graphics designing training institute in Noida
ReplyDeleteBest Graphics training institute in Noida, Graphic Designing Course, classes in Noida- webtrackker is providing the graphics training in Noida with 100% placement supports. If you are looking for the Best Graphics designing training institute in Noida For more call - 8802820025.
Graphics designing training institute in Noida, Graphics designing training in Noida, Graphics designing course in Noida, Graphics designing training center in Noida
Company address:
Webtrackker Technology
C- 67, Sector- 63, Noida
Phone: 01204330760, 8802820025
Email: info@webtrackker.com
Website: http://webtrackker.com/Best-institute-for-Graphic-Designing-training-course-in-noida.php
Thanks for sharing The Information The Information shared is very valuable Please keep Updating us Time Just went on redaing the article Python Online Training AWS Online Training Devops Online Training Data Science Online Training
ReplyDeleteThanks for posting this nice blog
ReplyDeleteUnic Sol is the best Best java training in hyderabad with job placements. Along with java training full stack, mean stack, angular & testing tools training is provided by industry experts. We are the best java training in Hyderabad.
very useful post and informative article i really impressed keep it up for most post selenium training in chennai
ReplyDeletejava training in chennai
java training in tambaram
aws training in chennai
aws training in tambaram
python training in chennai
python training in tambaram
selenium training in chennai
selenium training in tambaram
I wanted to thank you for this great read!! I definitely enjoying every little bit of it I have you bookmarked to check out new stuff you post.is article.
ReplyDeletedata science training in chennai
data science training in annanagar
android training in chennai
android training in annanagar
devops training in chennai
devops training in annanagar
artificial intelligence training in chennai
artificial intelligence training in annanagar
This comment has been removed by the author.
ReplyDeleteNice info regarding java persistence api My sincere thanks for sharing this post Please Continue to share this kind of post..
ReplyDeleteangular js training in chennai
angular js training in omr
full stack training in chennai
full stack training in omr
php training in chennai
php training in omr
photoshop training in chennai
photoshop training in omr
I must appreciate you for providing such a valuable content for us. This is one amazing piece of article. Helped a lot in increasing my knowledge
ReplyDeleteangular js training in chennai
angular js training in porur
full stack training in chennai
full stack training in porur
php training in chennai
php training in porur
photoshop training in chennai
photoshop training in porur