Reputation: 429
Can someone please help me to autowire SessionFactory
to a Repository
class in Spring
? Following code gives me this error. Any suggestion to overcome this problem. I checked many similar topics in StackOverflow but none of them were successful.
"Error creating bean with name 'appointmentController': Unsatisfied dependency expressed through field 'iFactory': Error creating bean with name 'appointmentFactory': Unsatisfied dependency expressed through field 'factory': No qualifying bean of type [org.hibernate.SessionFactory] found for dependency ...etc"
Repository class
@Repository
@ComponentScan({"org.hibernate.SessionFactory"})
public class AppointmentFactory {
@Autowired
private SessionFactory factory ;
public SessionFactory getFactory() {
return factory;
}
public void setFactory(SessionFactory factory) {
this.factory = factory;
}
}
Controller class
@RestController
@ComponentScan({"com.mobios.ep.services","com.ombios.ep.entity.factory")
public class AppointmentController {
@Autowired
private AppointmentService iService;
@Autowired
private AppointmentFactory iFactory;
@RequestMapping(value="appointment/get", method=RequestMethod.POST)
public AppoinmentWM getApointmentById(@RequestBody AppointmentReq appointment) throws Exception{
Log4JUtil.logger.info("APPOINTMENT,appointment_get_request,Request="+appointment.toString());
AppointmentService appoinmentService = new AppointmentService();
StatsService statsService = new StatsService();
Mapper mapper = new Mapper();
AppoinmentWM gotAppointment = null;
}
Upvotes: 0
Views: 1536
Reputation: 217
I think in the constructor but I'm struggling with this too currently
public AppointmentFactory(@Qualifier("mySessionFactory") SessionFactory sessionFactory){
super.setSessionFactory(sessionFactory);
}
Upvotes: 1
Reputation: 402
Try the following
@Autowired
private Environment env;
@Autowired
DataSource dataSource;
@Bean(name = { "sessionFactory" })
public LocalSessionFactoryBean sessionFactory() {
LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
sessionFactory.setDataSource(dataSource);
sessionFactory.setPackagesToScan(new String[] { "com.mypackage.my.entities" });
sessionFactory.setHibernateProperties(hibernateProperties());
return sessionFactory;
}
@Bean
public HibernateTransactionManager transactionManager() {
final HibernateTransactionManager txManager = new HibernateTransactionManager();
txManager.setSessionFactory(sessionFactory().getObject());
return txManager;
}
Properties hibernateProperties() {
return new Properties() {
{
// @formatter:off
setProperty("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto"));
setProperty("hibernate.dialect", env.getProperty("hibernate.dialect"));
setProperty("hibernate.globally_quoted_identifiers", env.getProperty("hibernate.dialect"));
}
};
}
Or
@Configuration
public class DataConfigTest {
@Autowired
EntityManagerFactory entityManagerFactory;
//@Autowired
//HibernateJpaSessionFactoryBean hibernateJpaSessionFactoryBean;
@Autowired
HibernateEntityManagerFactory hemf;
@Autowired
DataSource dataSource;
@Bean(name = { "sessionFactory" })
public SessionFactory sessionFactory() {
return hemf.getSessionFactory();
// HibernateJpaSessionFactoryBean factory = new
// HibernateJpaSessionFactoryBean();
// factory.setEntityManagerFactory(entityManagerFactory);
// return factory.getObject();
}
@Bean
public HibernateTransactionManager transactionManager() {
final HibernateTransactionManager txManager = new HibernateTransactionManager();
txManager.setSessionFactory(sessionFactory());
return txManager;
}
}
Upvotes: 1