Spring Data JPA vs Hibernate : Where are the implementation of methods in JPARepository? - spring

I'm new to JPA and Hibernate. I understand that Hibernate is a implementation of JPA but Spring Data JPA is a JPA Data Access Abstraction (and not an implementation of JPA).
But
StudentRepository.java
#Repository
public interface StudentRepository extends JpaRepository<Student,Integer>{
}
Routing in Controller class
#GetMapping("/all")
public List<Student> listAllStudents(){
List listOfStudents = studentRepo.findAll();
return listOfStudents;
}
The above code works fine. (Full code and other classes not added to reduce verbosity.)
I haven't added Hibernate or any other JPA implementation, I have just used Spring data JPA. But the code works fine (as shown in many tutorials in the web).
This is the dependency of Spring data JPA and I'm using.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>2.6.7</version>
</dependency>
<dependency>
<groupId>org.mariadb.jdbc</groupId>
<artifactId>mariadb-java-client</artifactId>
<scope>runtime</scope>
</dependency>
What confuses me is if I haven't used any JPA implementation, how does this code work? Where is the implementation of the code?
In logs I could see things related to Hibernate although I haven't added Hibernate dependency or used used anything from Hibernate directly.
2022-07-05 10:55:59.470 INFO 13332 --- [ restartedMain] org.hibernate.Version : HHH000412: Hibernate ORM core version 5.6.9.Final
2022-07-05 10:55:59.729 INFO 13332 --- [ restartedMain] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {5.1.2.Final}
2022-07-05 10:55:59.899 INFO 13332 --- [ restartedMain] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.MariaDB106Dialect
Does it indirectly use Hibernate somehow?
PS: Sorry, if the question is naive, I'm trying to understand how this is working.
Edit : Full code : https://github.com/TMGautham/SpringBootFirst

The spring-boot-starter-data-jpa includes Hibernate. The starters are, as the name implies, starters to make it easier for you to start. So this starter includes
Spring Data JPA
Hibernate
Spring JDBC, Spring Transactions, Spring AOP and Spring Aspects (if needed).
So you automatically get those dependencies you need instead of you trying to cobble together working versions and compatible versions.
So yes you did use an implementation of JPA and by default with the starter it is Hibernate.

Related

Is Zonky embedded database supported for Spring boot 3.0 version?

I am trying to migrate my application from spring boot 2.7.2 to 3.0.0 version.
On running integration test case, I am getting below error :
java.lang.NullPointerException: Cannot invoke "org.hibernate.metamodel.mapping.JdbcMapping.getJdbcValueBinder()" because "jdbcMapping" is null
Hibernate ORM core version is 6.1.5.Final.
Could someone please guide if they have used zonky libraries with spring boot 3.0 ?
I have added below dependencies in pom.xml :
<dependency>
<groupId>io.zonky.test</groupId>
<artifactId>embedded-database-spring-test</artifactId>
<version>2.2.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.zonky.test</groupId>
<artifactId>embedded-postgres</artifactId>
<version>2.0.2</version>
<scope>test</scope>
</dependency>
In case the test class uses a spring context that already contains a data source bean, the data source bean will be automatically replaced with a testing data source. The newly created data source bean will be injected into all related components, and you can also inject it into a test class.

How to register a hibernate typedescriptor in a "classic" Spring 5 application with jpa entitymanager

I have a classic spring 5 application (NO spring boot). For a custom basic type i have created a typedescriptor by subclassing AbstractTypeDescriptor according to Hibernate 5.2 Documentation - 2.3.5. Explicit BasicTypes. I am using hibernate via an jpa entitymanager. I have two spring beans configured for that: a LocalContainerEntityManagerFactoryBean and a HibernateJpaVendorAdapter.
Now i am a bit lost, how to register the typedescriptor during startup of the application. The docs mention an approach using a hibernate configuration object. But i have no idea, how to get this? Any help appreciated!

How to advice entity classes not spring beans

i'm looking to advice setters of entity classes using AspectJ on Spring Boot, but i found that only spring beans could be advised.
Is there any trick to advice setters of entity classes (for example), these entity classes could not be spring beans.
only spring beans could be advised
Well, it's true in case you are using Spring AOP and not(!!!) AspectJ.
If replacing Spring AOP with AspectJ is an option, you can weave what ever you like by using #Configurable
Here
You can find the documentation that says that you can put Spring annotations like #Transactional on your non beans instances.

hibernate entity-manager v/s hibernate jpa package which to use

How do I configure hibernate?
If hibernate-entitymanager package is used for JPA then what good is of hibernate-jpa package. And I have seen that they use org.hibernate.ejb.HibernatePersistence class is used for JPA provider in persistence.xml which is in hibernate-entitymanager package.
In some cases, I have not seen the provider tag in persistence.xml. In this case which class is used?. Does Session class has EntityManager or implements it . And none of hibernate beans are specified in Spring-application context file directly. Instead spring.orm packages are used. So spring framework picks which class for JPA functionality.
org.springframework.orm.jpa.LocalEntityManagerFactoryBean picks which bean if provider tag is specified and if provider tag is not specified which bean is picked?
Hibernate JPA is a standard JPA implementation. See also these other questions about differences.
What's the difference between JPA and Hibernate?
Difference between Hibernate and Hibernate JPA
similarity and difference between jpa and hibernate
Session and EntityManager do approximately the same thing. EntityManager is the "new way."
I'm not clear on what your questions are about the persistence.xml file.
Thanks for reply but my Question was in which package the jpa implementation actually is . Is it in hibernate entitymanager.jar or hibernate-jpa2.1.jar . I found this fallowing link to be of little use
.https://docs.jboss.org/jbossas/docs/Server_Configuration_Guide/4/html/ch01s02s01.html
but in this case in persistence.xml file contains org.hibernate.ejb.HibernatePersistence as jpa provider but this class is in hibernate-entitymanager package . But they also told that hibernate-jpa-2.0-api.jar is the JAR containing the JPA 2.0 API, it provides all the interfaces and concrete classes that the specification defines as public API. Said otherwise, you can use this JAR to bootstrap any JPA provider implementation. in this fallowing link
https://docs.jboss.org/hibernate/entitymanager/3.5/reference/en/html/configuration.html please could some one help me with this

How to remove NullPointerException from JSR 330 Spring application

I am implementing Spring+JSF application following the guidlines of http://www.mkyong.com/jsf2/jsf-2-0-spring-integration-example/ but I am using the latest Spring version (i.e. 3.x, it already contains the JSR 330 implementation, according to the documentation, so this should not be an issue) and my own classes, that is the difference from the mentioned example.
I am stuck with NullPointerException that indicates that the bean (that acts as JSF managed) bean has not received injection of the instance of Spring bean. All the beans use JSR 330 annotations at appropriate places (#Named, #Inject) and the interfaces of the beans and the variables (under #Inject annotation) follow the usual naming patterns...
So - how to debug this situation. E.g. is there way to see all the beans that sit into the Spring application context. E.g. so we can determine that the Spring context is not initialized appropriately. Maybe there are other methods how to see what is happening in app (debug injection)?
Spring supports JSR 330 annotations for dependency injection provided you have the relevant jars in your classpath.
You need to add the following jar to your application.
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
</dependency>
And you might get good info by enabling logging for org.springframework
Just add log4j jar and drop in this log4j.properties file in the root of the classpath.
Log4j.properties:
log4j.rootCategory=INFO, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ISO8601} %-5p %c %M - %m\n
log4j.category.org.springframework=DEBUG
See also:
Documentation
beans-standard-annotations
overview-logging-log4j

Resources