使用@PersistenceContext获取Null EntityManager

时间:2018-04-11 17:31:53

标签: spring hibernate

             

<bean id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"
            p:database="MYSQL" p:databasePlatform="org.hibernate.dialect.MySQL5Dialect"
            p:showSql="true" />
    </property>
    <property name="persistenceXmlLocation" value="classpath*:META-INF/persistence.xml" />
    <property name="persistenceUnitName" value="streamJpaDB" />
</bean>

持久单位

http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"     版本=&#34; 1.0&#34;&GT;

<persistence-unit name="streamJpaDB" transaction-type="RESOURCE_LOCAL">
    <non-jta-data-source>jdbc.streamDataSource</non-jta-data-source>
    <properties>
        <property name="hibernate.show-sql" value="true" />

    </properties>
</persistence-unit>

1 个答案:

答案 0 :(得分:0)

  • 您的非jta-data-source应该具有资源的确切名称,即jdbc / streamDataSource
  • 如果您的容器是tomcat,则应添加
         <ResourceLink name="jdbc/streamDataSource" global="jdbc/streamDataSource" type="javax.sql.DataSource"/> to con / context.xml