Today we are going to learn how to create hibernate session factory in spring boot. Here we are going to learn how to implement hibernate session factory in spring boot with its configuration and enable support for spring boot application.
I am new to this spring boot and i am creating my new project to learn step by step. If i found any obstacle like this one, i am going to post here with its solutions.
Today i was developing modules for my project with spring boot + hibernate using maven but found that we cannot use hibernate session factory directly. So, here we are going to learn how to configure it.
I was getting following error in my console window.
First let me show you my directory structure of my project that i have been working on so that you will not get confused on what i am going to do below.
If you have seen the image, i am using repository pattern to develop my spring boot application project. As you can see in my categoryDAO.java
class we need to implement session factory and was keep on getting NullPointer exception
.
So to use Hibernate Session Factory we need to add this new function or line in my CategoryDAO
.
1 2 3 4 5 6 7 |
@Autowired private EntityManagerFactory entityManagerFactory; public Session getSession() { Session session = entityManagerFactory.unwrap(SessionFactory.class).openSession(); return session; } |
After you copied above codes, you have to use getSession()
in your functions in order to use hibernate session factory properties.
I have been preparing spring boot hibernate maven example with create read and update tutorial by using MySQL as an database. If you want to follow that tutorial stay tuned.
Leave a Reply