I am new to JavaEE and i have been working on a simlpe Springboot project. Each time i run it i get this error:
Please feel free to answer to my question and any improvemnet in the code is Highly Appreciate.
Field sessionFactory in com.example.dao.CartDaoImpl required a bean of type 'org.hibernate.SessionFactory' that could not be found.
Action:
Consider defining a bean of type 'org.hibernate.SessionFactory' in your configuration.
It is possible to get Session from EntityManager which is already configured in spring-boot-starter-data-jpa
. So inject EntityManager instead of SessionFactory:
@Autowired
private EntityManager entityManager;
private Session getSession() {
return entityManager.unwrap(Session.class);
}
And use getSession() method where you need.
spring.jpa.properties.hibernate.current_session_context_class=org.springframework.orm.hibernate4.SpringSessionContext
and add this
spring.datasource.url=......
spring.datasource.username=....
spring.datasource.password=.....
spring.jpa.properties.hibernate.dialect=.......
spring.jpa.hibernate.ddl-auto=update
here is good example for configuration A Guide to Hibernate with Spring 4
Just remarks: 1# you use in UserServiceImpl for example
@Component
@Service
public class UserServiceImpl implements UserService {...
....
}
use only @Component or @Service but not both as it's redundant. Service is alread a Component.
2# in methods where you have only read operation us instead of default @Transactional
this @Transactional(readOnly = true)
3# in methods like void addCustomerOrder(CustomerOrder customerOrder);
better return boolean or some object like CustomerOrder than return just void.
4# class Queries is not Serializable
5# better use lazy as default value than fetch = FetchType.EAGER
6# dao class CartDaoImpl has dependency on service class, it's strange.
7# in some cases you have transaction on dao level in another on service
8# if you can create sub package impl and move all implementation into one.
you will have com.dao with N interfaces and com.dao.impl with N implementations for them, and not one package com.dao with N+N intercases and classes
add this into pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
and create package configs com.configs
and configure like DatabaseConfig
i see you project and i found that there is no SessionFactory
class or configuration for SessionFactory
into your project and you are use @Autowired
for SessionFactory
in CartDaoImpl
clas. that is the main issue. you need to configure the SessionFactory
yo can refer bellow example: http://www.devglan.com/spring-boot/spring-boot-hibernate-5-example
Create service and use it to get session from EntityManager:
@Service
public class HibernateService {
@PersistenceContext
private EntityManager entityManager;
public <R> NativeQuery<R> createNativeQuery(String sqlString, Class<R> resultClass) {
return getSession().createNativeQuery(sqlString, resultClass);
}
public NativeQuery createNativeQuery(String sqlString) {
return getSession().createNativeQuery(sqlString);
}
public Session getSession() {
return entityManager.unwrap(Session.class);
}
}
you can add more methods to reduce amount of written code.
Using Hibernate 5 simple check the below
-- Check the base package is scan (HibernateConfig.java)
-- Check the valid annotation in file @Configuration @EnableTransactionManagement
-- check the all valid beans are created or not LocalSessionFactoryBean,HibernateTransactionManager,HibernateTemplate
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.