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

  • JPA + Hibernate + Spring None Invasive

    Hello im trying to develop an application using Spring 2.0, Hibernate 3.2, using JPA support. I don't know why but doesn't work for me. The entity manager variable isn't setted. Im using an GenericHibernateDAO approach but with entity manager:

    Code:
    public abstract class GenericHibernateDAO<T, ID extends Serializable> implements GenericDAO<T, ID> {
    
    	private Class<T> persistentClass;
    	private EntityManager entityManager;
    ....
    
    /**
    	 * @return the entityManager
    	 */
    	public EntityManager getEntityManager() {
    		if (this.entityManager == null)
                throw new IllegalStateException("Entity Manager has not been set on DAO before usage");
    		return entityManager;
    	}
    
    	/**
    	 * @param entityManager the entityManager to set
    	 */
    	@PersistenceContext
    	public void setEntityManager(EntityManager entityManager) {
    		this.entityManager = entityManager;
    	}
    
    }
    The IllegalStateException is thrown when I use getEntityManager for retrieving information (for example session) for Criteria Queries.

    My persitance xml file is:

    Code:
    <persistence xmlns="http://java.sun.com/xml/ns/persistence"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
    	version="1.0">
    	<persistence-unit name="archival"
    		transaction-type="RESOURCE_LOCAL">
    		<provider>org.hibernate.ejb.HibernatePersistence</provider>
    		<class>edu.sunami.archival.model.Classroom</class>
    		<class>edu.sunami.archival.model.Photo</class>
    		<class>edu.sunami.archival.model.Teacher</class>
    		<class>edu.sunami.archival.model</class>
    		<properties>
    			<property name="hibernate.hbm2ddl.auto" value="create-drop" />
    			<property name="hibernate.dialect"
    				value="org.hibernate.dialect.PostgreSQLDialect" />
    			<property name="hibernate.show_sql" value="true"/>
    			<property name="hibernate.connection.driver_class" value="org.postgresql.Driver"/>
                <property name="hibernate.connection.url" value="jdbc:postgresql://localhost:5432/educa"/>
                <property name="hibernate.connection.username" value="asoto"/>
                <property name="hibernate.connection.password" value=""/>
    		</properties>
    	</persistence-unit>
    </persistence>
    Here database connection settings are put, but in my previous version i was using commons-dbcp in Spring.

    And finally a part of my Spring file:

    Code:
    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
    		<property name="persistenceUnitName" value="archival"/>
    	</bean>
    
    <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>
    	
    	<bean id="teacherDAO" class="edu.sunami.archival.dao.impl.TeacherHibernateDAO"/>
    In previous version but, a DefaultPersistenceUnitManager approach has been used for specify dbcp pool and location of persitance.xml file.

    I have been watching log output and no error has been thrown.

    It seems like entity manager is not created correctly so not setted. But i just don't know.

    Thank you very much for your help.

  • #2
    Can you turn debug on for the jpa package and post the appropriate logs here?
    Your DAO, if it extends the HibernateDAO should have the property injected. Make sure though you are using the Spring managed bean and not something else.

    Comment


    • #3
      Here i put the log. It seems all be ok.
      Code:
      6[main] DEBUG org.springframework.util.ClassUtils  - Class [org.apache.commons.collections.map.LinkedMap] or one of its dependencies is not present: java.lang.ClassNotFoundException: org.apache.commons.collections.map.LinkedMap
      6[main] DEBUG org.springframework.core.CollectionFactory  - Creating [java.util.LinkedHashMap]
      9[main] DEBUG org.springframework.core.CollectionFactory  - Creating [java.util.LinkedHashSet]
      
      139[main] DEBUG org.springframework.beans.factory.xml.PluggableSchemaResolver  - Loaded schema mappings: {......}
      214[main] DEBUG org.springframework.beans.factory.xml.DefaultDocumentLoader  - Using JAXP provider [com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl]
      325[main] DEBUG org.springframework.beans.factory.xml.ResourceEntityResolver  - Attempting to resolve XML Schema [http://www.springframework.org/schema/beans/spring-beans-2.0.xsd] using [org.springframework.beans.factory.xml.PluggableSchemaResolver]
      695[main] DEBUG org.springframework.beans.factory.xml.DefaultNamespaceHandlerResolver  - Loaded mappings [{....}]
      810[main] DEBUG org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader  - Loading bean definitions
      898[main] DEBUG org.springframework.beans.factory.xml.BeanDefinitionParserDelegate  - Neither XML 'id' nor 'name' specified - using generated bean name [org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor]
      912[main] DEBUG org.springframework.beans.factory.xml.XmlBeanFactory  - Creating shared instance of singleton bean 'application'
      913[main] DEBUG org.springframework.beans.factory.xml.XmlBeanFactory  - Creating instance of bean 'application' with merged definition [Root bean: class [edu.sunami.archival.impl.ArchivalImpl]; scope=singleton; abstract=false; lazyInit=false; autowireCandidate=true; autowireMode=2; dependencyCheck=0; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in class path resource [edu/sunami/applicationBeans.xml]]
      916[main] DEBUG org.springframework.beans.factory.xml.XmlBeanFactory  - Invoking BeanPostProcessors before instantiation of bean 'application'
      1031[main] DEBUG org.springframework.beans.CachedIntrospectionResults  - Getting BeanInfo for class [edu.sunami.archival.impl.ArchivalImpl]
      1039[main] DEBUG org.springframework.beans.CachedIntrospectionResults  - Caching PropertyDescriptors for class [edu.sunami.archival.impl.ArchivalImpl]
      1040[main] DEBUG org.springframework.beans.CachedIntrospectionResults  - Found bean property 'class' of type [java.lang.Class]
      1040[main] DEBUG org.springframework.beans.CachedIntrospectionResults  - Found bean property 'teacherDAO' of type [edu.sunami.archival.dao.TeacherDAO]
      1040[main] DEBUG org.springframework.beans.CachedIntrospectionResults  - Class[edu.sunami.archival.impl.ArchivalImpl] is cache-safe
      1042[main] DEBUG org.springframework.core.CollectionFactory  - Creating [java.util.LinkedHashMap]
      1043 [main] DEBUG org.springframework.beans.factory.xml.XmlBeanFactory  - Eagerly caching bean of type [edu.sunami.archival.impl.ArchivalImpl] with name 'application' to allow for resolving potential circular references
      1075 [main] DEBUG org.springframework.beans.factory.xml.XmlBeanFactory  - Invoking BeanPostProcessors before instantiation of bean 'entityManagerFactory'
      1077 [main] DEBUG org.springframework.beans.CachedIntrospectionResults  - Getting BeanInfo for class [org.springframework.orm.jpa.LocalEntityManagerFactoryBean]
      1113 [main] DEBUG org.springframework.beans.CachedIntrospectionResults  - Caching PropertyDescriptors for class [org.springframework.orm.jpa.LocalEntityManagerFactoryBean]
      1113 [main] DEBUG org.springframework.beans.CachedIntrospectionResults  - Found bean property 'class' of type [java.lang.Class]
      1113 [main] DEBUG org.springframework.beans.CachedIntrospectionResults  - Found bean property 'dataSource' of type [javax.sql.DataSource]
      1114 [main] DEBUG org.springframework.beans.CachedIntrospectionResults  - Found bean property 'entityManagerInterface' of type [java.lang.Class]
      1114 [main] DEBUG org.springframework.beans.CachedIntrospectionResults  - Found bean property 'jpaDialect' of type [org.springframework.orm.jpa.JpaDialect]
      1114 [main] DEBUG org.springframework.beans.CachedIntrospectionResults  - Found bean property 'jpaProperties' of type [java.util.Properties]
      1114 [main] DEBUG org.springframework.beans.CachedIntrospectionResults  - Found bean property 'jpaPropertyMap' of type [java.util.Map]
      1114 [main] DEBUG org.springframework.beans.CachedIntrospectionResults  - Found bean property 'jpaVendorAdapter' of type [org.springframework.orm.jpa.JpaVendorAdapter]
      1114 [main] DEBUG org.springframework.beans.CachedIntrospectionResults  - Found bean property 'nativeEntityManagerFactory' of type [javax.persistence.EntityManagerFactory]
      1114 [main] DEBUG org.springframework.beans.CachedIntrospectionResults  - Found bean property 'object' of type [java.lang.Object]
      1114 [main] DEBUG org.springframework.beans.CachedIntrospectionResults  - Found bean property 'objectType' of type [java.lang.Class]
      1114 [main] DEBUG org.springframework.beans.CachedIntrospectionResults  - Found bean property 'persistenceProvider' of type [javax.persistence.spi.PersistenceProvider]
      1114 [main] DEBUG org.springframework.beans.CachedIntrospectionResults  - Found bean property 'persistenceProviderClass' of type [java.lang.Class]
      1115 [main] DEBUG org.springframework.beans.CachedIntrospectionResults  - Found bean property 'persistenceUnitInfo' of type [javax.persistence.spi.PersistenceUnitInfo]
      1115 [main] DEBUG org.springframework.beans.CachedIntrospectionResults  - Found bean property 'persistenceUnitName' of type [java.lang.String]
      1115 [main] DEBUG org.springframework.beans.CachedIntrospectionResults  - Found bean property 'singleton' of type [boolean]
      1115 [main] DEBUG org.springframework.beans.CachedIntrospectionResults  - Class [org.springframework.orm.jpa.LocalEntityManagerFactoryBean] is cache-safe
      1115 [main] DEBUG org.springframework.core.CollectionFactory  - Creating [java.util.LinkedHashMap]
      1127 [main] DEBUG org.springframework.core.CollectionFactory  - Creating [java.util.LinkedHashMap]
      1128 [main] DEBUG org.springframework.beans.factory.xml.XmlBeanFactory  - Creating shared instance of singleton bean 'teacherDAO'
      1128 [main] DEBUG org.springframework.beans.factory.xml.XmlBeanFactory  - Creating instance of bean 'teacherDAO' with merged definition [Root bean: class [edu.sunami.archival.dao.impl.TeacherHibernateDAO]; scope=singleton; abstract=false; lazyInit=false; autowireCandidate=true; autowireMode=0; dependencyCheck=0; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in class path resource [edu/sunami/applicationBeans.xml]]
      1128 [main] DEBUG org.springframework.beans.factory.xml.XmlBeanFactory  - Invoking BeanPostProcessors before instantiation of bean 'teacherDAO'
      1146 [main] DEBUG org.springframework.beans.CachedIntrospectionResults  - Getting BeanInfo for class [edu.sunami.archival.dao.impl.TeacherHibernateDAO]
      1169 [main] DEBUG org.springframework.beans.CachedIntrospectionResults  - Caching PropertyDescriptors for class [edu.sunami.archival.dao.impl.TeacherHibernateDAO]
      1170 [main] DEBUG org.springframework.beans.CachedIntrospectionResults  - Found bean property 'class' of type [java.lang.Class]
      1170 [main] DEBUG org.springframework.beans.CachedIntrospectionResults  - Found bean property 'entityManager' of type [javax.persistence.EntityManager]
      1170 [main] DEBUG org.springframework.beans.CachedIntrospectionResults  - Found bean property 'persistentClass' of type [java.lang.Class]
      1170 [main] DEBUG org.springframework.beans.CachedIntrospectionResults  - Found bean property 'session' of type [org.hibernate.Session]
      1170 [main] DEBUG org.springframework.beans.CachedIntrospectionResults  - Class [edu.sunami.archival.dao.impl.TeacherHibernateDAO] is cache-safe
      1170 [main] DEBUG org.springframework.core.CollectionFactory  - Creating [java.util.LinkedHashMap]
      1170 [main] DEBUG org.springframework.beans.factory.xml.XmlBeanFactory  - Eagerly caching bean of type [edu.sunami.archival.dao.impl.TeacherHibernateDAO] with name 'teacherDAO' to allow for resolving potential circular references
      1181 [main] DEBUG org.springframework.core.CollectionFactory  - Creating [java.util.LinkedHashMap]
      1181 [main] DEBUG org.springframework.beans.factory.xml.XmlBeanFactory  - Invoking BeanPostProcessors before initialization of bean 'teacherDAO'
      1182 [main] DEBUG org.springframework.beans.factory.xml.XmlBeanFactory  - Invoking BeanPostProcessors after initialization of bean 'teacherDAO'
      1183 [main] DEBUG org.springframework.core.CollectionFactory  - Creating [java.util.LinkedHashSet]
      1183 [main] DEBUG org.springframework.beans.factory.xml.XmlBeanFactory  - Autowiring by type from bean name 'application' via property 'teacherDAO' to bean named 'teacherDAO'
      1190 [main] DEBUG org.springframework.core.CollectionFactory  - Creating [java.util.LinkedHashMap]
      1207 [main] DEBUG org.springframework.beans.BeanWrapperImpl  - About to invoke write method [public void edu.sunami.archival.impl.ArchivalImpl.setTeacherDAO(edu.sunami.archival.dao.TeacherDAO)] on object of class [edu.sunami.archival.impl.ArchivalImpl]
      1208 [main] DEBUG org.springframework.beans.BeanWrapperImpl  - Invoked write method [public void edu.sunami.archival.impl.ArchivalImpl.setTeacherDAO(edu.sunami.archival.dao.TeacherDAO)] with value of type [edu.sunami.archival.dao.TeacherDAO]
      1208 [main] DEBUG org.springframework.beans.factory.xml.XmlBeanFactory  - Invoking BeanPostProcessors before initialization of bean 'application'
      1208 [main] DEBUG org.springframework.beans.factory.xml.XmlBeanFactory  - Invoking BeanPostProcessors after initialization of bean 'application'

      Comment


      • #4
        I don't see the jpa bean post processor being invokes (ofc, I must have gotten lost in the log).
        Make sure that the annotation post processor is not lazy initialized and try removing autowire for the time being as it might create problems with the object passed by to the DAO.

        Comment


        • #5
          i have put PersistanceBeanPostProcessor to lazy-init to false and deleted autowire property.
          Debugging i have seen that setEntityManager is never called.
          Last edited by mag_gandalf; Oct 19th, 2006, 12:25 PM.

          Comment


          • #6
            I have found the problem, now i have another problem (i will see what's happen), but the error was that i have been using beanfactory instead of application context. All right sorry for all headache i have produced. I will write this to my blog :P.

            Comment

            Working...
            X