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

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.

Related

Integration of Flyway into JPA + OSGi

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
}
}

Packaging JPA entities in a jar inside a Spring Boot application

I am refactoring a JEE REST (using JAX-RS 2.0) application as a Spring Boot application. My old app is packaged in a .war and has a jar file with entities and the persistence.xml configuration file for JPA. This jar is copied into WEB-INF/lib directory. I know Spring JPA works a different way and I don't use persistence.xml now but I wonder if I can package my JPA entity classes in a jar and include them in my Spring Boot apps just like I am doing now. This way I can easily reuse that jar in different Spring Boot Applications.
I'm pretty certain you can do this since I have done the same on one of my projects very recently. The only thing you need to do is make sure that you add an #EntityScan annotation on your main Spring Boot config class with the base package of your entities in the JAR.
#EntityScan("my.external.jar.entity.package")
Spring Boot doesn't really care whether the JPA entities are packages as a separate jar or included into the application. Its a runtime framework and in runtime classes can be loaded from the jar (it should reside in BOOT-INF/lib or 'directly' from the *.class files in the spring boot artifact.
Now there is a rule in spring boot, that says that it will scan for beans (including entities) only in the package where your "main" class resides or under it. This is done in order to avoid long process of analysis of, say, third-party classes that you might use. These third-party classes are usually not spring aware at all, at certainly do not contain any spring beans.
Example:
Say, you place your "main" class (the one annotated with #SpringBootApplication) in the package: com.mycompany.myapp
In this case, the following packages will be scanned (just a couple of examples):
com.mycompany.myapp
com.mycompany.myapp.web
com.mycompany.myapp.services.bl
com.mycompany.myapp.whatever.doesnt.matter
...
The following packages won't be scanned however (again, examples, not the full list):
com.mycompany
com.anothercompany
org.hibernate
If you want to to "alter" this default rule and place the entities in the package that doesn't adhere this convention, for example com.mycompany.jpa.entities then you should indeed use #EntityScan annotation as our colleagues have already suggested.
You can read about this topic here. You might also need to get familiar with #EnableJpaRepositories if you're using spring data but, while related, its a different topic.
In my case I had this problem, and after importing the library in the application's pom.xml, in the SpringBoot Project Main class, insert an #EntityScan annotation with the first package and *. Like this: #EntityScan ("br.*")

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?

Facing Spring Roo issue while converting JPA project to non-JPA project

I have a Spring - JPA project generated using Spring Roo. Now for some requirement we are replacing our JPA layer with some other framework (MyBatis) which is not JPA-compliant. I have done the changes and they are working fine also. I have removed all the JPA dependencies from my pom.xml file.
I am facing issues with my JUnit test project which is used to test DAO layer. Spring Roo is looking for the javax.persistence.Entity class which is part of JPA specific jar.
Please find below the error details below:
can't determine annotations of missing type javax.persistence.Entity
when weaving type ****.**.***Test
when weaving classes
when weaving
when batch building BuildConfig[null] #Files=12 AopXmls=#0
[Xlint:cantFindType]
error at (no source information available
Can anyone please advise me how to remove this error without adding a JPA dependency?
You could try to make a push-in from your test .aj file to the related .java file and then remove the necessary code (imports, annotations) that makes that your Spring Roo shell shows that error.
If you are not sure about how to make a push-in, you could read Spring Roo 1.3.2.RELEASE documentation http://docs.spring.io/spring-roo/docs/1.3.2.RELEASE/reference/html/removing.html#removing-step-by-step-1
Regards,

Value of provider in Persistence.xml

What is the value to be set in provider XML element of persistence.xml if the Oracle database is used? Which jars need to included to write a simple JPA application?.
I have currently included only ejb3-persistence.jar.
When the application is run, below error is seen,
javax.persistence.PersistenceException: No Persistence provider for EntityManage
Decide which JPA implementation you're going to use :- some to choose from DataNucleus, OpenJPA, Hibernate, EclipseLink all of which support persistence to Oracle RDBMS, and there are likely others. Each has a "provider" class name, so you use that in persistence.xml