*
The moose likes Spring and the fly likes Spring injection issue while using iBatis DAO Object in two classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Spring injection issue while using iBatis DAO Object in two classes" Watch "Spring injection issue while using iBatis DAO Object in two classes" New topic
Author

Spring injection issue while using iBatis DAO Object in two classes

prince davies
Ranch Hand

Joined: May 08, 2009
Posts: 74
Injecting SqlMapClient to one webservice implementation pojo(A) class and a normal pojo(A) classes using SPRING.

When I do only for class A,I am able to get ADao object instantiated and able to get connected todatabase and get database transaction done and returning values.

But when I do(injecting) for two classes A ,B .It gets instantiated ,but it does not return any value.It throws NullPointerException at m_aDao.saveAndReturnId(); Where I am using object of ADao to run sql queries to get some values using SqlMapClient of iBatis.



Class A is Webservice implementation classs(pojo) as given below.

Class A{
private ADao m_aDao;
public void createAssessmentRequest(String userId, Holder<AssessmentRequest> assessmentRequest){
B b = new B();
b.assembleAndSave(assessmentRequest);
}
public ADao getADao() {return m_aDao;}
public void setADao(ADao aDao) {m_aDao = aDao;}
}


Class B is service helper classs(pojo)

Class B{
private ADao m_aDao;
public void assembleAndSave{
m_aDao.saveAndReturnId();
}
public ADao getADao() {return m_aDao;}
public void setADao(ADao aDao) {m_aDao = aDao;}
}



public class ADaoSqlmap extends AbstractSqlMapDao implements ADao{
public long saveAndReturnId() {
Long Id =-1L;
Map AMap = new HashMap();
AMap .put( "createuserId", userId );
AMap .put( "lstUpdtduserId", userId );
Id = (Long) insert( STAT_ID_INSERT_AND_GET_ID,AMap );
return Id.longValue() ;
}
}


Spring.xml

<bean id="JndiSettings" class="org.springframework.jndi.JndiTemplate">
<property name="environment">
<props>
<prop key="java.naming.factory.initial">weblogic.jndi.WL InitialContextFactory</prop>
</props>
</property>
</bean>

<bean id="dataSource" class="org.springframework.aop.framework.ProxyFact oryBean">
<property name="proxyInterfaces"><value>javax.sql.DataSource </value></property>
<property name="targetSource">
<bean class="org.springframework.jndi.JndiObjectTargetSo urce">
<property name="jndiName"><value>${connections/dataSources/mgmtSource/jndiName}</value></property>
<property name="jndiTemplate" ref="JndiSettings"/>
<property name="lookupOnStartup" value="false"/>
</bean>
</property>
</bean>

<bean id="aSqlMap" class="org.springframework.orm.ibatis.SqlMapClient FactoryBean">
<property name="configLocation">
<value>classpath:sqlmaps/assessment-sqlmap-config.xml</value>
</property>
<property name="dataSource"><ref local="dataSource"/></property>
</bean>

<bean id="ADaoImpl" class="com.telus.credit.assessment.dao.ADaoSqlmap" >
<property name="sqlMapClient"><ref local="aSqlMap"/></property>
</bean>

<bean id="txManager" class="org.springframework.transaction.jta.WebLogi cJtaTransactionManager" />

<bean id="service" lazy-init="true" class="org.springframework.transaction.interceptor .TransactionProxyFactoryBean">
<property name="transactionManager"><ref local="txManager"/></property>
<property name="target"><ref local="aTx"/></property>
<property name="transactionAttributes">
<props>
<prop key="*">PROPAGATION_REQUIRES_NEW,-Exception</prop>
</props>
</property>
</bean>

<!--Ends adding transaction manager -->
<bean id="aTx" lazy-init="true" class="A">
property name="aDao">
<ref local="ADaoImpl"/>
</property>
</bean>

<bean id="b" lazy-init="true" class="B">
<ref local="ADaoImpl"/>
</property>
</bean>


Why it is throwing NullPointerException at m_aDao.saveAndReturnId?

Is there any problems in injecting in spring file? is there any problem in using same object for both A and B classes?

Please help me to resolve this issue.

Thanks.


How do I have access to same DAO Object in two classes and at the same I should be able to instantiate one class in another class and should not throw NullPointerException when accessing a method in that DAO Object.This DAO Object is implemented using iBatis.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

You must have made a mistake by copy/pasting your code :


Where is the property opening tag ?


And please UseCodeTags.


[My Blog]
All roads lead to JavaRanch
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Spring injection issue while using iBatis DAO Object in two classes
 
Similar Threads
[Security:090398]Invalid Subject: weblogic :java.lang.SecurityException: [nested exception is java.l
Spring - WebService-iBatis SqlMapClient
Spring iBatis NullPointerException when accesing SQLMAP implemented class
login form with database and spring security
NPE at getHibernateTemplate( )