使用Spring 3 MVC在DAO中由Query对象返回的NullPointerException

时间:2011-12-05 09:56:28

标签: java hibernate spring jpa dao

我是春天的新人,我的日食中有3个项目。一个JPA.jar,client.jar和一个Web.war。 我将在Tomcat 6上部署这些,因此所有依赖项主要在Web.war上。 我的问题是我可以在oracle数据库中保存数据,但是我无法从DAO中的Query对象中获取(选择)数据:(。

这是我的servlet-context                            

</beans:bean>
                        <!-- Oracle Driver -->
<beans:bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <beans:property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
    <beans:property name="url" value="jdbc:oracle:thin:@//sapodev.sapo.co.za:1523/P3P"/>
    <beans:property name="username" value="pensions"/>
    <beans:property name="password" value="pensions"/>

</beans:bean>

<!-- JPA EntityManagerFactory -->           
<beans:bean id="entityManagerFactory" 
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">  

    <beans:property name="persistenceUnitName" value="frameworkPU"/>  
    <beans:property name="dataSource" ref="dataSource"/>  
    <beans:property name="jpaVendorAdapter">  
        <beans:bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">  
            <beans:property name="showSql" value="true"/>  
            <!-- <property name="generateDdl" value="false"/>  --> 
            <beans:property name="databasePlatform" value="org.hibernate.dialect.Oracle10gDialect"/> 
        </beans:bean>  
    </beans:property>  
</beans:bean> 

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

<beans:bean id="persistenceAnnotation"    class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />  
<beans:bean id="contactService" class="za.co.sapo.service.ContactServiceImpl"/>
    <context:component-scan base-package="za.co.sapo.controller" />
    <context:annotation-config /> 
    <context:component-scan base-package="za.co.sapo.dao"/>
</beans:beans>

这是DAO类

public List<DevUserTab> findUsers() {
    List<DevUserTab> list = null;

    log.info("DD");
    try{
        TypedQuery<DevUserTab> query = this.entityManagerFactory.createNamedQuery("SELECT d from DevUserTab d", DevUserTab.class);
        list= query.getResultList();
    } catch(Exception e){
        log.info(" EE");
        e.printStackTrace();
    }
    return list;
}

这是服务类

@Autowired
public ContactDAO contactDAO;
public List findUsers() {
    // TODO Auto-generated method stub
    ContactDAO contact = new ContactDAO();
    List users = contact.findUsers();
    return users;
}

我的控制器

@RequestMapping(value = "/table")
public ModelAndView getContacts(Locale locale, Model model) {
    logger.info("Getting the Contacts for us "+ locale.toString());

    ModelAndView mv = new ModelAndView("/table");
    try{
        List<Contact> objects = ServiceLayerContext.getContext().findContacts();
        mv.addObject("objects", objects );
    } catch(Exception e){
        e.printStackTrace();
    }
    return mv;
}

1 个答案:

答案 0 :(得分:0)

createNamedQuery希望您引用在persistence.xml中配置的查询

当您正在直接指定JPQL时,需要使用createQuery方法。