Does JPA only refer to RDBMS - java

JPA is all about data persistence; is data persistence only limited to RDBMS ? if not what are all the different persistence mechanisms (like Excel,File System,XML, NON RDMS etc ..) we can achieve with JPA specifications ?

JPA is designed for RDBMS, and looking at the API and metadata you can see that this is the case. It is possible to apply JPA to other datastores, but approximations have to be made for some concepts, particularly when querying. On the other hand the JDO API was designed for all datastores, and such approximations don't have to be made.
DataNucleus was the first implementation to provide JPA across a range of other datastores (Excel, ODBMS, ODF, LDAP, BigTable, HBase, MongoDB, etc), and has provided these for the last 2+ years. It also provides JDO access for the same datastores.

JPA is all about data persistence; is data persistence only limited to RDBMS ?
Yes.
JPA is a framework that provides an object / relational mapping. Relational is the "R" in RDBMS.
A project (Hibernate OGM) started recently to allow Hibernate to interface with NoSQL-type databases. The goal of the project is "to provide a common interface for NoSQL datastores using JPA constructs". (That is not the same as implementing JPA for NoSQL.) The article linked below describes the project as "nascent"; i.e. only recently started, don't hold your breath waiting.
References:
Java Persistence API - Wikipedia.
Hibernate Object Mapping for NoSQL Data Stores

JPA only defines standard mappings for relational data. But many JPA providers support non-relational data as well. Normally it is the runtime side of the API that is supported, mapping is normally done through non-standard meta-data.
Also there are many JDBC providers that support the JDBC API and SQL to non-relational data and data sources, which will work with any JPA provider. This is typically the best solution for accessing non-relational data.
There is no standard to mapping to non-relational data, as non-relational data encompasses a broad range of data formats, and are by definition non-standard. The Java Connector Architecture (JCA) standard is Java's standard for accessing non-relational data. However most non-relational adapters provide JDBC drivers instead of JCA drivers as JDBC usage is more widespread.
See,
http://en.wikibooks.org/wiki/Java_Persistence/Databases#EIS.2C_and_Non-relational_Data_Sources
EclipseLink has support for several persistence services including:
JPA
EIS (Enterprise Information Systems) non-relational data sources through JCA connectors
JAXB (mapping XML data)
DBWS (database web-services)
SDO (Service Data Objects)

The Java Persistence API deals with the way relational data is mapped
to Java objects ("persistent entities"), the way that these objects
are stored in a relational database so that they can be accessed at a
later time, and the continued existence of an entity's state even
after the application that uses it ends. In addition to simplifying
the entity persistence model, the Java Persistence API standardizes
object-relational mapping.
The qoutes are taken fro here:
http://www.oracle.com/technetwork/articles/javaee/jpa-137156.html
Shortly, yes. The JPA is about mapping of java objects to relational DB.
Is there a way to "abuse" the API and create implementation that maps objects to other targets like NOSQL? I believe it is possible but not very simple. How for example will you implement support of relational annotations like #OneToMany?

Related

Different ways to access database in Java

