だから私はSpring JPAのテストでこの問題を抱えています。これが私のコードです:
ArticleService
:
@Service("articleService")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public class ArticleServiceImpl implements ArticleService {
private ArticleDao articleDao;
@Autowired
public ArticleServiceImpl(ArticleDao articleDao) {
this.articleDao = articleDao;
}
public ArticleServiceImpl() {
}
@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
public void addArticle(Article article) {
articleDao.saveArticle(article);
}
}
私のArticleDao
:
@Repository("articleDao")
public class ArticleDaoImpl implements ArticleDao {
private EntityManager em;
@PersistenceContext
public void setEntityManager(EntityManager em) {
this.em = em;
}
// To Save the article detail
public void saveArticle(Article article) {
article.setAddedDate(new Date());
em.persist(article);
}
}
InputDatabaseServiceImpl
問題は、クラスでこれらのメソッドを実行することにあります。
public class InputDatabaseServiceImpl implements InputDatabaseService {
public ArticleService articleService;
public InputDatabaseServiceImpl(ArticleService articleService){
this.articleService= articleService;
}
public int inputArticle(Article article) {
articleService.saveArticle(article);
return 0;
}
アプリケーションappContext.xml
<context:property-placeholder location="/WEB-INF/database.properties" />
<tx:annotation-driven transaction-manager="transactionManager" />
<!-- Declare a datasource that has pooling capabilities-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close"
p:driverClass="${database.driver}"
p:jdbcUrl="${database.url}"
p:user="${database.user}"
p:password="${database.password}"
p:acquireIncrement="5"
p:idleConnectionTestPeriod="60"
p:maxPoolSize="100"
p:maxStatements="50"
p:minPoolSize="10" />
<!-- Declare a JPA entityManagerFactory-->
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" >
<property name="persistenceXmlLocation" value="classpath*:META-INF/persistence.xml"></property>
<property name="persistenceUnitName" value="hibernatePersistenceUnit" />
<property name="dataSource" ref="dataSource"/>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" >
<property name="showSql" value="true"/>
</bean>
</property>
</bean>
<!-- Declare a transaction manager-->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
</beans>
inputArticle
クラスに電話するたびendpoint
にNullPointerExpcetion
、列に並びましたarticleService.saveArticle(article);
私はそれが解決するのが最も簡単な例外であることを知っていましたが、私はしばらくの間これに苦労しており、助けが必要です..誰かが私に何が欠けているのかヒントを与えることができますか?