我正在尝试设置一个Spring JPA Hibernate简单示例WAR,以将其部署到Glassfish. 我看到一些示例使用persistence.xml文件,而其他示例则没有. 有些示例使用数据源,有些则不使用.到目前为止,我的理解是如果有以下情况,则不需要dataSource:
I'm trying to set up a Spring JPA Hibernate simple example WAR for deployment to Glassfish. I see some examples use a persistence.xml file, and other examples do not. Some examples use a dataSource, and some do not. So far my understanding is that a dataSource is not needed if I have:
<persistence-unit name="educationPU" transaction-type="JTA"> <provider>org.hibernate.ejb.HibernatePersistence</provider> <class>com.coe.jpa.StudentProfile</class> <properties> <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" /> <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/COE" /> <property name="hibernate.connection.username" value="root" /> <property name="show_sql" value="true" /> <property name="dialect" value="org.hibernate.dialect.MySQLDialect" /> </properties> </persistence-unit>我可以很好地部署,但是我的EntityManager不会被Spring注入.
I can deploy fine, but my EntityManager is not getting injected by Spring.
我的applicationContext.xml:
My applicationContext.xml:
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean"> <property name="persistenceUnitName" value="educationPU" /> </bean> <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> <property name="entityManagerFactory" ref="entityManagerFactory" /> </bean> <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" /> <tx:annotation-driven transaction-manager="transactionManager" /> <bean id="StudentProfileDAO" class="com.coe.jpa.StudentProfileDAO"> <property name="entityManagerFactory" ref="entityManagerFactory" /> </bean> <bean id="studentService" class="com.coe.services.StudentService"> </bean>我的EntityManager类:
My class with the EntityManager:
public class StudentService { private String saveMessage; private String showModal; private String modalHeader; private StudentProfile studentProfile; private String lastName; private String firstName; @PersistenceContext(unitName="educationPU") private EntityManager em; @Transactional public String save() { System.out.println("*** em: " + this.em); //em is null this.studentProfile= new StudentProfile(); this.saveMessage = "saved"; this.showModal = "true"; this.modalHeader= "Information Saved"; return "successs"; }我的web.xml:
<listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
我缺少Spring将"em"注入到StudentService中的任何东西吗?
Are there any pieces I am missing to have Spring inject "em" in to StudentService?
推荐答案只是为了确认您是否确实已完成...
Just to confirm though you probably did...
您是否包含了
<!-- tell spring to use annotation based congfigurations --> <context:annotation-config /> <!-- tell spring where to find the beans --> <context:component-scan base-package="zz.yy.abcd" />您的应用程序context.xml中的
位?
bits in your application context.xml?
我也不确定您是否可以在这种设置中使用jta事务类型?那不需要数据源托管的连接池吗?因此,请尝试使用RESOURCE_LOCAL.
Also I'm not so sure you'd be able to use a jta transaction type with this kind of setup? Wouldn't that require a data source managed connection pool? So try RESOURCE_LOCAL instead.
更多推荐
Spring JPA和persistence.xml
发布评论