2013-01-17 45 views
0

我的目标是实例化applicationContext.xml文件中的EntityManagerFactory以获取在SQL数据库中注册的所有帖子。 这里的主要文件的内容:persistence.xml中当我使用EntityManagerFactory时发生了NullPointerException

<?xml version="1.0" encoding="UTF-8"?> 
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"> 
    <persistence-unit name="post-unit" transaction-type="RESOURCE_LOCAL"> 
     <provider>org.hibernate.ejb.HibernatePersistence</provider> 
     <class>com.zone42.model.Post</class> 
     <exclude-unlisted-classes>true</exclude-unlisted-classes> 
    </persistence-unit> 
</persistence> 

的applicationContext.xml

<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
     http://www.springframework.org/schema/tx 
     http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 
     http://www.springframework.org/schema/context 
     http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 

    <!-- Properties files linkers --> 

    <context:property-placeholder location="/WEB-INF/resources/database/jdbc.properties"/> 
    <context:property-placeholder location="/WEB-INF/resources/database/hibernate.properties"/> 

    <!-- Config database - initialization --> 

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
     <property name="driverClassName" value="${jdbc.driverClassName}" /> 
     <property name="url" value="${jdbc.url}" /> 
     <property name="username" value="${jdbc.username}" /> 
     <property name="password" value="${jdbc.password}" /> 
    </bean> 

    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> 
     <property name="dataSource" ref="dataSource" /> 
    </bean> 

    <!-- Three main layers definition --> 

    <context:annotation-config /> 
    <context:component-scan base-package="com.zone42"/> 

    <!-- Transaction sub-system initialization --> 

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> 
     <property name="entityManagerFactory" ref="entityManagerFactory"/> 
    </bean> 

    <tx:annotation-driven transaction-manager="transactionManager" /> 

</beans> 

(WEB-INF /班/)PostDAO.java

public class PostDAO extends GenericDAOEntity<Post> implements IPostDAO 
{ 

} 

GenericDAOEntity.java

@Transactional 
public class GenericDAOEntity<T> implements IGenericDAO<T> 
{ 
    /** 
    * Properties 
    */ 

    @Autowired 
    @PersistenceContext(unitName="post-unit") 
    private EntityManagerFactory entityManagerFactory/* = Persistence.createEntityManagerFactory(persistence_unit_name)*/; 

    //Get all posts 

    @SuppressWarnings("unchecked") 
    public List<T> findAll(Class<T> obj) { 
     EntityManager entityManager = this.entityManagerFactory.createEntityManager(); 
     Query query = entityManager.createQuery("from " + obj.getSimpleName()); 
     return (query.getResultList()); 
    } 

    /** 
    * Accessors 
    */ 

    public EntityManagerFactory getEntityManagerFactory() { 
     return entityManagerFactory; 
    } 

    @PersistenceContext(unitName="post-unit") 
    public void setEntityManagerFactory(EntityManagerFactory entityManagerFactory) { 
     this.entityManagerFactory = entityManagerFactory; 
    } 
} 

我尝试了几个配置组合,但没有成功。 NullPointerException来自findAll方法,当我想从entityfactory实例创建一个EntityManager实例。我想我有一个配置问题。我想确切地说,我直接在类中使用operator new实例化EntityManagerFactory时,代码运行正常。现在我只想以另一种方式分配我的工厂,即使用appicationContext.xml文件中的xml。谁能帮我?提前致谢。

+0

你如何获得'GenericDAOEntity'的实例? –

回答

0

您需要将其标记字段/ setter方法为@Autowired或显式接线参考在你的XML:

<bean class="PostDAO"> 
    <property name="entityManagerFactory" ref="entityManagerFactory"/> 
</bean> 
+0

感谢您的回答,但注释不应出现在EntityManagerFactory的声明之上。我忘记擦除这个声明。你有其他想法吗? – user1364743

+0

上下文中是否有'PostDao':组件扫描软件包? –

相关问题