Note that there are some explanatory texts on larger screens.

plurals
  1. POSpring JPA and persistence.xml
    primarykey
    data
    text
    <p>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:</p> <pre><code>&lt;persistence-unit name="educationPU" transaction-type="JTA"&gt; &lt;provider&gt;org.hibernate.ejb.HibernatePersistence&lt;/provider&gt; &lt;class&gt;com.coe.jpa.StudentProfile&lt;/class&gt; &lt;properties&gt; &lt;property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" /&gt; &lt;property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/COE" /&gt; &lt;property name="hibernate.connection.username" value="root" /&gt; &lt;property name="show_sql" value="true" /&gt; &lt;property name="dialect" value="org.hibernate.dialect.MySQLDialect" /&gt; &lt;/properties&gt; &lt;/persistence-unit&gt; </code></pre> <p>I can deploy fine, but my EntityManager is not getting injected by Spring.</p> <p>My applicationContext.xml:</p> <pre><code>&lt;bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean"&gt; &lt;property name="persistenceUnitName" value="educationPU" /&gt; &lt;/bean&gt; &lt;bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"&gt; &lt;property name="entityManagerFactory" ref="entityManagerFactory" /&gt; &lt;/bean&gt; &lt;bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" /&gt; &lt;tx:annotation-driven transaction-manager="transactionManager" /&gt; &lt;bean id="StudentProfileDAO" class="com.coe.jpa.StudentProfileDAO"&gt; &lt;property name="entityManagerFactory" ref="entityManagerFactory" /&gt; &lt;/bean&gt; &lt;bean id="studentService" class="com.coe.services.StudentService"&gt; &lt;/bean&gt; </code></pre> <p>My class with the EntityManager:</p> <pre><code>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"; } </code></pre> <p>My web.xml:</p> <pre><code> &lt;listener&gt; &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt; </code></pre> <p></p> <p>Are there any pieces I am missing to have Spring inject "em" in to StudentService?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload