Spring Hibernate服务层中的DAO NullPointerException

时间:2015-10-28 19:52:46

标签: java spring hibernate

我使用Spring和Hibernate的组合相对较新,并且有一个我似乎无法弄清楚的问题。在服务内部有许多DAO - 每个都在我的applicationContext.xml文件中定义。除了其中一个DAO之外的所有DAO都被实例化,但其中一个是null,我无法弄清楚为什么只有那个为null。一个DAO为null是RelationshipDAO。这是我的代码......

服务层:

public class LocalReadServiceImpl extends implements LocalReadService {

private CallDao callDao;
private CallSectionDao callSectionDao;
private CaseClassDao caseClassDao;
private ReferralDao referralDao;
private RelationshipDao relationshipDao;

public Relationship retRelationshipByCode(String relationshipCode) {
    Relationship relationship =(Relationship)relationshipDao.findRelationshipByCode(relationshipCode);
    return relationship;
}

DAO:

public class RelationshipDao extends HibernateDaoSupport{

public RelationshipDao() {
    super(Relationship.class);
}

public Relationship findRelationshipByCode(String relationshipCode) {
    Criteria criteria = getSession().createCriteria(Relationship.class);
    criteria.add(Restrictions.eq("relationshipCode",relationshipCode)); 
    Relationship relationship = (Relationship)criteria.uniqueResult();
    return relationship;
}
}

的applicationContext.xml

<bean id="localReadService" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
    parent="txProxyTemplate">
    <property name="target">
        <bean class="redmane.hmsCallcenter.service.impl.LocalReadServiceImpl">
            <property name="callDao" ref="callDao" />
            <property name="callSectionDao" ref="callSectionDao" /> 
            <property name="caseClassDao" ref="caseClassDao" />     
            <property name="referralDao" ref="referralDao" />       
            <property name="relationshipDao" ref="relationshipDao" />   
        </bean>
    </property>
</bean>

<bean id="baseDaoTemplate" abstract="true">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

<bean id="callDao" class="redmane.hmsCallcenter.dao.CallDao"
    parent="baseDaoTemplate" />
<bean id="callSectionDao"
    class="redmane.hmsCallcenter.dao.CallSectionDao"
    parent="baseDaoTemplate" />     
<bean id="caseClassDao"
    class="redmane.hmsCallcenter.dao.CaseClassDao"
    parent="baseDaoTemplate" /> 
<bean id="referralDao" class="redmane.hmsCallcenter.dao.ReferralDao"
    parent="baseDaoTemplate" />     
<bean id="relationshipDao"
    class="redmane.hmsCallcenter.dao.RelationshipDao"
    parent="baseDaoTemplate" /> 

我不知道为什么所有其他DAO都会被实例化,但是一个RelationshipDAO不会。

非常感谢任何见解。如果需要更多细节,请告诉我。

1 个答案:

答案 0 :(得分:1)

原来这是配置XML文件不完整的问题,所以Spring并没有创建所有对象。一旦我们纠正了XML文件,问题就解决了。