gpt4 book ai didi

java - 为什么在 Tomcat8 环境中不通过 @PersistenceContext 注入(inject) EntityManager?

转载 作者:行者123 更新时间:2023-11-30 08:49:15 24 4
gpt4 key购买 nike

我有一些服务可以将对象持久化到上下文并在运行时面对空的 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 网络容器上运行这个项目。

最佳答案

Tomcat 默认不支持 EJB,我建议你要么 add the OpenEJB webapp to Tomcat ,或使用像 GlassFish 这样的全栈应用服务器或 WebLogic Server .

关于java - 为什么在 Tomcat8 环境中不通过 @PersistenceContext 注入(inject) EntityManager?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31517291/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com