为什么EntityManager不是通过Tomcat8环境中的@PersistenceContext注入的?

时间:2015-07-20 13:02:21

标签: java tomcat servlets jpa

我有一些服务可以将对象持久化到上下文,并在运行时面对空EntityManager

@Stateless
public class PointServies {

@PersistenceContext(name="pointProvider",type = PersistenceContextType.EXTENDED)
private EntityManager em;

public void addMovie(Point point) throws Exception {
    em.persist(point); // em is null here.
}
}

这是我的persistence.xml

<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence

http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"

version="2.1">

<persistence-unit name="pointProvider">

    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>

    <class>model.Point</class>

    <properties>
        <!-- Configuring JDBC properties -->
        <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost/testdb" />
        <property name="javax.persistence.jdbc.user" value="root" />
        <property name="javax.persistence.jdbc.password" value="admin" />
        <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />

        <!-- Hibernate properties -->
        <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
        <property name="hibernate.hbm2ddl.auto" value="update" />

        <property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(ForeignKeys=true)"/>
    </properties>
</persistence-unit>

看起来PersistenceContext无法找到我的xml ..... persistence.xml存在于“src / META-INF / persistence.xml”中 所有其他来源都在“src / model / Point”

我尝试在Tomcat 8 web容器上运行这个项目。

1 个答案:

答案 0 :(得分:2)

默认情况下,Tomcat不支持EJB,我重新命令add the OpenEJB webapp to Tomcat,或使用像GlassFishWebLogic Server这样的全栈应用服务器。