I will try to respond objectively, but also by analysing technology choices in general terms.
About API Choices
Firstly, the idea of supplier independence that several Apis sell is tempting but in most cases it hardly occurs. Take the case of application servers, for example. Can anyone do the distribution of the same WAR in a Tomcat, Jboss, Glassfish or Weblogic? Each contains specific features that vary immensely from one to the other. On the other hand, if you completely ignore the features of your application server, you will lose the advantages of using it.
In practice, you should choose from the options available that seem to best meet the requirements of your project without spending too much time on it, as it is probably not worth going into too much detail when it comes to mature and commonly used products on the market. Of course there will be wrong choices and this is a real risk, so here counts much the experience and experience of the team and the leader.
Hibernate or JPA?
Speaking now specifically of JPA and Hibernate, I can say that JPA has really made great strides, but sometimes you may need a unique Hibernate feature, like Dynamic Queries or User Types (Update: Hibernate 4.3 implements the JPA 2.1 specification and has the annotation @Converter
, so it is no longer necessary to resort to a User Type to map an enum or non-standard type. Anyway, the important thing is the idea represented in the text.).
Which to choose then? The answer is: neither one nor the other. You can use both Apis at the same time. As a suggestion, JPA might have preference, but the Hibernate API would be used when needed. Particularly I had no trouble doing that.
Example #1 of Hibernate + JPA: Dynamic queries
Using a dynamic query allows not including null fields in commands like INSERT
, for example, so that the value default database will be used. In Hibernate this can be done through XML mapping or annotations @org.hibernate.annotations.Entity
and @DynamicInsert
(in the latest versions).
But, suppose you already have a persistence.xml and a JPA entity as below:
@javax.persistence.Entity
public class MinhaEntidade { ... }
Now what? Just add the Hibernate note:
@org.hibernate.annotations.Entity
@org.hibernate.annotations.Entity(dynamicInsert = true)
public class MinhaEntidade { ... }
Example #2 of Hibernate + JPA: Session Access
What if you have some HQL feature that would greatly enhance one of the features? No problem, just unpack the Session
hibernate:
Session session = entityManager.unwrap(Session.class);
Impact of this approach
Obviously, following these examples, the implementation would be "tied" to Hibernate.
However, the impact of a change, which I believe there will always be, will be minimized to some specific functionalities.
Finally, what’s the chance real that you need to trade Hibernate for another JPA implementation in the near future? I don’t mean to say that Hibernate is absolutely superior, but the odds are slim that someone really needs a resource that only exists, let’s say, in Eclipselink, and there is none workaround available.
The problem with this type of question is not because it is opinionated, but because the answers will be more based on opinions. The question is a kind of poll, which is not very appropriate for the OS format.
– elias
Having said that, there is an aphorism that says, "when you are finding it very difficult to choose between two things, it is probably not a very important decision". I think it applies in this case: the decision between JPA2 and Hibernate will probably not impact much on the success/failure of the big project. =)
– elias
I know almost zero on the subject itself, but it seems that the question is seeking information objectively and the first answer seems to be well elaborated. The line is thin but can work. Anyway, when doubts arise, it is always good to reflect if it can be improved.
– Maniero