-
Sep 18th, 2011, 04:21 AM
#1
JPA failed to inject entityManager
Hi all,
I tried to use Spring to inject entityManager to a JPA DAO class. But it seems that the entitymanager always returns null.
My applicationContext.xml file is like this:
<?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:jee="http://www.springframework.org/schema/jee"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/aop http://www.springframework.org/schem...ng-aop-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schem...-beans-3.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schem...ng-jee-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schem...ing-tx-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config />
<jee:jndi-lookup id="dataSource" jndi-name="jdbc/videoServer" />
<bean class="org.springframework.orm.jpa.support.Persist enceAnnotationBeanPostProcessor" />
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSes sionFactoryBean">
<property name="mappingResources">
<list>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect"> org.hibernate.dialect.MySQLDialect</prop>
</props>
</property>
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerE ntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="loadTimeWeaver">
<bean
class="org.springframework.instrument.classloading .InstrumentationLoadTimeWeaver" />
</property>
<property name="persistenceUnitName" value="VideoAppPU"/>
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionM anager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<bean id="triggerAdditionalFeatureDao" class="impl.TriggerAdditionalFeatureDaoImpl">
</bean>
</beans>
My DAO file is like this:
public class TriggerAdditionalFeatureDaoImpl implements TriggerAdditionalFeatureDao {
@PersistenceContext
private EntityManager entityManager;
public EntityManager getEntityManager() {
return entityManager;
}
/*
* (non-Javadoc)
*
* @see
* edu.cmu.math.video.dao.TriggerAdditionalFeatureDao #createFeature(java
* .lang.String, java.lang.String, java.lang.String, boolean)
*/
@Override
public boolean createFeature(String schedName, String triggerName, String triggerGroup, boolean feature) {
if (schedName == null || schedName.equals("") || triggerName == null || triggerName.equals("")
|| triggerGroup == null || triggerGroup.equals("")) {
return false;
}
TriggerAdditionalFeaturePK triggerAdditionalFeaturePK = new TriggerAdditionalFeaturePK();
triggerAdditionalFeaturePK.setSchedName(schedName) ;
triggerAdditionalFeaturePK.setTriggerGroup(trigger Group);
triggerAdditionalFeaturePK.setTriggerName(triggerN ame);
TriggerAdditionalFeature triggerAdditionalFeature = new TriggerAdditionalFeature();
triggerAdditionalFeature.setTriggerAdditionalFeatu rePK(triggerAdditionalFeaturePK);
triggerAdditionalFeature.setFeature(feature);
getEntityManager().merge(triggerAdditionalFeature) ;
return true;
}
}
Could anyone here tell me how to solve this problem?
Thanks and best regards
-
Sep 19th, 2011, 03:00 AM
#2
Please use [ code][/code ] tags when posting code
....
Make sure that:
1) you are using an ApplicationContext and not a BeanFactory
2) you are using the instance from the configuration and are not creating a new instance.
3) you aren't configuring the dao twice...
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules