9

在我的网络应用程序中,我在 Apache Tomcat (TomEE)/7.0.37 服务器上使用 OpenJPA。我使用 Netbeans 自动生成类(“来自数据库的实体类......”和“来自实体类的会话 Bean......”)。在 SessionBean(例如 UserFacade)我想获取 EntityManager:

@Stateless
public class UserFacade extends AbstractFacade<User> {
  @PersistenceContext(unitName = "CollDocPU")
  private EntityManager em;

  @Override
  protected EntityManager getEntityManager() {
    return em;
  }
}

但是当我通过上述方式得到它时,我得到了空值。当我通过:

@Override
protected EntityManager getEntityManager() {
    EntityManagerFactory emf = Persistence.createEntityManagerFactory("CollDocPU");
    EntityManager ecm = emf.createEntityManager(); 
    return ecm;
}    

ecm 不为空,可以

我的persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="CollDocPU" transaction-type="RESOURCE_LOCAL">
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
<class>model.entity.StudentAddSolution</class>
<class>model.entity.Lecturer</class>
<class>model.entity.Solution</class>
<class>model.entity.Student</class>
<class>model.entity.Course</class>
<class>model.entity.File</class>
<class>model.entity.CourseHasLecturer</class> 
<class>model.entity.Mail</class>
<class>model.entity.StudentAtCourse</class>
<class>model.entity.Roles</class>
<class>model.entity.Task</class>
<class>model.entity.User</class>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
   <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:11080/myBase?zeroDateTimeBehavior=convertToNull"/>
   <property name="javax.persistence.jdbc.password" value="pass,"/>
   <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
   <property name="javax.persistence.jdbc.user" value="myBase"/>
   <property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(ForeignKeys=true)"/>
  </properties>
 </persistence-unit>
</persistence>
4

3 回答 3

5

要从中获取不为空的 EntityManager

@PersistenceContext(unitName = "CollDocPU")
private EntityManager em;

我必须更改我的persistance.xml,将事务类型更改为“JTA”并添加:

<jta-data-source>java:openejb/Resource/myDatabase</jta-data-source>
<non-jta-data-source>java:openejb/Resource/myDatabaseUnmanaged</non-jta-data-source>

之后,我必须在我的服务器配置中声明资源:在 [tomee 安装文件夹]/conf/tomee.xml 文件中:

<?xml version="1.0" encoding="UTF-8"?>
 <tomee>
  <Resource id="myDatabase" type="DataSource">
     JdbcDriver com.mysql.jdbc.Driver
     JdbcUrl jdbc:mysql://localhost:11080/jkitaj?zeroDateTimeBehavior=convertToNull
     UserName jkitaj
     Password pass,
  </Resource>

  <Resource id="myDatabaseUnmanaged" type="DataSource">
     JdbcDriver com.mysql.jdbc.Driver
     JdbcUrl jdbc:mysql://localhost:11080/jkitaj?zeroDateTimeBehavior=convertToNull
     UserName jkitaj
     Password pass,
     JtaManaged false
  </Resource>
 </tomee>

看这里:

http://openejb.979440.n4.nabble.com/org-apache-openjpa-lib-jdbc-ReportingSQLException-type-not-found-or-user-lacks-privilege-td4665124.html

http://mobiarch.wordpress.com/2012/12/07/configuring-a-mysql-data-source-in-tomee/

于 2013-09-23T10:03:02.300 回答
1

我遇到了同样的问题,因为我在我的项目中将 Jersey Rest 与 JPA/Hibernate 和 Spring 一起使用,并且每次使用时都将实体管理器设为 null

@PersistenceContext(name = "JPA_DEMO", type = PersistenceContextType.TRANSACTION)
 EntityManager em;

如果我像下面的语法一样手动创建它,它工作正常。

EntityManagerFactory ENTITY_MANAGER_FACTORY = Persistence.createEntityManagerFactory("JPA_DEMO");//
EntityManager em = ENTITY_MANAGER_FACTORY.createEntityManager();

经过一些研究,我发现问题是,虽然我们使用的是负责注入实体管理器对象的@PersistenceContext,但它没有从中获取对象的类,即由Spring 创建和获取实体管理器对象,并将其注入到我们在@PersistenceContext 下定义的EntityManager 对象。负责的类是在我的 application.xml 中定义的LocalContainerEntityManagerFactoryBean (用于加载我的 spring bean)。所以我定义了它并且它起作用了。下面是定义它的语法,它是用于事务管理器的,所以你也可以使用事务。

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

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

    <bean id="MgrFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="packagesToScan" value="com.restDemo"/>
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
    </property>
    <property name="jpaProperties">
        <props>
            <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
            <prop key="hibernate.dialect">${hibernate.dialect}</prop>
            <prop key="hibernate.cache.use_second_level_cache">${hibernate.cache.use_second_level_cache}</prop>
            <prop key="hibernate.cache.use_query_cache">${hibernate.cache.use_query_cache}</prop>
            <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
            <prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
        </props>
    </property>
</bean>

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="${jdbc.driver}"/>
    <property name="url" value="${jdbc.url}"/>
    <property name="username" value="${jdbc.user}"/>
    <property name="password" value="${jdbc.password}"/>
</bean>
于 2018-12-04T11:12:52.623 回答
0

persistence.xml文件应位于 META-INF 文件夹下。检查文档以了解结构。

于 2013-09-17T10:25:27.267 回答