Spring JPA and persistence.xml

Posted by bmw0128 on Stack Overflow See other posts from Stack Overflow or by bmw0128
Published on 2009-07-15T16:46:05Z Indexed on 2010/05/23 16:11 UTC
Read the original article Hit count: 432

Filed under:
|
|

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>

I can deploy fine, but my EntityManager is not getting injected by Spring.

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>

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";
}

My web.xml:

  <listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

Are there any pieces I am missing to have Spring inject "em" in to StudentService?

© Stack Overflow or respective owner

Related posts about java

Related posts about spring