Announcement Announcement Module
No announcement yet.
Spring, JPA + Hibernate on Tomcat. Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring, JPA + Hibernate on Tomcat.

    Using Tomcat 6, I am trying to get started with JPA + Hibernate under Spring.

    Working with an MSSQL Database, I've set up an applicationContext.xml with an entityManagerFactory, pulling a connection from persistence.xml.. So far nothing but problems.

    I have hardcoded the DB connection in persistence.xml, as making a JNDI datasource in tomcats context.xml, keeps kicking back an error telling me that JNDI datasource does not exist..

    Hardcoding the source info in the persistence.xml has moved me on a little, but I have the following :

    from applicationContext.xml

    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
    		<property name="persistenceUnitName" value="testPU" />
    		<property name="jpaVendorAdapter">
    				<property name="database" value="SQL_SERVER" />
    				<property name="generateDdl" value="false" />
    				<property name="showSql" value="true" />
    	<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    		<property name="entityManagerFactory" ref="entityManagerFactory"/>
    	<bean id="ClientDAO"
    		<property name="entityManagerFactory" ref="entityManagerFactory" />
    	<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
    	<bean class=""/>
    And the code in its simplest form:

    package com.test.project.dao;
    import java.util.ArrayList;
    import java.util.List;
    import com.test.project.entities.Client;
    import javax.persistence.EntityManagerFactory;
    import org.springframework.beans.factory.BeanFactory;
    import org.springframework.context.ApplicationContext;
    import org.springframework.dao.DataAccessException;
    import org.springframework.orm.jpa.JpaTemplate;
    import org.springframework.stereotype.Repository;
    import org.springframework.transaction.annotation.Transactional;
    public class clientDAO extends JpaDaoSupport implements IclientDAO {
            public Client findClientByCode(String clientCode) {
                return (Client) getJpaTemplate().findByNamedQuery("Client.findByClientcodeC", clientCode).get(0);
    I am continually getting "NullPointerException" on the first line in findClientByCode. I've broken this line out into segments, and the issue is basically that jpaTemplate is null. calling super.checkDaoConfig(); throws an exception that either the template, or EntityManagerFactory are required..

    But I have that set in the app context.. No?

    I've done a bunch with hibernate and spring, and this is my first venture into JPA..

    Anyone know what I am doing wrong?



  • #2
    from the code you paste,I can not know what is wrong with you! the error tell you JNDI can not exist,I guess the reason it will be occurrence for the server.xml of tomcat or datasource configuration


    • #3
      Thanks for the reply..

      I gave up on the project and started from scratch, as I could no longer see the wood for the trees.. When I went through to salvage what I could, I noticed the issue staring me straight in the face.. The controller handling the request from spring, was creating a new instance of the DAO object.. Not using springs hooked up DAO with the entityManagerFactory reference.. The DAO was originally pure JDBC all hard-coded, and I'd never taken the time to go back and rewire it.. Sadly that meant I was chasing ghost errors for a couple of days...

      ~ Jon