我正在尝试设置一个Spring JPA Hibernate简单示例WAR来部署到Glassfish.我看到一些示例使用persistence.xml文件,而其他示例则没有.一些示例使用dataSource,而另一些示例则不使用.到目前为止,我的理解是,如果我有以下情况,则不需要dataSource:
<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>
Run Code Online (Sandbox Code Playgroud)
我可以部署好,但是我的EntityManager没有被Spring注入.
我的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>
Run Code Online (Sandbox Code Playgroud)
我的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";
}
Run Code Online (Sandbox Code Playgroud)
我的web.xml:
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
Run Code Online (Sandbox Code Playgroud)
是否有任何我错过的部分让Spring注入"em"到StudentService?
Mic*_*les 13
只是为了确认你可能做了......
你有没有包括
<!-- 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" />
Run Code Online (Sandbox Code Playgroud)
应用程序context.xml中的位?
另外我不太确定你能用这种设置的jta交易类型吗?这不需要数据源托管连接池吗?所以请尝试使用RESOURCE_LOCAL.
归档时间: |
|
查看次数: |
115895 次 |
最近记录: |