Spring JPA 和persistence.xml

编程入门 行业动态 更新时间:2024-10-22 18:38:49
本文介绍了Spring JPA 和persistence.xml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试设置一个 Spring JPA Hibernate 简单示例 WAR 以部署到 Glassfish.我看到一些示例使用了 persistence.xml 文件,而其他示例则没有.有些示例使用数据源,有些则不使用.到目前为止,我的理解是,如果我有,则不需要数据源:

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:

My 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

本文发布于:2023-10-25 14:06:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1527175.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:JPA   Spring   xml   persistence

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!