java - Spring JPA无法通过EntityDao

标签 java spring hibernate jpa

所以我在测试 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类时,我都会在articleService.saveArticle(article);行中得到NullPointerExpcetion >

我知道这是最容易解决的异常,但我已经为此苦苦挣扎了一段时间,我需要帮助..任何人都可以给我一个提示,我错过了什么?

最佳答案

发生的情况是 ArticleService 属性未初始化。

这可能是由于 InputDatabaseServiceImpl bean 配置不匹配,或者注入(inject)依赖项的注释不匹配(如果您的 bean 是 Autowiring 的)。

试试这个:

public class InputDatabaseServiceImpl implements InputDatabaseService {


   @Autowired
   public ArticleService articleService;

   public InputDatabaseServiceImpl(){
      //no need of arguments constructor
   }

   public int inputArticle(Article article) {
      articleService.saveArticle(article);
      return 0;
   }

}

要 Autowiring bean,该 bean 必须提供一个公共(public)默认构造函数,而您的 ArticleServiceImpl 类没有该构造函数。修复形式将重构 ArticleServiceImpl:

@Service("articleService")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public class ArticleServiceImpl implements ArticleService {

  @Autowired
  private ArticleDao articleDao;


  public ArticleServiceImpl() {
     //default constructor required for @Autowired
  }

  public ArticleServiceImpl() {
  }

  @Transactional(propagation = Propagation.REQUIRED, readOnly = false)
  public void addArticle(Article article) {
    articleDao.saveArticle(article);
  }
}

关于java - Spring JPA无法通过EntityDao,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13723270/

相关文章:

java - MouseListener 的 JLabel 和 JButton 之间需要建议

java - Liferay Maven 服务构建器失败

java - 重复的类 org.apache.commons

java - AppEngine Memcache 过期策略

sql - JPA 映射的父实体返回与子实体相同数量的多次

java - 我使用 RestTemplate 时不断收到 "Unsupported Media Type"错误

java - Spring 3 : Network Client-Server with custom protocol for runtime service generation

mysql - 使用 GenerationType.Table 时如何集群 Hibernate ORM 标识符

java - Hibernate 嵌套 JoinColumns 会导致从数据库中进行大量查询,并包含不必要的数据

java - Glassfish MySQL Hibernate 无法构建 Hibernate SessionFactory