【问题标题】:JPA setup with EntityManager and MySQL database problems带有 EntityManager 和 MySQL 数据库问题的 JPA 设置
【发布时间】:2012-01-30 13:59:14
【问题描述】:

我一直在尝试设置一个服务器来公开一些公共网络服务。当我尝试@Autowire 对象并设置与 MySQL 数据库的连接时,我确实遇到了很多问题。当我修复一个错误时,会出现 10 个新错误,但似乎这个 EntityManager 和 EntityManagerFactory 问题是主要错误。

我尝试在 cloudfoundry 服务器和本地码头服务器上进行部署。在我的本地码头服务器上,我收到此错误:

http://pastie.org/3282244

当我的 xml 文件(pom.xml、servlet-context.xml 和 persistence.xml)看起来像这样时:

http://pastie.org/3282236

我的用户对象实现可以在上面链接的 java 错误下面看到。 (不能在帖子中使用超过 2 个链接)

一般的错误是关于无法自动装配字段,但据我了解,这是由于实体管理器的问题。我对吗? 我可能怀疑 pom.xml 文件有一些版本问题。

非常感谢任何帮助,现在已经过去了很多天而没有任何改善!

乔恩

【问题讨论】:

  • 我们不会在 pastebin 周围追逐链接。将相关的代码和配置粘贴到您的问题中。

标签: mysql spring jpa autowired


【解决方案1】:

看起来实体管理器配置不正确。这个 sn-p 对我有用:

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

【讨论】:

    猜你喜欢
    • 2013-07-25
    • 1970-01-01
    • 1970-01-01
    • 2011-10-09
    • 2014-04-17
    • 1970-01-01
    • 1970-01-01
    • 2017-10-26
    • 1970-01-01
    相关资源
    最近更新 更多