问题描述
- 我的hibernate和spring 整合时getHibernateTemplate().find出现空指针问题
-
Spring组件:
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<!-- 数据库连接参数信息 -->方言,映射描述都没问题,
/context:component-scan<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <!-- 定义通知 --> <tx:advice id="txAdvice" transaction-manager="txManager"> <!-- 指定目标组件中的哪些方法采用哪种事务管理方式 --> <tx:attributes> <tx:method name="find*" read-only="true" propagation="REQUIRED"/> <tx:method name="get*" read-only="true" propagation="REQUIRED"/> <tx:method name="save*" propagation="REQUIRED"/> <tx:method name="update*" propagation="REQUIRED"/> <tx:method name="delete*" propagation="REQUIRED"/> </tx:attributes> </tx:advice> <!-- AOP配置,将通知和切入结合(目标对象) --> <aop:config> <!-- 将所有DAO作目标 --> <aop:pointcut expression="within(org.dao..*)"
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"> </property> </bean> <!-- 开启事务注解配置 --> <tx:annotation-driven transaction-manager="txManager"/>
解决方案
三张表:
UserInfo_SEQ
<property name="grade" type="integer">
<column name="GRADE" />
</property>
<property name="Content" type="string">
<column name="CONTENT" />
</property>
<property name="createDate" type="date">
<column name="CREATEDATE" />
</property>
<property name="userId" type="integer" update="false"
insert="false" >
<column name="USERID" />
</property>
<property name="flag" type="integer">
<column name="FLAG" />
</property>
<!-- 指定userInfo属性,采用many-to-one关系加载 -->
<many-to-one name="userInfo" column="USERID" lazy="false"
class="org.entity.UserInfo">
</many-to-one>
<!-- 采用一(EventGrade)对多(Event)加载信息 -->
<set name="event_Grades" >
<key column="GRADE"></key>
<one-to-many
class="org.entity.Event_Grade"/>
</set>
table="EVENT_GRADE">
UserInfo_SEQ
</class>
UserInfo_SEQ
</class>
UserInfo_SEQ
<property name="userName" type="string">
<column name="USERNAME" />
</property>
<!-- 采用一对多关系加载相关的Service记录 -->
<set name="events" inverse="true" cascade="all">
<!-- 指定关联条件字段,写外键字段 -->
<key column="USERID"></key>
<!-- 指定要采用的关系,要加载的数据类型 -->
<one-to-many class="org.entity.Event"/>
</set>
</class>
解决方案二:
table="EVENT_GRADE">
UserInfo_SEQ
解决方案三:
public Event findById(Integer id)throws DAOException{
// String findAllById = "from Event where id = ?";
// Object[] primes = {id};
List list = (List)this.getHibernateTemplate().find("from Event where id ="+id );
if(list!=null && !list.isEmpty()){
return list.get(0);
}
// Event event = (Event)this.getHibernateTemplate().get(Event.class,id);
// return event;
return null;
}
public List findGrade() throws DAOException {
String findGradeName = "select gradeName from Event_Grade ";
List names= new ArrayList();
System.out.println( this.getHibernateTemplate().find( findGradeName));
names.addAll( this.getHibernateTemplate().find(findGradeName));
if(!names.isEmpty()){
for (Object o : names) {
System.out.println(o);
}
return names;
}else {
System.out.println("DAOImpl为空");
return null;
}
}
测试返回的结果都为null,请哪位大神给解决一下,谢谢了
解决方案四:
把错误信息贴出来塞。。。。。。
解决方案五:
同求 看了好多贴了 为毛这个问题就没人回答呢 回答的也是牛头不对马嘴,不切要害,