SpringBoot configuration with pure Hibernate ORM without JPA - java

I'm new to SpringBoot development and I want to add Hibernate ORM framework to my SpringBoot project. I know
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
this dependency gives that capability but it's not supporting for every Hibernate features in pure Hibernate framework.
Example:
session.createNativeQuery(query).addEntity(LectureSchedule.class).list();
is there any way to configure Hibernate framework for SpringBoot applications not via JPA dependency and autowire SessionFactory so I can perform basic hibernate coding after that without using JPA methods?

If you don't want to use the spring starter pom file, you can add the hibernate dependencies one by one - depending on your needs.
The minimums are the hibernate-core package and spring-orm.
This tutorial describes how to do it step by step.

Related

How can I ensure that one spring-boot-starter is loaded before another?

I'm trying to ensure that one Spring Boot Starter is loaded before another. In this case, I have a custom spring boot starter written by my organization and the spring-boot-starter-data-jpa starter. It's crucial that the custom starter's bean is ran before spring-boot-starter-data-jpa is initialized. An example snippet of the dependencies in pom.xml:
<dependencies>
<dependency>
<groupId>com.example</groupId>
<artifactId>spring-boot-starter-my-customer-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
</dependencies>
How would one go about this? The solution I'm currently considering is adding a #Primary annotation to the top of the bean initialized in my company's custom starter, not sure if this would work and looking for best practice here. The bean I'm attempting to load in the custom starter before spring-boot-starter-jpa's beans are loaded is responsible for setting proxy settings, therefore must be loaded first so that traffic to the database can be routed via proxy.
Not really sure as to what you are trying to achieve here, but a way of controlling bean creation could be done using the DependsOn annotation.
In you case, since you would like to initialize your custom bean before the actual database connection is made, I think that adding this to your database configuration class should be enough.

Does Spring Data JPA require Spring Boot?

I'd like to write a small example program with Spring Data JPA.
Is Spring Boot a requirement to use this spring project?
No, you are perfectly fine to use Spring Data JPA by itself.
Do note that Spring Boot makes it easier to set up a project, all Spring Data JPA examples use:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
After trying, I found it is possible to use Spring Data JPA with plain Spring, without spring boot.
When not using boot, the following are needed:
1) spring-core maven dependency
2) spring-context dependency
3) spring-data-jpa dependency
4) hibernate-entitymanager or some other JPA provider
5) mysql-connector-java or some other DB connector
6) javax.persistence-api dependency
7) AnnotationConfigApplicationContext instead of SpringBootApplication
8) #EnableJpaRepositories("mypackage")
9) #ComponentScan("mypackage")
10) #Bean for LocalContainerEntityManagerFactoryBean and dataSource
11) Setting Hibernate properties to EntityManager
12) #Bean for PlatformTransactionManager
You can use Spring without Boot module but, in your case, you'll have to consider building an project containing Spring Core + Data + JPA and run it inside an Application Server.
If you just do some small example, like you said, Spring Boot + Data + JPA might be faster and easier to setup and run.

Understanding Spring Boot

