Announcement Announcement Module
Collapse
No announcement yet.
Problem injection EntityManagerFactory Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Problem injection EntityManagerFactory

    spring version used : 3.0.3.RELEASE
    and javax.persistence /persistence-api v1.0

    My application is a Spring MVC apps

    I created a generic class for the common operation Hibernate CRUD action

    There is a pproblem in abstract class AbstractDAO<T> how has a problem to inject the EntityManager while creating the bean.
    I am wondering why ?

    Can someone give me some Help ?

    Code:
    @PersistenceContext
     public void setEntityManagerFactory(EntityManagerFactory emf) {
                 this.jpaTemplate = new JpaTemplate(emf);
     }
    here is the stack trace

    Code:
     Error creating bean with name 'platHome' defined in file [C:\Users\artaud\Documents\workspace-sts-2.3.2.RELEASE\mvc-basi
    c\target\classes\com\myshop\dao\plate\PlatHome.class]: Post-processing failed of bean type [class com.myshop.dao.plate.PlatHome] failed; nested exception is java.lang.IllegalStateException: 
    Specified parameter type [interface javax.persistence.EntityManagerFactory] is incompatible with resource type [javax.persistence.EntityManager]:
    java.lang.IllegalStateException: Specified parameter type [interface javax.persistence.EntityManagerFactory] is incompatible with resource type [javax.persistence.EntityManager]
    	at org.springframework.beans.factory.annotation.InjectionMetadata$InjectedElement.checkResourceType(InjectionMetadata.java:134)
    	at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor$PersistenceElement.<init>(PersistenceAnnotationBeanPostProcessor.java:617)
    	at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.findPersistenceMetadata(PersistenceAnnotationBeanPostProcessor.java:394)
    	at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.postProcessMergedBeanDefinition(PersistenceAnnotationBeanPostProcessor.java:320)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyMergedBeanDefinitionPostProcessors(AbstractAutowireCapableBeanFactory.java:798)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:493)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
    	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291)
    	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190)
    	at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:838)
    	at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:780)
    	at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:697)
    	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:478)
    	at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:84)
    	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:283)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1064)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
    	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291)
    	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)



    My config /META-INF/spring-data.xml



    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"
    	xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop"
    	xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
    		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd
    		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
    		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
    
    
    	<!--  Déclaration du PropertyPlaceholderConfigurer -->
    	<bean id="propertyConfigurer"
    		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    		<property name="locations">
    			<list>
    				<value>classpath*:db.properties</value>
    			</list>
    		</property>
    	</bean>
    	
    	<!-- traduction des exceptions -->
    	<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
     
    
    	<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
    
    	<bean id="entityManagerFactory"
    		class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    		<property name="dataSource" ref="dataSource" />
    		<property name="jpaVendorAdapter">
    			<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
    				<property name="database" value="MYSQL" />
    				<property name="showSql" value="true" />
    			</bean>
    		</property>
    	</bean>
    
    	<bean id="dataSource"
    		class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    		<property name="driverClassName" value="com.mysql.jdbc.Driver" />
    		<property name="url" value="jdbc:mysql://localhost/restaurant" />
    		<property name="username" value="root" />
    		<property name="password" value="" />
    	</bean>
    
    	<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    		<property name="entityManagerFactory" ref="entityManagerFactory" />
    	</bean>
    
    	<!--
    		Indique a Spring que les transactions sont geree par @Transactional
    	-->
    	<tx:annotation-driven transaction-manager="transactionManager" />
    
    	<!--
    		Indique a Spring que la configuration des beans se fait par
    		annotations
    	-->
    	<context:annotation-config />
    	<context:component-scan base-package="com.myshop" />
    
    
    
    </beans>

    Code:
    
    package com.myshop.dao.plate;
    
    import java.util.Collection;
    
    import javax.persistence.EntityManagerFactory;
    import javax.persistence.PersistenceContext;
    import javax.persistence.PersistenceContextType;
    import javax.persistence.PersistenceUnit;
    
    import org.springframework.orm.jpa.JpaTemplate;
    
    import com.myshop.domain.plate.Plat;
    
    public abstract class AbstractDAO<T>  {
            
            private Class<T> entityClass;
            
            protected JpaTemplate jpaTemplate;
    
            protected Class<T> getEntityClass() {
                    return entityClass;
            }
            
            @PersistenceContext
            public void setEntityManagerFactory(EntityManagerFactory emf) {
                    this.jpaTemplate = new JpaTemplate(emf);
            }
    
            @SuppressWarnings("unchecked")
            public AbstractDAO(Class<T> entityClass) {
                    this.entityClass = entityClass;
            }
            
         
    
            @SuppressWarnings("unchecked")
            public Collection<T> getAll() {
                    return jpaTemplate.find("from " + entityClass.getSimpleName());
            }
    
            public void persist(T t) {
        		jpaTemplate.persist(t);
        	}
    }
    META-INF/persistence.xml
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence">
        <persistence-unit name="jpaTutos" transaction-type="RESOURCE_LOCAL">
            <provider>org.hibernate.ejb.HibernatePersistence</provider>
        </persistence-unit>
        
    </persistence>
Working...
X