Migrate from native to EclipseLink persistence - java

My application is using Spring 2.5, and native TopLink 10g. I want to migrate my application from native TopLink to EclipseLink.
Currently each DAO in my application extends TopLinkDaoSupport.java (in Spring 2.5), in order to use the #getSession() method for all DB operations, but the return type of the method is oracle.toplink.sessions.Session instead of org.eclipse.persistence.sessions.
Is there any solution of said problem?

I would also recommend to use JPA instead of the old TopLink API.
If you really need access to Session object, you can get it from EntityManager too. Check org.eclipse.persistence.internal.jpa.EntityManagerImpl in EclipseLink - this is a class that implements EntityManager. There is a getDelegate() method implemented that returns this (EntityManagerImpl) so ((org.eclipse.persistence.jpa.JpaEntityManager)[EntityManager].getDelegate()).getActiveSession() gives you org.eclipse.persistence.sessions.Session.

Related

Using Hibernate Sessions features along with Spring Data JPA

I know that Spring Data JPA uses Hibernate, however, I have some questions that need clarification.
Will I be able to use other Hibernate features such as C3P0, Hibernate second level caching, or Hibernate sessions if I use Spring Data JPA?
What are the advantages of using #Query for writing custom queries over using hibernate sessions and HQL?
Is there any performance difference between using Spring Data JpaRepository query methods and using Hibernate HQL?
I have seen the other similar questions here and they don't answer these questions.
I know that Spring Data JPA uses Hibernate
No. It uses JPA. Even though Hibernate is the default JPA implementation of Spring Boot, and the most popular JPA implementation, any JPA engine can be used.
Will I be able to use other Hibernate features such as C3P0
C3P0 has nothing to do with Hibernate. It's a connection pool. You can use any connection pool you wnt both with Spring and with Hibernate. Spring Boot uses HikariCP by default, and I would stick to that (it's a very good pool).
Hibernate second level caching
Yes.
or Hibernate sessions if I use Spring Data JPA?
There's really no good reason to use the old, proprietary Hibernate Session API, instead of using the standard JPA API. If you really need to, I don't see why you couldn't use it, but I wouldn't (and never had to).
What are the advantages of using #Query for writing custom queries over using hibernate sessions and HQL?
Query takes a HQL (JPQL, to be exact) query. If you use Query, you use HQL. The advantage is that you just need to declare the query. the binding of parameters, execution of the query, paging, etc. are done for you by Spring. But you can use custom repository implementations and use the native JPA API if you need to.
Is there any performance difference between using Spring Data JpaRepository query methods and using Hibernate HQL?
No.

spring-jdbc vs spring-data-jdbc and what are they supporting

