2012-01-03 131 views
2

這是事情......我有我的基於spring3.0的Maven web項目,我想爲我的DA使用hibernate + jpa ......但是當我嘗試使用entityManager OBJET我只是得到一個NullPointerException ...這裏是代碼:EntityManager NullPointerException Spring + Hibernate + Maven

@Repository 
@Component 
public class DAOPersonImp implements DAOPerson{ 

@PersistenceContext 
private EntityManager entityManager; 
@Override 
public Person getById(int id) { 
    // TODO Auto-generated method stub 
    return entityManager.find(Person.class, id); 
} 

@Override 
@Transactional 
public List<Person> getAll() { 
    Query query = entityManager.createQuery("SELECT * FROM PERSON"); 
    List<Person> persons = null; 
    persons = query.getResultList(); 
    return persons; 
} 

當我嘗試創建查詢OBJET我得到的錯誤。

這是我的applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?> 

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

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> 
    <property name="dataSource" ref="dataSource" /> 
    <property name="persistenceUnitName" value="PersistUnit" /> 
    <property name="jpaVendorAdapter"> 
     <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> 
      <property name="database" value="MYSQL" /> 
      <property name="showSql" value="true" /> 
     </bean> 
    </property> 
</bean> 

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

而且錯誤!

GRAVE: Servlet.service() for servlet [dispatchermaven] in context with path [/OneX-1] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause 
java.lang.NullPointerException 
    at com.divux.onex.daoImp.DAOPersonImp.getAll(DAOPersonImp.java:45) 
    at com.divux.onex.logic.FriendListLogic.getEntrys(FriendListLogic.java:23) 
    at com.divux.onex.controller.FriendListController.getEntrys(FriendListController.java:25) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:616) 
    at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176) 
    at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:426) 
    at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:414) 
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:790) 
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719) 
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644) 
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:560) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:641) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) 
    at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) 
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240) 
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164) 
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462) 
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164) 
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100) 
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:563) 
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) 
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:399) 
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:317) 
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:204) 
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:182) 
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:311) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603) 
    at java.lang.Thread.run(Thread.java:679) 

不知道多少信息,你對子級需要的,但我將要給你任何你需要的傢伙!

非常感謝!甩掉我的可怕英語...

+0

你有<背景:組件掃描基礎包= 「com.divux」/>在你的applicationContext.xml? – 2012-01-03 21:51:16

+0

我對Hibernate或Spring並不熟悉,但在使用JSF時,@ PersistenceContext是用它的單元名寫的,比如'@PersistenceContext(unitName =「yourUnitName」)'。我認爲這可能是一個問題。 – Lion 2012-01-03 21:52:51

+0

你怎麼稱呼dao.getAll()? – Bozho 2012-01-03 22:00:17

回答

2

PersistenceContex不注入entitymanager。所以你得到NullPointerException使persistence.xml在正確的位置,它的單元名稱是正確的。我還發布了一個示例配置,以便您可以相應地配置applicationContex。

<!-- Prints used SQL to stdout --> 

    <property name="showSql" value="${hibernate.show.sql}"/> 

    <!-- Generates tables. --> 

    <property name="generateDdl" value="${hibernate.generate.ddl}"/> 

    <property name="databasePlatform" value="${postgres.hibernate.database.platform}"/> 

</bean> 



<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" 

     destroy-method="close"> 

    <property name="driverClass" value="${postgres.datasource.driver.class}"/> 

    <property name="jdbcUrl" value="${postgres.datasource.jdbc.url}"/> 

    <property name="user" value="${postgres.datasource.username}"/> 

    <property name="password" value="${postgres.datasourse.password}"/> 

    <property name="minPoolSize" value="1"/> 

    <property name="acquireIncrement" value="1"/> 

    <property name="maxPoolSize" value="3"/> 

</bean> 



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



<bean id="postgres.transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> 

    <property name="entityManagerFactory" ref="entityManagerFactory"/> 

    <property name="dataSource" ref="dataSource"/> 

</bean> 



<bean id="entityManagerFactory" 

     class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> 

    <property name="dataSource" ref="dataSource"/> 

    <property name="jpaVendorAdapter" ref="jpaAdapter"/> 

    <property name="persistenceUnitManager" ref="persistenceUnitManager"/> 

    <property name="persistenceUnitName" value="PersistenceUnit"></property> 

</bean> 
+0

感謝您的信息,是的,它似乎我沒有得到正確的目錄中的persistence.xml ...仍然有問題找到它!但你的貢獻是完全準確的國際海事組織 – BrunoX 2012-01-06 19:34:49

相關問題