There are a lot of technologies to access data using Java.
I was reading about some of them, including:
JPQL
HQL
Criteria API
Querydsl
jOOQ
JaQu
JDBC
ActiveJDBC
Now I am really confused because of the overhead. What are the main differences and similarities between these technologies? Which are most commonly used? Short comparison is more than welcome.
The fundamental foundation for database access in Java is JDBC. It is an interface (no implementation) that permits database vendors to expose their databases in a standard way, so Java programmers don't have to radically alter their code to support different relational databases.
That said, it accepts SQL, and SQL is standard, with so many variants that the standards do not permit cross database portability.
The rest of the platforms tend to build upon JDBC in an attempt to ease the cost of integrating with other databases. The numbers of ways one can interact with a database is varied, basically you get improvement on issuing SQL commands or the additional library takes over the writing of a compatible SQL command for you.
The two major categories are "database like" and "object storage" where object storage focuses on you storing a Java Object, and the libraries take care of most of the details of SQL generation.
Technology / query language / type / notes
Java Persistance API / JPQL / object store / not an implementation, but an interface allowing multiple implementations, query language is not table specific, but object specific
Hibernate / HQL / object store / a popular Java solution, but tied to Hibernate only.
Java Persistance API / Criteria API / object store / Criteria API is the code (programming) equivalent to the JPQL strings.
Java Persistance API & JDO / QueryDSL API / object store / Java API to build queries similar to Criteria API, but different
JDBC / jOOQ / direct JDBC / Java API that replaces SQL strings construction with method calls that are table centric
H2 database / JaQu / object store / Linked directly to one database, non-portable. Follows Microsoft LINQ syntax.
JDBC / ActiveJDBC / object store / Looks like a branded version of almost-JPA for webapps.
The two fundamental APIs in this space are JDBC and JDO, where the subset of JDO you wish to use if you are only going to support relational databases is JPA. Neither JDBC nor JDO provide a database connection directly, they are pure-play APIs. That said, a lot of database vendors push their APIs which don't leverage these technologies, I would advise not using any technology that isn't based on JDBC and JPA.
Again to leverage multiple implementations, I would also advise that you not use a query language that isn't based on JPQL (if you build queries in Strings) or the CriteriaAPI (if you build queries in code), both which are conceptual components of JPA. If you are using JDBC directly, use PreparedStatements for all issued SQL, and keep in mind that while you may be able to leverage your existing SQL codebase with a pure JDBC solution, you will likely get a better quality implementation (and possibly faster delivery) using JPA, because there are lots of corner cases in mapping Relational Databases to Java that very few existing database codebases handle.
Do not write JDBC code.
You will never do a better job of it than MyBatis and/or Hibernate.
Instead, learn and use either MyBatis or Hibernate.
MyBatis is simpler (and most likely sufficient for anything you will need) and Hibernate has a million features (none of which you probably need).
Using JDBC API and registering your DB class and making the connection.
Using Datasource, where you define DB details on your server like WebSphere or Weblogic
server And you lookup the defined datasource.
Using some ORM framework like Hibernate.

Custom JPA implementation // existing No SQL JPA implementations

Background:
I've been using JPA lately, and I am very impressed by how easily I was able to produce a persistence layer for a reasonably large relational database project.
We use a lot of no-sql databases at my company, specifically column oriented ones. I have some questions about potentially using JPA for those databases:
Questions
Can JPA be used with NO-SQL databases? It stands to reason that if the framework can generate a query for a SQL database and map the results, then it probably could reasonably easily be tailored to generate a different kind of query and a different mapping, for say, querying Hadoop maybe?
If it's possible, are there any existing implementaitons of JPA that use things besides SQL?
Are there any good resources on implementing/extending JPA? I realize TSQL, PLSQL, etc. must all be specifically addressed in JPA, so there must be an extensibility mechanism we can manipulate.
There are various JPA implementations that support (the badly termed) "NoSQL" set of datastores. The most complete we've found to be DataNucleus which also provides the more suitable JDO API also. It supports MongoDB, Cassandra, HBase, AppEngine, LDAP, spreadsheets, Neo4j, and some others
As per your question i came across Hibernate OGM which stands for Hibernate Object Grid Mapper which provides JPA (java Persistence api)the support for the NoSQL solutions.
Hibernate OGM has the following capabilities : -
persists entities into a NoSQL
datastore specific native queries
full-text queries, using Hibernate Search as indexing engine
I haven't explore more on this framework OGM but looks very promising solution for your questions.
You can refer to the following URL to get more idea about the Hibernate OGM
http://hibernate.org/ogm/

