2013-01-17 97 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':組件掃描軟件包? –

相關問題