Integration of Flyway into JPA + OSGi - jpa

in my current project I have integrated JPA into OSGi standalone application. For integration I have taken following OSGi specification Implementations:
OSGi R7 Platform (Equinox 3.13.0)
JPA 2.2 (Eclipselink 2.7.1)
JPA Container
Apache Aries JPA Container 2.7.0
Apache Aries JPA Eclipselink adapter 2.7.0
JDBC Service (PAX JDBC MariaDB 1.3.0)
This integration works perfect.
The next step to go - Flyway integration. The DB Migration Scripts should be packed directly into Persistence Bundle. Now I would like to trigger the migration exactly when DataSource is created, immediately before EntityManagerFactory and EntityManagerFactoryBuilder Services will be Registerd. At this moment I should have access to Persistence Bundle class loader and i should have an initialized Datasource. The only solution, that I have found, is to refactor Apache Areas JPA Container and put a Flyway migration call into AriesEntityManagerFactoryBuilder.dataSourceReady. The Flyway trigger is stored as locations in JPA properties like this:
<property name="org.flywaydb.Locations" value="classpath:com/hrrm/budget/domain/account/migrations"/>
This solution is correct placed at a perfect time to call. But it is not confirm with OSGi JPA Service Specification 1.1 and was implemented as a hook into Apache Aries JPA Container.
Is there another more perfect and specification-confirm solution to integrate Flyway into my project?

To hook into the DataSource creation you can use a PreHook service like described in the pax-jdbc docs.
You can find an example here.
#Component(property="name=persondb")
public class Migrator implements PreHook {
public void prepare(DataSource ds) throws SQLException {
// Put your migration calls here
}
}

Related

How to inject EntityManager in a MDB which is configured in ejb-jar.xml

I have demo application with JPA 2.1 and weblogic 12.1.3 deployment works fine and am able to get EntityManager which is annotated on the stateless bean.
I have application where I have ejb-jar.xml, on this file we have configured all MDB and session beans, Note: application is not using annotation.
And ejb-jar.xml uses http://java.sun.com/dtd/ejb-jar_2_0.dtd(ejb 2), but our application is running on weblogic 12.1.3. On this configuration, I tried to get EntityManager with the help of annotation in MDB, but am getting null object.
Question.
1. Is it possible to annotate EntityManager, with above configuration.
2. If not possible what are alternative ways. meaning I need to move all the configuration of ejb-jar.xml to annotation.
Thanks
Daya

Does Spring Data JPA internally use Hibernate & why my app is working if I am not giving dialect property?

I just started learning Spring Data JPA, I connected to mysql in localhost and able to save a record but I am unable to understand why it is working if I am not giving dialect property in properties file and is hibernate a default implementation of spring data instead of ibatis or Eclispe link, because in my pom.xml I just added the dependency of spring-data-jpa and never mentioned what kind of JPA implementation I want to use.
application.properties
spring.jpa.hibernate.ddl-auto=create
spring.jpa.properties.hibernate.
spring.datasource.url=jdbc:mysql://localhost:3306/initsoftware
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.username=ppppppp
spring.datasource.password=xxxxxxx
logging.level.root=DEBUG
spring.jpa.show-sql=true
Since you have an application.properties I assume you are using Spring Boot and not just Spring Data JPA.
In order to use JPA with Spring Boot you would typically add spring-boot-starter-data-jpa to your dependencies. This indeed comes with Hibernate out of the box as you can see when you inspect the dependencies.
Spring Data JPA itself doesn't come with a JPA implementation. You have to add that.
iBatis is not a JPA implementation.
If the assumption above doesn't match your scenario you can use the maven dependency plugin to inspect your (transient) dependencies. The following is a good starting point.
mvn dependency:tree -Dverbose
If you use a different build tool, it probably has a similar feature.

Spring boot with shared JavaEE entities jar

I got a problem where I'm not able to find a solution.
We got a shared library with all entities which are setup to use with JavaEE 6 entities (Websphere). So mapping is based on the JavaEE 6 jpa annotations.
unfortunately this doesn't work out of the box with Spring Boot. Because not all annotations combinations are supported.
e.g.:
Caused by: org.hibernate.AnnotationException: #Column(s) not allowed on a #OneToOne property:
Used Database is db2. The connection works fine. Only the share entity model does not work out the box.
I'm looking now to use a spring boot application that uses openjpa with a db2 database and a persistence.xml with an external jar file. Someone got any experience with this?

PersistenceUnit null with Glassfish and Quartz

I'm building a simple web app using jsp and servlets. The application is deployed on Glassfish server and use JPA / Hibernate.
I need to create a Quartz Job that work with JPA (select / update...). I've tried to add :
#PersistenceUnit
private EntityManagerFactory emf;
into my Job but it's null. By the way it works for my servlets and ServletContextListener.
I don't see how I can force glassfish to inject the persistence unit.
Any idea ?
THX
Injecting resources works only for container managed classes. Servlets and ServletContextListeners are container managed classes, your Quarz Job is not. Easiest way around is to use JNDI lookup.

Websphere 7 EntityManagerFactory creation problem

I'm working on a maven project which uses seam 2.2.0, hibernate 3.5.0-CR-2 as JPA provider, DB2 as database server and Websphere 7 as application server. Now I'm facing de following problem:
In my EJBs that are seen also as SEAM components I want to use the EntityManager from EJB container (#PersistenceContext private EntityManager em) not Seam's EntityManager (#In private EntityManager em). But this is the problem, I cannot obtain an EntityManager using #PersistenceContext.
On server logs it sais that it cannot create an EntityManagerFactory and gets a ClassCastException:
java.lang.ClassCastException: org.hibernate.ejb.HibernatePersistence incompatible with javax.persistence.spi.PersistenceProvider
After a lot of debugging and searching on forums I'm assuming that the problem is that Websphere doesn't use the Hibernate JPA provider.
Has anyone faced this problem and has a solution? I configured already WAS class loader order for my application to load the classes with the application class loader first and I\ve packed all necessary jars in application ear as written in: WAS InfoCenter: Features for EJB 3.0 development . If necessary I'll post my persistence.xml, components.xml files and stack trace.
I've found this problem discussed also here:
Websphere EntityManagerFactory creation problem
Hibernate 3.3 fail to create entity manager factory in Websphere 7.0. Please help
Any hint will be useful.
Thanks in advance!
Mihaela
I suspect that you've included the JPA API jar in your EAR. When using "parent last" (also known as "load classes with application class loader first"), your application is loading a second copy of the javax.persistence.spi.PersistenceProvider class, which is incompatible with the copy included in WAS. You need to either remove those classes from your EAR or change back to "parent first" delegation mode.