I'm curious what is the difference between the spring-jdbc (what I missing in the newest spring release) and spring-data-jdbc.
Is there a difference or just a renaming (in the repositories I don't see this)?
And is there somewhere described what are the supported targets(DB/JDBC specs/JDK) of the versions?
e.g. for the plain JDBC from oracle I can see that information here:
http://www.oracle.com/technetwork/database/enterprise-edition/jdbc-faq-090281.html#01_03_1
(e.g.: JDBC Spec 4.1 in ojdbc7.jar on Java7/Java8 on Oracle DB 12.1/12cR1)
But I miss that for spring-jdbc - where do I find that information?
spring-jdbc
The docs for spring-jdbc are basically here:
https://docs.spring.io/spring/docs/current/spring-framework-reference/data-access.html
Though it doesn't specifically point you to the Spring project spring-jdbc. This project just provides all of the Spring abstractions over the plain JDBC DataSource that you can use with the Spring Framework. For example, Spring's DataSources which nicely hook into Spring's Transaction management capabilities, like the #Transactional annotation.
Also, the JdbcTemplate is part of this module, which allows you to execute SQL statements and extract objects from ResultSets without dealing with exception handling or the nasty details of properly closing statements, connections and the like.
spring-data-jdbc
spring-data-jdbc, on the other hand, provides the Spring Data abstraction over spring-jdbc. That is, you can create a Spring Data CrudRepository and a simple "entity" (not a JPA entity!) and, as Spring Data does, it will create your queries for you without you having to write native CRUD queries over JDBC, as in this example on the spring-data-examples git repo.
Using the referenced example as a demonstration:
interface CategoryRepository extends CrudRepository<Category, Long> {}
The above code is all you could need (using introspection on the Category object name as the source for the table name (based on a NamingStrategy) and it's properties as columns, again similar to JPA, but not using JPA.
Rather than writing your own like so:
#Repository
public class CategoryRepository {
public void create(Category category) {
jdbcTemplate.execute("insert...");
}
// The rest of my other CRUD operations
}

Diffrence between JPA API and hibernate native API

I am still a beginner in hibernate.I have started reading the user guide in which i found this architecture.
I know that hibernate is a jpa implementation and the jpa jar contains only interfaces.But i want to understand why JPA API is in the same level as Hibernate native api.
And if JPA contains only Interfaces how can we call for example entityManger.persist(entity) and normaly the entity manager is an interface.
where is the entity manger implementation ???
I know that hibernate is a jpa implementation and the jpa jar contains only interfaces.But i want to understand why JPA API is in the same level as Hibernate native api.
Because you can either use the JPA API (EntityManager, EntityManagerFactory etc) or Hibernate native API (Session, SessionFactory etc) to interact with the ORM entities and the database.
And if JPA contains only Interfaces how can we call for example entityManger.persist(entity) and normaly the entity manager is an interface. where is the entity manger implementation ???
JPA API like EntityManager, EntityManagerFactory etc are implemented by Hibernate (one of the JPA implementations, other like EclipseLink etc do exist). You will find that implementation in one of the hibernate jar files. .
A quick ref of Hibernate implementation of EntityManagerFactory here on grepcode

Trouble using Guice Persist together with JTA / bitronix

We have an existing j2se project that already uses JPA and guice-persist. Now, because we want to add JMS functionality, there is a request for 2-phase-commit and JTA. We'll use the bitronix transaction manager because there's no container (like spring).
To my understanding, the first thing we have to do is to change the transaction-type of the persistence unit from RESSOURCE-LOCAL to JTA, because we want database transactions to vote for commit rather then commit. The commit is done on phase 2 after collecting all votes.
With guice-persist we use the #Transactional annotation for methods that should run in a single transaction. The JPAPersistModule provides an EnitiyManagerFactory and it is used for guice-persist internal classes, like JpaLocalTxnInterceptor that wraps the annotated methods.
Now I get exceptions like
java.lang.IllegalStateException: A JTA EntityManager cannot use getTransaction()
at org.hibernate.ejb.AbstractEntityManagerImpl.getTransaction(AbstractEntityManagerImpl.java:1009)
at com.google.inject.persist.jpa.JpaLocalTxnInterceptor.invoke(JpaLocalTxnInterceptor.java:57)
...
because the JpaLocalTxnInterceptor calls getTransaction() on the provided entity manager.
I'm quite stuck, at the moment. Is there any way to use guice-persist together with JTA or o we really have to drop guice-persist from the project? Or, is there any replacement for guice-persist if we want to do JTA (with Bitronix)?
Had a similar situation. In our case we were using Guice + Jooq. We wanted Jooq because we have a large legacy Rails DB and wanted fine control plus speed. We picked Guice over Spring because we felt it is a better framework, and it is much faster and we like compile time checking.
We could NOT use Guice persist with Jooq, so we :
Use Atomikos JTA (free version)
Wrote our own #Transactional AOP annotation interceptor;
Our injectable Service provides the java.sql.Connection to our jooq processors, but always supply an Atomikos DataSource bean
We basically modified this code:
http://www.dailyjavatips.com/2011/10/24/database-transactions-google-guice-aop/
So that example uses regular JDBC Tx, but we modified it so it would use Atmomikos' JTA aware Tx instead.
Works like a charm!
Oje

Using Hibernate Validator with JPA and Spring

I'm using Hibernate Validator 4.0.2, Spring 3.0 and Hibernate 3.3.2 (which, as I understand it, is pre-JPA2) as a JPA 1 provider.
I've found it easy to integrate the Validator into the MVC layer (it just works) but can't see how to integrate the validator automatically into the JPA entityManager (JPA 1).
Basically, I have some entities that will be persisted but that do not come from the web layer and have therefore not already been validated. I'd like a neat way of running them through the validator pre-persist.
Is there an easy way of doing this? I'm aware that if I was using a JPA 2 provider (like Hibernate 3.5 when it is released), it'd be almost automatic. That's roughly what I'm looking for.
You need to write an entity listener and to trigger validation on #PrePersist, #PreUpdate and even #PreRemove (there are valid use-case for that). See Bean Validation with JPA 1.0 for a code sample.

Categories