'Troubles with Current Session Context Hibernate
I'm coding web library project and I have an issue with Hibernate Current Session Context. I receive
error 500 message No CurrentSessionContext configured!
when I try to access books page. When I try to access index page everything is OK
Index page
Internal error 500 NoCurrentSessionConfigured
It is my Hibernate Configuration file
package com.library.config;
import org.apache.tomcat.dbcp.dbcp2.BasicDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.orm.hibernate5.HibernateTransactionManager;
import org.springframework.orm.hibernate5.LocalSessionFactoryBean;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import javax.sql.DataSource;
import java.util.Properties;
@Configuration
@ComponentScan("com.library")
@EnableTransactionManagement
@PropertySource(value = "classpath:application.properties")
public class HibernateConfig {
private Environment environment;
@Autowired
public void setEnvironment(Environment environment) {
this.environment = environment;
}
private Properties hibernateProperties() {
Properties properties = new Properties();
properties.put("hibernate.dialect", environment.getRequiredProperty("hibernate.dialect"));
properties.put("hibernate.show_sql", environment.getRequiredProperty("hibernate.show_sql"));
return properties;
}
@Bean
public DataSource dataSource() {
BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName(environment.getRequiredProperty("jdbc.driverClassName"));
dataSource.setUrl(environment.getRequiredProperty("jdbc.url"));
dataSource.setUsername(environment.getRequiredProperty("jdbc.username"));
dataSource.setPassword(environment.getRequiredProperty("jdbc.password"));
return dataSource;
}
@Bean
public LocalSessionFactoryBean sessionFactory() {
LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
sessionFactory.setDataSource(dataSource());
sessionFactory.setPackagesToScan("com.library.model");
sessionFactory.setHibernateProperties(hibernateProperties());
return sessionFactory;
}
@Bean
public HibernateTransactionManager transactionManager() {
HibernateTransactionManager transactionManager = new HibernateTransactionManager();
transactionManager.setSessionFactory(sessionFactory().getObject());
return transactionManager;
}
}
BookController.java
package com.library.controller;
import com.library.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/books")
public class BookController {
@Autowired
private BookService bookService;
@GetMapping
public String allBooks(Model model) {
model.addAttribute("books", bookService.findAll());
return "books";
}
}
BookServiceImpl.java
package com.library.service.impl;
import com.library.dao.BookDAO;
import com.library.model.Book;
import com.library.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@Service
public class BookServiceImpl implements BookService {
@Autowired
private BookDAO bookDAO;
@Override
@Transactional
public void addBook(Book book) {
bookDAO.addBook(book);
}
@Override
@Transactional
public List<Book> findAll() {
return bookDAO.findAll();
}
@Override
@Transactional
public Book findBookById(Integer id) {
return bookDAO.findBookById(id);
}
@Override
@Transactional
public Book findBookByTitle(String title) {
return bookDAO.findBookByTitle(title);
}
@Override
@Transactional
public void updateBook(Book book) {
bookDAO.updateBook(book);
}
@Override
@Transactional
public void deleteBook(Book book) {
bookDAO.deleteBook(book);
}
}
BookDAO.java
package com.library.dao.impl;
import com.library.dao.BookDAO;
import com.library.model.Book;
import org.hibernate.SessionFactory;
import org.hibernate.query.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public class BookDAOImpl implements BookDAO {
@Autowired
private SessionFactory sessionFactory;
@Override
public void addBook(Book book) {
sessionFactory.getCurrentSession().persist(book);
}
@Override
public List findAll() {
return sessionFactory.getCurrentSession().createQuery("from Books").list();
}
@Override
public Book findBookById(Integer id) {
return sessionFactory.getCurrentSession().get(Book.class, id);
}
@Override
public Book findBookByTitle(String title) {
Query query = sessionFactory.getCurrentSession().createQuery("from books where title=:title");
query.setParameter("title", title);
return (Book) query.uniqueResult();
}
// @Override
// public List<Book> findBookByAuthor(Author author) {
// return null;
// }
@Override
public void updateBook(Book book) {
sessionFactory.getCurrentSession().update(book);
}
@Override
public void deleteBook(Book book) {
sessionFactory.getCurrentSession().delete(book);
}
}
application.properies
jdbc.driverClassName = com.mysql.cj.jdbc.Driver
jdbc.url = jdbc:mysql://localhost:3306/Library
jdbc.username = root
jdbc.password = password
hibernate.dialect = org.hibernate.dialect.MySQL8Dialect
hibernate.show_sql = true
It is my project structure
I have tried different things such as changing getCurrentSession() to openSession()
or adding
<property name="hibernate.current_session_context_class">
org.hibernate.context.internal.ThreadLocalSessionContext
</property>
or
<property name="hibernate.current_session_context_class">
thread
</property>
Also checked connection with database. Nothing helps. Could you please, suggest what I'm doing wrong?
Solution 1:[1]
Maybe because "sessionFactory.getCurrentSession()" and there's no session opened,I suggest you try to open a session from your sessionFactory object e.g Session session = sessionFactory.openSession(); ,do the findAll with the session object and then close it,hope it works for you !
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | karim farhouti |