What's the difference between JPA and Hibernate? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Closed 4 years ago.
Locked. This question and its answers are locked because the question is off-topic but has historical significance. It is not currently accepting new answers or interactions.
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 I'm trying to learn either Hibernate or JPA2. I picked up Pro JPA2 book and it keeps referring to "JPA provider". For example:
If you think a feature should be standardized, you should speak up
and request it from your JPA provider
This confuses me so I have a few questions:
Using JPA2 alone can I fetch data from DB by simply annotating my POJO's
Is JPA2 supposed to be used with a "JPA Provider" e.g TopLink or Hibernate? If so, then what's the benefit of using JPA2 + Hibernate as compared to JPA2 alone, or compared to Hibernate alone ?
Can you recommend a good practical JPA2 book. "Pro JPA2" seems more like a bible and reference on JPA2 (It doesn't get into Queries until the later half of the book). Is there a book that takes a problem/solution approach to JPA2?
As you state JPA is just a specification, meaning there is no implementation. You can annotate your classes as much as you would like with JPA annotations, however without an implementation nothing will happen. Think of JPA as the guidelines that must be followed or an interface, while Hibernate's JPA implementation is code that meets the API as defined by the JPA specification and provides the under the hood functionality.
When you use Hibernate with JPA you are actually using the Hibernate JPA implementation. The benefit of this is that you can swap out Hibernate's implementation of JPA for another implementation of the JPA specification. When you use straight Hibernate you are locking into the implementation because other ORMs may use different methods/configurations and annotations, therefore you cannot just switch over to another ORM.
For a more detailed description read my blog entry.
JPA is the dance, Hibernate is the dancer.
Some things are too hard to understand without a historical perspective of the language and understanding of the JCP.
Often there are third parties that develop packages that perform a function or fill a gap that are not part of the official JDK. For various reasons that function may become part of the Java JDK through the JCP (Java Community Process)
Hibernate (in 2003) provided a way to abstract SQL and allow developers to think more in terms of persisting objects (ORM). You notify hibernate about your Entity objects and it automatically generates the strategy to persist them. Hibernate provided an implementation to do this and the API to drive the implementation either through XML config or annotations.
The fundamental issue now is that your code becomes tightly coupled with a specific vendor(Hibernate) for what a lot of people thought should be more generic. Hence the need for a generic persistence API.
Meanwhile, the JCP with a lot of input from Hibernate and other ORM tool vendors was developing JSR 220 (Java Specification Request) which resulted in JPA 1.0 (2006) and eventually JSR 317 which is JPA 2.0 (2009). These are specifications of a generic Java Persistence API. The API is provided in the JDK as a set of interfaces so that your classes can depend on the javax.persistence and not worry about the particular vendor that is doing the work of persisting your objects. This is only the API and not the implementation. Hibernate now becomes one of the many vendors that implement the JPA 2.0 specification. You can code toward JPA and pick whatever compliant ORM vendor suits your needs.
There are cases where Hibernate may give you features that are not codified in JPA. In this case, you can choose to insert a Hibernate specific annotation directly in your class since JPA does not provide the interface to do that thing.
Source: http://www.reddit.com/r/java/comments/16ovek/understanding_when_to_use_jpa_vs_hibernate/
JPA is the interface while Hibernate is the implementation.
Traditionally there have been multiple Java ORM solutions:
Hibernate
TopLink
JDO
each implementation defining its own mapping definition or client API. The JPA expert group gathered the best of all these tools and so they created the Java Persistence API standard.
A standard persistence API is very convenient from a client point of view, making it relatively easy to switch one implementation with the other (although in practice it's not that simple because on large projects you'll have to use specific non-standard features anyway).
The standard JPA has pushed Java ORM competition to a new level and this can only lead to better implementations.
As explained in my book, High-Performance Java Persistence, Hibernate offers features that are not yet supported by JPA:
extended identifier generators (hi/lo, pooled, pooled-lo)
transparent prepared statement batching
customizable CRUD (#SQLInsert, #SQLUpdate, #SQLDelete) statements
static or dynamic collection filters (e.g. #FilterDef, #Filter, #Where) and entity filters (e.g. #Where)
mapping properties to SQL fragments (e.g. #Formula)
immutable entities (e.g. #Immutable)
more flush modes (e.g. FlushMode.MANUAL, FlushMode.ALWAYS)
querying the second-level cache by the natural key of a given entity
entity-level cache concurrency strategies
(e.g. Cache(usage = CacheConcurrencyStrategy.READ_WRITE))
versioned bulk updates through HQL
exclude fields from optimistic locking check (e.g. #OptimisticLock(excluded = true))
versionless optimistic locking (e.g. OptimisticLockType.ALL, OptimisticLockType.DIRTY)
support for skipping (without waiting) pessimistic lock requests
support for Java 8 Date and Time
support for multitenancy
support for soft delete (e.g. #Where, #Filter)
These extra features allow Hibernate to address many persistence requirements demanded by large enterprise applications.
From the Wiki.
Motivation for creating the Java Persistence API
Many enterprise Java developers use lightweight persistent objects provided by open-source frameworks or Data Access Objects instead of entity beans: entity beans and enterprise beans had a reputation of being too heavyweight and complicated, and one could only use them in Java EE application servers. Many of the features of the third-party persistence frameworks were incorporated into the Java Persistence API, and as of 2006 projects like Hibernate (version 3.2) and Open-Source Version TopLink Essentials have become implementations of the Java Persistence API.
As told in the JCP page the Eclipse link is the Reference Implementation for JPA. Have look at this answer for bit more on this.
JPA itself has features that will make up for a standard ORM framework. Since JPA is a part of Java EE spec, you can use JPA alone in a project and it should work with any Java EE compatible Servers. Yes, these servers will have the implementations for the JPA spec.
Hibernate is the most popular ORM framework, once the JPA got introduced hibernate conforms to the JPA specifications. Apart from the basic set of specification that it should follow hibernate provides whole lot of additional stuff.
JPA is just a specification which needs concrete implementation.
The default implementation oracle provide is "Eclipselink" now. (Toplink is donated by Oracle to Eclipse foundation to merge with eclipselink)
(Reference : http://www.oracle.com/technetwork/middleware/toplink/index-085257.html
http://www.eclipse.org/org/press-release/20080317_Eclipselink.php
)
Using Eclipselink, one can be sure that the code is portable to any implementation if need arises.
Hibernate is also a full JPA implementation + MORE ( Sort of JPA Plus). Hibernate is super set of JPA with some extra Hibernate specific functionality.
So app developed in Hibernate may not be compatible when switched to other implementation.
Still hibernate is choice of majority of developers as JPA implementation and widely used.
Another JPA implementation is OpenJPA (openjpa.apache.org) which is an extension of Kodo implementation.
JPA : is just like an interface and have no concrete implementation of it to use functions which are there in JPA.
Hibernate : is just a JPA Provider which have the implementation of the functions in JPA and can have some extra functions which might not be there in JPA.
TIP : you can use
*combo 1* : JPA + JPA Provider(Hibernate)
*combo 2* : only Hiberante which does not need any interface
Combo 1 : is used when you feel that your hibernate is not giving better performance and want to change JPA Provider that time you don't have to write your JPA once again. You can write another JPA Provider ... and can change as many times you can.
Combo 2 : is used very less as when you are not going change your JPA Provider at any cost.
Visit http://blog-tothought.rhcloud.com//post/2, where your complete confusion will get clear.
JPA is the interface, Hibernate is one implementation of that interface.
JPA is a specification to standardize ORM-APIs. Hibernate is a vendor of a JPA implementation. So if you use JPA with hibernate, you can use the standard JPA API, hibernate will be under the hood, offering some more non standard functions.
See http://docs.jboss.org/hibernate/stable/entitymanager/reference/en/html_single/ and http://docs.jboss.org/hibernate/stable/annotations/reference/en/html_single/
JPA is just a specification.In market there are many vendors which implements JPA. Different types of vendors implement JPA in different way. so different types of vendors provide different functionality so choose proper vendor based on your requirements.
If you are using Hibernate or any other vendors instead of JPA than you can not easily move to hibernate to EclipseLink or OpenJPA to Hibernate.But If you using JPA than you just have to change provide in persistence XML file.So migration is easily possible in JPA.
JPA is an API, one which Hibernate implements.Hibernate predates JPA. Before JPA, you write native hibernate code to do your ORM. JPA is just the interface, so now you write JPA code and you need to find an implementation. Hibernate happens to be an implementation.
So your choices are this:
hibernate, toplink, etc...
The advantage to JPA is that it allows you to swap out your implementation if need be. The disadvantage is that the native hibernate/toplink/etc... API may offer functionality that the JPA specification doesn't support.
While JPA is the specification, Hibernate is the implementation provider that follows the rules dictated in the specification.
Java - its independence is not only from the operating system, but also from the vendor.
Therefore, you should be able to deploy your application on different application servers.
JPA is implemented in any Java EE- compliant application server and it allows to swap application servers, but then the implementation is also changing. A Hibernate application may be easier to deploy on a different application server.
JPA is a specification that you implement in your data layer to perform db opertations, OR mappings and other required tasks.
Since it is just a specification, you need a tool to have it implemented. That tool can be either Hibernate, TopLink, iBatis, spring-data etc.
You don't necessarily require JPA if you are using Hibernate in your Data Layer. But if you use JPA specification for Hibernate, then it will make switching to other ORM tools like iBatis, TopLink easy in future, because the specification is common for others as well.
*(if you remember, you do import javax.persistence.*; when you use annotations for OR mapping (like #Id, #Column, #GeneratedValue etc.) in Hibernate, that's where you are using JPA under Hibernate, you can use JPA's #Query & other features as well)
JPA is a Java API specification which describes the management of relational data in applications using Java Platform. where as Hibernate is a ORM (Object Relational Mapping) library which follows JPA specification.
You can think JPA as a set of Rules which is implemented by Hibernate.
JPA is JSR i.e. Java Specification Requirement to implement Object Relational Mapping which has got no specific code for its implementation. It defines certain set of rules for for accessing, persisting and managing the data between Java objects and the relational databaseWith its introduction, EJB was replaced as It was criticized for being heavyweight by the Java developer community.
Hibernate is one of the way JPA can be implemented using te guidelines.Hibernate is a high-performance Object/Relational persistence and query service which is licensed under the open source GNU Lesser General Public License (LGPL) .The benefit of this is that you can swap out Hibernate's implementation of JPA for another implementation of the JPA specification. When you use straight Hibernate you are locking into the implementation because other ORMs may use different methods/configurations and annotations, therefore you cannot just switch over to another ORM.
JPA is just a specification which needs concrete implementation. The default implementation provided by oracle is "Eclipselink" now. Toplink is donated by Oracle to Eclipse foundation to merge with eclipselink.
Using Eclipselink, one can be sure that the code is portable to any implementation if need arises. Hibernate is also a full JPA implementation + MORE. Hibernate is super set of JPA with some extra Hibernate specific functionality. So application developed in Hibernate may not be compatible when switched to other implementation. Still hibernate is choice of majority of developers as JPA implementation and widely used.
Another JPA implementation is OpenJPA, which is an extension of Kodo implementation.
JPA vs Hibernate
I try to explain in very easy words.
Suppose you need a car as we all know their are several A class manufacturer like MERCEDES, BMW , AUDI etc.
Now in above statement CAR(is a specification) as every car have common features like thing with 4 wheels and can be driven on road is car...so its like JPA.
And MERCEDES, BMW , AUDI etc are just using common car feature and adding functionality according to their customer base so they are implementing the car specification like hibernate , iBATIS etc.
So by this common features goes to jpa and hibernate is just an implementation according to their jboss need.
1 more thing
JPA includes some basic properties so in future if you want to change hibernate to any other implementation you can easily switch without much headache and for those basic properties includes JPA annotations which can work for any implementation technology, JPQL queries.
So mainly we implement hibernate with JPA type technology just for in case we want to switch our implementation according to client need plus you will write less code as some common features are involved in JPA.
If someone still not clear then you can comment as i m new on stack overflow.
Thank you
JPA is just a specification while Hibernate is one of the JPA provider i.e hibernate is implementing various things mentioned in JPA contract.
JPA or Java Persistence API is a standard specification for ORM implementations whereas Hibernate is the actual ORM implementation or framework.
JPA is Java Persistence API. Which Specifies only the specifications for APIs. Means that the set of rules and guidelines for creating the APIs. If says another context, It is set of standards which provides the wrapper for creating those APIs , can be use for accessing entity object from database. JPA is provided by oracle.When we are going to do database access , we definitely needs its implementation. Means JPA specifies only guidelines for implementing APIs. Hibernate is a JPA provider/Vendor who responsible for implementing that APIs. Like Hibernate TopLink and Open JPA is some of examples of JPA API providers. So we uses JPA specified standard APIs through hibernate.
Figuratively speaking JPA is just interface, Hibernate/TopLink - class (i.e. interface implementation).
You must have interface implementation to use interface. But you can use class through interface, i.e. Use Hibernate through JPA API or you can use implementation directly, i.e. use Hibernate directly, not through pure JPA API.
Good book about JPA is "High-Performance Java Persistence" of Vlad Mihalcea.

Is there a good generic pattern for data store queries that hide vendor specific logic?

I'm trying to find a good way to implement a generic search API in Java that will allow my users to search the backend repository without needing to know what that backend technology is, and so that if in the future we switch vendors I can reimplement the underlying logic without needing to recode the API. The repository underneath could be a relational database or a document store like SOLR, CouchDB, MongoDB, etc... It would need to support all the typical search requirements such as wildcards, ranges, bitwise operators, and so on.
Are there any standard ways of approaching this problem?
Would JPA be my best bet? Would it do everything I need it for, including non-relational databases?
Thanks in advance!
What you need is a ORM framework like Hibernate, if you go for JPA, you need to re-invent a lot of wheel.
using Hibernate you can write the business logic for searching the backend database or repository without vendor specific implementation, and if later you need to change the backend, you can do it without affecting your existing business code implementation.
I would advice you to check the hibernate documentation for further reference
The Spring Data umbrella of projects provides a nice DAO abstraction named CrudRepository. I believe most of the sub-projects (JPA, MongoDB, etc.) provide some implementations of it.
JPA would be one of a number of implementations you would use to map your relational database to objects. It would not protect you from database changes.
I think you're looking for the DAO Pattern. What I'm doing is as follows:
Create an interface for each DAO
Create a higher level DAO implementation that simply calls my actual database specific implementation
Wire the higher level DAO implementation to the database specific implementation with Spring.
This way, no code anywhere touches database specific implementation. The connections are formed only in XML.
JPA is designed around RDBMS ... only. Using it for other types of datastores makes little sense since things likes its query language leak SQL syntax. JDO is designed for datastore agnoticity, and provides persistence to many datastores using its implementations such as DataNucleus, though not all of those that you mention.
JPA is designed around RDBMS, Hibernate is also designed for RDBMS. There are few implementations of JPA which support no-sql. Similar projects are built around hibernate to support no-sql databases. However the API itself is tuned for RDBMS.
Implemeting a DAO patterns would require you to write your own query api. Later extend the implementation when ever your data store changes.
JDO and DataNucleus is ground up designed for heterogeneous data stores. Already has support for a dozen stores ,plus RDBMS. Beauty is that the query api remains constant across the stores. JDO allows you to work with domain model and leave the storage details to implementations like DataNucleus.
Hence I suggest JDO api with datanucleus.
The below link gives list data stores and f features already available in DataNucleus
http://www.datanucleus.org/products/accessplatform_3_0/datastore_features.html

Can I use ZODB with Hibernate/JPA?

I'm writing an application that uses JPA for persistence. Currently I'm testing with Hibernate and a MySQL database. The server it's going to be deployed on already has ZODB running though. To avoid having to install MySQL especially for this app, it would be nice to use ZODB as the backend for JPA. Is this possible, with or without Hibernate?
Do you mean ZODB as in Zope Object Database? If so the answer is no. JPA is all about ORM (Object Relational Mapping), meaning your are working with relational databases. ZODB is a object database which is completely different thing. Why would you use JPA if you are going to use a object database?
No, the ZODB is a python-specific object database. It uses Python-specific tricks to load and store object state, something the JPA cannot provide (it is designed to map objects to relational databases).
No current implementation for persisting to ZOPE DB but you could add support for persisting it using JPA via DataNucleus by adding support for that DB. Sounds complicated but the basics is using doable in a couple of days ... as per
http://www.datanucleus.org/servlet/wiki/display/ENG/HOWTO+Support+a+new+datastore
DataNucleus already supports other object datastores via JPA (db4o, NeoDatis) hence why it ought to be doable

Categories