gpt4 book ai didi

java - 如何在 Spring 中将 SessionFactory Autowiring 到 Repository 类

转载 作者:行者123 更新时间:2023-12-02 11:54:30 25 4
gpt4 key购买 nike

有人可以帮我将 SessionFactory Autowiring 到 Spring 中的 Repository 类吗?以下代码给了我这个错误。任何解决这个问题的建议。我在 StackOverflow 上查了很多类似的主题,但没有一个成功。

"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
@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 类

@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;

}

最佳答案

尝试以下操作

        @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"));


}
};
}

或者

@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;
}
}

关于java - 如何在 Spring 中将 SessionFactory Autowiring 到 Repository 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47694766/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com