2013-01-17 16 views
0

내 목표는 applicationContext.xml 파일에서 EntityManagerFactory를 인스턴스화하여 SQL 데이터베이스에 등록 된 모든 게시물을 가져 오는 것입니다. EntityManagerFactory를 사용할 때 NullPointerException이 발생했습니다.

public class PostDAO extends GenericDAOEntity<Post> implements IPostDAO 
{ 

} 
PostDAO.java

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가/클래스 /)

<?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> 

의 persistence.xml : 다음은 주요 파일의 내용입니다

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은 엔티티 팩토리 인스턴스에서 EntityManager의 인스턴스를 만들 때 findAll 메서드에서 가져온 것입니다. 구성 문제가 있다고 생각합니다. 정확한 코드가 작동하려면 EntityManagerFactory를 클래스에서 직접 new 연산자를 사용하여 인스턴스화해야합니다. 이제는 appicationContext.xml 파일에서 xml을 사용하는 다른 방법을 선택하여 내 공장을 할당하려고합니다. 누구든지 나를 도울 수 있습니까? 미리 감사드립니다.

+0

를? –

답변

0
당신은 당신의 XML에 대한 참조를 묶을 명시 적으로 @Autowired로 필드/세터를 표시하거나 하나 필요

: 당신은 어떻게`GenericDAOEntity`의 인스턴스를 얻을 수 있습니까

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

답해 주셔서 감사합니다.하지만 주석은 EntityManagerFactory 선언 위에 나타나서는 안됩니다. 나는이 선언을 지우는 것을 잊었다. 다른 아이디어가 있습니까? – user1364743

+0

'PostDao'는 컨텍스트에서 : component-scan package? –

관련 문제