0
我設置了一個應用程序,接受REST調用,然後執行一些數據庫工作。我正在嘗試使用Spring通過註釋來執行所有操作,但我想保留persistence.xml文件。但是,我正在拋出一個錯誤,那就是沒有一個叫做unitName的Bean被添加到@PersistenceContext中。試圖使用Spring與JPA和JTA只有Spring註釋
@Repository
@Transactional
public class UserController implements Serializable {
private static final long serialVersionUID = 4625223295588959802L;
@PersistenceContext(unitName="PerfJPASpring")
private EntityManager em;
@Transactional
public void addUser(User user) {
em.persist(user);
}
@Transactional
public void deleteAll() {
em.createNamedQuery("User.deleteAll").executeUpdate();
}
@Transactional
public List<User> selectAllRecords() {
return em.createNamedQuery("User.findAll").getResultList();
}
}
的另一個問題我是怎麼做的,我得到Spring使用persistence.xml並在LocalContainerEntityManagerFactoryBean沒有定義數據源。我已將數據源添加到App Server。
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackageClasses=UserController.class)
public class DatabaseConfig {
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
EclipseLinkJpaVendorAdapter vendorAdapter = new EclipseLinkJpaVendorAdapter();
vendorAdapter.setGenerateDdl(true);
vendorAdapter.setShowSql(false);
LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
factory.setJpaVendorAdapter(vendorAdapter);
factory.setPackagesToScan("com.example.jpa.model");
factory.setDataSource(dataSource());
Properties jpaProps = new Properties();
jpaProps.put("eclipselink.weaving", "false");
factory.setJpaProperties(jpaProps);
return factory;
}
@Bean
public JpaTransactionManager transactionManager() throws SQLException {
JpaTransactionManager txManager = new JpaTransactionManager();
txManager.setEntityManagerFactory(entityManagerFactory().getObject());
return txManager;
}
public DataSource dataSource() {
try {
Context initCtx = new InitialContext();
return (DataSource) initCtx.lookup("jdbc/userdb");
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("Unable to lookup datasource", e);
}
}
}
unitName與persistence.xml文件中的unitName匹配。
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="PerfJPASpring" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>jdbc/userdb</jta-data-source>
<class>com.example.jpa.model.User</class>
</persistence-unit>
</persistence>
這是我收到的錯誤。
Uncaught init() exception created by servlet [DispatcherServlet] in application [PerfJPASpring]: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userController': Injection of persistence dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'PerfJPASpring' is defined
at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.postProcessPropertyValues(PersistenceAnnotationBeanPostProcessor.java:357)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1219)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:751)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:839)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:538)
at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:668)
at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:540)
at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:494)
at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)
at javax.servlet.GenericServlet.init(GenericServlet.java:244)
at [internal classes]
請指點我在正確的方向爲春天使用unitName和我如何可以避免指定數據源在Db配置?
謝謝。