在我的网络应用程序中,我在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;
}
}
但是当我通过上面的方式得到它时,我得到null。 当我通过:
@Override
protected EntityManager getEntityManager() {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("CollDocPU");
EntityManager ecm = emf.createEntityManager();
return ecm;
}
ecm不为null,没关系
我的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>
答案 0 :(得分:2)
从
获取非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://mobiarch.wordpress.com/2012/12/07/configuring-a-mysql-data-source-in-tomee/
答案 1 :(得分: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对象中。 负责的类是 LocalContainerEntityManagerFactoryBean ,该类在我的application.xml(用于加载我的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>
答案 2 :(得分:0)
persistence.xml 文件应位于META-INF文件夹下。查看this文档以了解结构。