我正在编写一个Java应用程序,该应用程序应该公开一些rest api并建立db oracle连接。
我创建了一个用于定义DataSource,sessionFactory和transactionManager的类:
@Configuration
@ConfigurationProperties("oracle")
@EnableAutoConfiguration(exclude = { //
DataSourceAutoConfiguration.class, //
DataSourceTransactionManagerAutoConfiguration.class, //
HibernateJpaAutoConfiguration.class })
public class BeanFactory {
@Autowired
private Environment env;
@Bean(name = "dataSource")
public DataSource getDataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
// See: application.properties
dataSource.setDriverClassName(env.getProperty("spring.datasource.driver-class-name"));
dataSource.setUrl(env.getProperty("spring.datasource.url"));
dataSource.setUsername(env.getProperty("spring.datasource.username"));
dataSource.setPassword(env.getProperty("spring.datasource.password"));
return dataSource;
}
@Autowired
@Bean(name = "sessionFactory")
public SessionFactory getSessionFactory(DataSource dataSource) throws Exception {
Properties properties = new Properties();
// See: application.properties
properties.put("hibernate.dialect", env.getProperty("spring.jpa.properties.hibernate.dialect"));
properties.put("show_sql", env.getProperty("spring.jpa.show-sql"));
properties.put("current_session_context_class", env.getProperty("spring.jpa.properties.hibernate.current_session_context_class"));
properties.put("hibernate.temp.use_jdbc_metadata_defaults",false);
// Fix Postgres JPA Error:
// Method org.postgresql.jdbc.PgConnection.createClob() is not yet implemented.
LocalSessionFactoryBean factoryBean = new LocalSessionFactoryBean();
// Package contain entity classes
factoryBean.setPackagesToScan(new String[] { "" });
factoryBean.setDataSource(dataSource);
factoryBean.setHibernateProperties(properties);
factoryBean.afterPropertiesSet();
//
SessionFactory sf = factoryBean.getObject();
System.out.println("## getSessionFactory: " + sf);
return sf;
}
@Autowired
@Bean(name = "transactionManager")
public HibernateTransactionManager getTransactionManager(SessionFactory sessionFactory) {
HibernateTransactionManager transactionManager = new HibernateTransactionManager(sessionFactory);
return transactionManager;
}
}
然后我创建一个尚未实现的类DAO,就像一个存根。我需要sessionFactory才能创建它:
@Repository
@Transactional
public class ConnectionDAO {
@Autowired
private SessionFactory sessionFactory;
public Customer getCustomer(String name){
return new Customer("","");
}
public Boolean setCustomer(String name, String surname){
/*
*
*/
return false;
}
}
最后,我有了带有API REST组件的类。我尝试将DAO对象创建为自动装配或通过新方法创建,但仍然无法使用:
@RestController
public class TrainingRestController {
@Autowired
private ConnectionDAO daoConnection;
@RequestMapping("/GetGroup")
@GetMapping
public Customer retrieve(@RequestParam(value="nome") String name){
log.info("Retrieving " + name);
return daoConnection.getCustomer(name);
}
@RequestMapping("/SetGroup")
@PostMapping
public boolean set(String name, String surname){
log.info("Setting " + name + " " + surname);
return daoConnection.setCustomer(name, surname);
}
}
问题是未填充到DAO类中的sessionFactory。 我也尝试将autoWire删除到sessionFactory Bean上的BeanFactory类中,但是不起作用。 我缺少一些注释吗?
如果将daoConnection设置为Autowired,我将执行公共客户检索():
java.lang.ClassCastException: org.springframework.orm.jpa.EntityManagerHolder无法转换为 org.springframework.orm.hibernate5.SessionHolder位于 org.springframework.orm.hibernate5.HibernateTransactionManager.doGetTransaction(HibernateTransactionManager.java:399) 〜[spring-orm-5.0.9.RELEASE.jar:5.0.9.RELEASE]在 org.springframework.transaction.support.AbstractPlatformTransactionManager.getTransaction(AbstractPlatformTransactionManager.java:342) 〜[spring-tx-5.0.9.RELEASE.jar:5.0.9.RELEASE]在 org.springframework.transaction.interceptor.TransactionAspectSupport.createTransactionIfNecessary(TransactionAspectSupport.java:474) 〜[spring-tx-5.0.9.RELEASE.jar:5.0.9.RELEASE]在 org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:289) 〜[spring-tx-5.0.9.RELEASE.jar:5.0.9.RELEASE]在 org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:98) 〜[spring-tx-5.0.9.RELEASE.jar:5.0.9.RELEASE]在 org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185) 〜[spring-aop-5.0.9.RELEASE.jar:5.0.9.RELEASE]在 org.springframework.aop.framework.CglibAopProxy $ DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:688) 〜[spring-aop-5.0.9.RELEASE.jar:5.0.9.RELEASE]在 com.test.ConnectionDAO $$ EnhancerBySpringCGLIB $$ 7579b8ee.getCustomer() 〜[classes /:na]在 com.test.TrainingRestController.retrieve(TrainingRestController.java:18) 〜[classes /:na]在sun.reflect.NativeMethodAccessorImpl.invoke0(本机 方法)〜[na:1.8.0_181]在 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 〜[na:1.8.0_181]在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 〜[na:1.8.0_181]在java.lang.reflect.Method.invoke(Method.java:498) 〜[na:1.8.0_181]在 org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:209) 〜[spring-web-5.0.9.RELEASE.jar:5.0.9.RELEASE]在 org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:136) 〜[spring-web-5.0.9.RELEASE.jar:5.0.9.RELEASE]在 org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:102) 〜[spring-webmvc-5.0.9.RELEASE.jar:5.0.9.RELEASE]在 org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:891) 〜[spring-webmvc-5.0.9.RELEASE.jar:5.0.9.RELEASE]在 org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:797) 〜[spring-webmvc-5.0.9.RELEASE.jar:5.0.9.RELEASE]在 org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) 〜[spring-webmvc-5.0.9.RELEASE.jar:5.0.9.RELEASE]在 org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:991) 〜[spring-webmvc-5.0.9.RELEASE.jar:5.0.9.RELEASE]在 org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:925) 〜[spring-webmvc-5.0.9.RELEASE.jar:5.0.9.RELEASE]在 org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:974) 〜[spring-webmvc-5.0.9.RELEASE.jar:5.0.9.RELEASE]在 org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:866) 〜[spring-webmvc-5.0.9.RELEASE.jar:5.0.9.RELEASE]在 javax.servlet.http.HttpServlet.service(HttpServlet.java:635) 〜[tomcat-embed-core-8.5.34.jar:8.5.34]在 org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:851) 〜[spring-webmvc-5.0.9.RELEASE.jar:5.0.9.RELEASE]在 javax.servlet.http.HttpServlet.service(HttpServlet.java:742) 〜[tomcat-embed-core-8.5.34.jar:8.5.34]在 org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) 〜[tomcat-embed-core-8.5.34.jar:8.5.34]在 org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) 〜[tomcat-embed-core-8.5.34.jar:8.5.34]在 org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) 〜[tomcat-embed-websocket-8.5.34.jar:8.5.34]在 org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) 〜[tomcat-embed-core-8.5.34.jar:8.5.34]在 org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) 〜[tomcat-embed-core-8.5.34.jar:8.5.34]在 org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99) 〜[spring-web-5.0.9.RELEASE.jar:5.0.9.RELEASE]在 org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) 〜[spring-web-5.0.9.RELEASE.jar:5.0.9.RELEASE]在 org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) 〜[tomcat-embed-core-8.5.34.jar:8.5.34]在 org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) 〜[tomcat-embed-core-8.5.34.jar:8.5.34]在 org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:109) 〜[spring-web-5.0.9.RELEASE.jar:5.0.9.RELEASE]在 org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) 〜[spring-web-5.0.9.RELEASE.jar:5.0.9.RELEASE]在 org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) 〜[tomcat-embed-core-8.5.34.jar:8.5.34]在 org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) 〜[tomcat-embed-core-8.5.34.jar:8.5.34]在 org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:93) 〜[spring-web-5.0.9.RELEASE.jar:5.0.9.RELEASE]在 org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) 〜[spring-web-5.0.9.RELEASE.jar:5.0.9.RELEASE]在 org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) 〜[tomcat-embed-core-8.5.34.jar:8.5.34]在 org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) 〜[tomcat-embed-core-8.5.34.jar:8.5.34]在 org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200) 〜[spring-web-5.0.9.RELEASE.jar:5.0.9.RELEASE]在 org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) 〜[spring-web-5.0.9.RELEASE.jar:5.0.9.RELEASE]在 org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) 〜[tomcat-embed-core-8.5.34.jar:8.5.34]在 org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) 〜[tomcat-embed-core-8.5.34.jar:8.5.34]在 org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198) 〜[tomcat-embed-core-8.5.34.jar:8.5.34]在 org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) [tomcat-embed-core-8.5.34.jar:8.5.34]在 org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:493) [tomcat-embed-core-8.5.34.jar:8.5.34]在 org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140) [tomcat-embed-core-8.5.34.jar:8.5.34]在 org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81) [tomcat-embed-core-8.5.34.jar:8.5.34]在 org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87) [tomcat-embed-core-8.5.34.jar:8.5.34]在 org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342) [tomcat-embed-core-8.5.34.jar:8.5.34]在 org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:800) [tomcat-embed-core-8.5.34.jar:8.5.34]在 org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) [tomcat-embed-core-8.5.34.jar:8.5.34]在 org.apache.coyote.AbstractProtocol $ ConnectionHandler.process(AbstractProtocol.java:806) [tomcat-embed-core-8.5.34.jar:8.5.34]在 org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.doRun(NioEndpoint.java:1498) [tomcat-embed-core-8.5.34.jar:8.5.34]在 org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-8.5.34.jar:8.5.34]在 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [na:1.8.0_181]在 java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:624) [na:1.8.0_181]在 org.apache.tomcat.util.threads.TaskThread $ WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-8.5.34.jar:8.5.34]在 java.lang.Thread.run(Thread.java:748)[na:1.8.0_181]
这是transactionManager的创建。我尝试从sessionFactory获取currentSession