In a training Spring MVC + JPA/Hibernate project, I'm getting the error in question, after I've changed my configuration.
在一个培训的Spring MVC+JPA/Hibernate项目中,在我更改了配置之后,我收到了有问题的错误。
Originally the DAO class was like this:
最初的道班是这样的:
public Course getById(Integer id) throws DAOException {
Course result = null;
try {
EntityManager em = emf.getFactory().createEntityManager();
result = em.find(Course.class, id);
} catch (Exception e) {
logger.error(e.getMessage(), e);
throw new DAOException("Could not get Course By Id", e);
}
return result;
}
I was told to spawn EntityManager with @PersistenceContext
, and avoid creating new EntityManager every now and then. So I replaced the above EntityManager em = ...
line with a @Autowired EntityManager em;
field, and changed the JpaConfig class, so it looks like:
我被告知要用@PersistenceContext生成EntityManager,并且避免时不时地创建新的EntityManager。因此,我替换了上面的EntityManager em=...使用@AuTower EntityManager em;字段,并更改了JpaConfig类,使其如下所示:
@Configuration
@ComponentScan(basePackages = "com.example.school")
@PropertySource("classpath:database.properties")
@EnableTransactionManagement
public class JpaConfig {
private final Environment environment;
@Autowired
public JpaConfig(Environment environment) {
this.environment = environment;
}
@Autowired
DataSource dataSource;
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() throws NamingException {
LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
em.setDataSource(dataSource);
em.setPackagesToScan("com.example.school");
JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
em.setJpaVendorAdapter(vendorAdapter);
return em;
}
@Bean
public PlatformTransactionManager transactionManager(EntityManagerFactory emf) {
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(emf);
return transactionManager;
}
@Bean
public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
return new PersistenceExceptionTranslationPostProcessor();
}
}
While previously the configuration file was that simple:
虽然以前的配置文件很简单:
@Configuration
public class EntityManagerConfig {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("PU");
@Bean
EntityManager em() {
return emf.createEntityManager();
}
}
Now I get a stacktrace with the following most important problem in it:
现在我得到了一个堆栈跟踪,其中包含以下最重要的问题:
org.springframework.beans.factory.UnsatisfiedDependencyException ...
Org.springframework.beans.factory.UnsatisfiedDependencyException..。
...
..。
Caused by: org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'courseDAO' is expected to be of type 'com.example.school.dao.CourseDAO' but was actually of type 'com.sun.proxy.$Proxy49'
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1317)
原因:名为‘CourseDAO’的org.springframework.beans.factory.BeanNotOfRequiredTypeException:Bean的类型应该是‘com.example.School.dao.CourseDAO’,但实际上在org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1317)中属于‘com.sun.prox.$Proxy49’类型
What can be wrong about this configuration?
这种配置可能会有什么错误?
更多回答
Does the courseDAO derive from an interface?
CourseDAO是否派生自接口?
我是一名优秀的程序员,十分优秀!