2012-01-30 74 views
0

我一直試圖建立一個服務器,將公開的一些公共Web服務。當我嘗試使用@Autowire對象並建立與MySQL數據庫的連接時,確實遇到了很多問題。當我修復一個錯誤時,會出現10個新錯誤,但是看起來像這個EntityManager和EntityManagerFactory問題是主要錯誤。JPA設置與EntityManager和MySQL數據庫問題

我試過在cloudfoundry服務器和本地碼頭服務器上部署。在我的本地碼頭的服務器我得到這個錯誤:

http://pastie.org/3282244

,當我的XML文件(pom.xml中,servlet的context.xml中和persistence.xml中)看起來是這樣的:

http://pastie.org/3282236

我的用戶對象實現可以在與上面鏈接的java錯誤下面看到。 (不能在一篇文章中使用超過2個鏈接)

一般的錯誤是關於不能自動裝載字段,但正如我已經理解的那樣,這是因爲實體管理員的問題。我對嗎? 我可能會懷疑pom.xml文件中存在一些版本問題。

任何幫助非常感謝,到現在的很多天的方式沒有任何改進!

Jon

+1

我們不會追逐周圍引擎收錄鏈接。將相關的代碼和配置位粘貼到您的問題中。 – skaffman 2012-01-30 15:43:14

回答

0

看起來像實體管理器沒有配置正確。這段代碼爲我工作:

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

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

<!-- replace this with your data source --> 
<jee:jndi-lookup id="dataSource" jndi-name="java:comp/env/jdbc/project" /> 

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

    <!-- this is important if you want to 
     connect JPA and JdbcTemplate transaction control --> 
    <property name="jpaVendorAdapter"> 
     <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> 
      <property name="showSql" value="false" /> 
     </bean> 
    </property> 
</bean> 


<!-- jdbc templates that are equal for all databases -- may not needed by you --> 
<bean class="org.springframework.jdbc.core.JdbcTemplate" id="jdbcTemplate"> 
    <constructor-arg ref="dataSource" /> 
</bean> 
<bean class="org.springframework.jdbc.core.simple.SimpleJdbcTemplate" id="simpleJdbcTemplate"> 
    <constructor-arg ref="dataSource" /> 
</bean> 

然後你可以使用(如果你使用Spring Autowireing)

@PersistenceContext 
private EntityManager entityManager;