Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring JPA and persistence.xml

Tags:

People also ask

Does Spring data JPA require persistence xml?

So, actually you can configure JPA in Spring without persistence. xml by writing a custom PersistenceUnitManager , though such a manager is not available out of the box.

What is persistence xml in JPA?

The persistence. xml file is a standard configuration file in JPA. It has to be included in the META-INF directory inside the JAR file that contains the entity beans. The persistence.

Where should persistence xml be located?

If you package the persistence unit as a set of classes in a WAR file, persistence. xml should be located in the WAR file's WEB-INF/classes/META-INF directory.

What is the purpose of the persistence XML file?

The persistence. xml configuration file is used to configure a given JPA Persistence Unit. The Persistence Unit defines all the metadata required to bootstrap an EntityManagerFactory , like entity mappings, data source, and transaction settings, as well as JPA provider configuration properties.


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?