I am trying to understand the difference between spring boot and spring boot web. From this boot tutorial the pom contains spring boot as the parent and spring boot web as a dependency like so:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.1.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
What are the uses for the 2 different versions? Do you always use them together? This spring boot documentation tells me if the program is production ready to use:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
But if that's the case then why isn't there one for web like so:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web-actuator</artifactId>
</dependency>
</dependencies>
There are lots of these different 'starter' poms for spring boot. Each one of them tells maven to bring in the dependencies needed for that particular functionality. So spring-boot-starter-web brings in all the stuff needed for MVC and autoconfigures it with sensible defaults. The real trick to spring boot is it when it autoconfigures things it uses a whole of #ConditionalOnClass or other such annotations that look at what dependencies are on the classpath and provides configuration for those dependencies. What this means is when you have that spring boot actuator starter, when it's doing its autoconfiguration it will look at what other spring boot start poms you have in your pom.xml and it will configure different endpoints for the actuator so you can see the various metrics the actuator provides for that particular module.
Spring Boot is a framework, spring-boot-starter-web is one of the packages that comes with it, a jar file.
Just like JDK is a library, and util is one of the packages included in the JDK.
From: https://docs.spring.io
Spring Boot provides a number of “Starters” that auto-configures your application adds jars to your classpath. The spring-boot-starter-parent is a core starter that provides useful maven defaults. It also provides a dependency-management section so that if you import additional starters then you can omit version tags for “blessed” dependencies. Therefore you should only need to specify the Spring Boot version number on this dependency
Starters are a set of convenient dependency descriptors that you can include in your application considering each starter covers a specific area.The starters contain a lot of the dependencies that you need to get a project up and running quickly and with a consistent, supported set of managed transitive dependencies.For example, if you want to get started using Spring and JPA for database access, just include the "spring-boot-starter-data-jpa" dependency in your project, and you are good to go.
Spring Boot
spring-boot-starter-web is starter for building web, including RESTful, applications using Spring MVC. It uses Tomcat as the default embedded container
Spring Boot has different groups of starters like
1-Spring Boot application starters: spring-boot-starter-web , spring-boot-starter-jdbc , spring-boot-starter-jpa etc
2-Spring Boot production starters : spring-boot-starter-actuator which provides production ready features to help you monitor and manage your application
3-Spring Boot technical starters: spring-boot-starter-jetty , spring-boot-starter-tomcat these starters can be used to exclude or swap specific technical facets
1.spring-boot-starter-parent deals with the auto start of main method and run methods so on..
2.and spring-boot-starter-web deals with the spring MVC things like controller, autowired so on.. Hope this helps..
Spring boot is a very cool tool of Spring Source. In many conference the team talck about of Spring Boot as one of the three DSR(Domain Specific Run-Time) of Pivotal.... Spring Boot, Spring XD and Grails(now Pivotal didn't support Groovy & Grails).
That said compare Spring boot stand alone and Spring boot web enviroment may be a cool conversation. First of all Spring boot give you many production ready istruments such as actuator(avaiable whit sprin boot in web enviroment), spring remote shall and so on. The main difference is the same of had a spring stand-alone context or a Spring web context. Of course some of the potentiality that yon could have are avaiable in a web context, Actuator is an example. but the main difference is in what kind of application you need, web or stand-alone.

Is it possible to use OSGi Enterprise 4.x & JPA 2.1 Entity Listener injected via blueprint?

We are in the OSGi world.
JPA 2.1 supports injectable EntityListner.
And EclipseLink supports JPA 2.1.
But the OSGi Enterprise Spec 4.x only supports JPA 2.0.
So it seems that injectable EntityListener doesn't work in OSGi, even when EclipseLink supports them.
Is it currently possible to define EntityListener in blueprint.xml and use them for injection? This would allow to inject OSGi Services into an Entity Listener. (Currently we need an explicit lookup using FrameworkUtil to do the lookup.)
Has anybody done so successfully and can share some hints, especially in regard to the used bundle (versions)?
Have you tried using the following maven dependency with org.eclipse.persistence.jpa.PersistenceProvider in the provider-Element of the persistence.xml? In the case of a JEE application server, the classloader would prefer libraries packaged with the deployed application by the ones bundled with the server itself.
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>eclipselink</artifactId>
<version>2.5.1</version>
<scope>provided</scope>
</dependency>

I have javax.inject in my pom.xml, will spring use it automatically?

I copied a pom.xml while I was going through a spring mvc tutorial online, and it had:
<!-- #Inject -->
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
</dependency>
From what I understand spring has its own dependancy injection built-in, will this override the default and use javax.inject?
I have also seen slf4j in pom's, with no further setup in code or xml.
How does this work under the covers, spring examines the lib's folder and if any lib is found that is overridable it does it?
The dependency in your pom for javax.inject
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
</dependency>
has nothing to do with Spring . It just brings in a javax.inject dependency into the project . A jar with the name javax.inject-1.jar . This jar is needed if you use the #Inject annotation which is supported by Spring as well .
You could use #Autowired/#Resource/#Inject as per your needs . See here for their difference and also a discussion at What is the difference between #Inject and #Autowired in Spring Framework? Which one to use under what condition? .
As for slf4j , it is just an abstraction over frameworks like log4j allowing run time plugging in of logging framework . Spring-OSGI , Hibernate all use this internally. So this is why you find the dependency in your pom . Hope this clears things out.
inject enables JSR 330 support to spring. Using the inject annotations makes the application not tied to spring - it could be switched to Java EE 6 or guice or other providers which support the specification.
You could either use inject or spring injection (or perhaps both).
as #Aravind told javax.inject dependency has nothing to do with spring. and it's used to get annotation #Inject, it's is part of the Java CDI standard, introduced in Java EE 6 (JSR-299)
Spring using #Inject synonymously with their own #Autowired annotation.
#Autowired and #Inject, two annotations works the same way as Spring has decided to support some JSR-299 annotations in addition to their own.
comming to
Simple Logging Facade for Java (SLF4J):
The Simple Logging Facade for Java or (SLF4J) serves as a simple
facade or abstraction for various logging frameworks, e.g.
java.util.logging, log4j and logback, allowing the end user to plug in
the desired logging framework at deployment time.

Categories