Announcement Announcement Module
Collapse
No announcement yet.
init-method and @PersistenceContext Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • init-method and @PersistenceContext

    Dear all,

    I use spring 2.0 final with hibernate 3.2 final.
    I declare a bean in the context with init-method="init", this method works with the entityManager :

    Code:
    @PersistenceContext
    private EntityManager entityManager;
    
    public void init()
    {
      // do some stuff with this.entityManager (only select)
    }
    The code above fails :
    javax.persistence.PersistenceException: org.hibernate.SessionException: Session is closed!

    Even if I add a @Transactionnal annotation on the init method.

    I guess (but I may be wrong) that the init method is called during the context initialization before all the entityManager is ready to use.

    If I replace @PersistenceContext by @PersistenceContext(type=PersistenceContextType.EX TENDED ) it works

    But as far as I know, PersistenceContextType.EXTENDED means that the entityManager is managed by the application so I am responsible for closing it. So at the end of my 'init' method I add this line :
    this.entityManager.close();

    But then I get the following error :
    Invalid usage: Cannot close a container-managed EntityManager

    Actually I am wondering if the usage of PersistenceContextType.EXTENDED in my case is nasty hack...

    By the way, I defined entityManager factory and transaction management as follow :
    Code:
    	<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    		<property name="persistenceUnitName" value="hibernateEntityManager"/>
    		<property name="dataSource" ref="dataSource"/>
    		<property name="jpaProperties">
    			<props>
    				<prop key="hibernate.cache.provider_class">${datasource.hibernate.cache.provider_class}</prop>
    				<prop key="hibernate.cache.use_query_cache">${datasource.hibernate.cache.use_query_cache}</prop>
    				<prop key="hibernate.ejb.classcache.example.Category">read-only</prop>
    			</props>
    		</property>
    	</bean>
    
    	<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        	<property name="entityManagerFactory" ref="entityManagerFactory" />
    	</bean>
    	
    	<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
    
    	<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>
    	
    	<tx:annotation-driven />
    Thanks for your opinion about that,

    Gengis
Working...
X