浏览 5523 次
锁定老帖子 主题:HibernateTemplate问题!
该帖已经被评为新手帖
|
|
---|---|
作者 | 正文 |
发表时间:2006-12-08
public class RoomDaoImpl extends HibernateDaoSupport implements RoomDao{ public List findUnbookroom()throws Exception{ //String hql="from ROOMS where booked=?"; String hql="from ROOMS"; //return (List)getHibernateTemplate().find(hql,new Integer(0)); return (List)getHibernateTemplate().find(hql); } } public class RoomserviceImpl implements Roomservice{ private RoomDao roomDao=new RoomDaoImpl(); public List findUnbookroom()throws Exception{ return roomDao.findUnbookroom(); } } 下面是我的测试代码: public class test2 extends TestCase { public void test() throws Exception{ Roomservice roomservice=new RoomserviceImpl(); List unbookroomlist=roomservice.findUnbookroom(); Room room=(Room)unbookroomlist.get(0); //Room room=roomservice.getSelectId(1); //assertEquals(null,room); int size=unbookroomlist.size(); //assertEquals("zhong",room.getType()); assertEquals(1,size); } } 测试出现红条: java.lang.NullPointerException at com.lingirl.RoomDaoImpl.findUnbookroom(RoomDaoImpl.java:21) at com.lingirl.RoomserviceImpl.findUnbookroom(RoomserviceImpl.java:10) at com.lingirl.test2.test(test2.java:15) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at junit.framework.TestCase.runTest(TestCase.java:154) at junit.framework.TestCase.runBare(TestCase.java:127) at junit.framework.TestResult$1.protect(TestResult.java:106) at junit.framework.TestResult.runProtected(TestResult.java:124) at junit.framework.TestResult.run(TestResult.java:109) at junit.framework.TestCase.run(TestCase.java:118) at junit.framework.TestSuite.runTest(TestSuite.java:208) at junit.framework.TestSuite.run(TestSuite.java:203) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:478) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:344) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196) 不知道为什么返回不了查询结果,请问我写的代码有错误吗? 最好谁可以指教一下有关HIBERNATETEMPLATE的用法,谢谢了~~ 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2006-12-09
估计是RoomDaoImpl中没有注入sessionFacory
|
|
返回顶楼 | |
发表时间:2006-12-09
把配置文件发上来看看吧
|
|
返回顶楼 | |
发表时间:2006-12-09
To eric_chan:
我已经在配置文件里依赖注入sessionFacory. 风雪涟漪: 下面是我的配置文件: <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <!-- - springside上说,PropertyPlaceholderConfigurer不能使用lazy-load --> <beans default-lazy-init="false"> <!-- - 指定配置文件 --> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:hibernate.properties</value> </list> </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/test"/> <property name="username" value="root"/> <property name="password" value="1234"/> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource"><ref local="dataSource"/></property> <property name="mappingResources"> <list> <value>com/lingirl/domain/Room.hbm.xml</value> <value>com/lingirl/domain/Roombook.hbm.xml</value> <value>com/lingirl/domain/User.hbm.xml</value> <value>com/lingirl/domain/Client.hbm.xml</value> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> </props> </property> </bean> <bean id="roomDao" class="com.lingirl.RoomDaoImpl"> <property name="sessionFactory"><ref local="sessionFactory"/></property> </bean> </beans> |
|
返回顶楼 | |
发表时间:2006-12-09
public class RoomserviceImpl implements Roomservice{ private RoomDao roomDao=new RoomDaoImpl(); public List findUnbookroom()throws Exception{ return roomDao.findUnbookroom(); } } 这段代码有问题roomDao应该注射进来,你这样new出来就没有sessionFactory |
|
返回顶楼 | |
发表时间:2006-12-09
TO eric_chan:
刚才参考别人的一个例子,也发现了这个问题,我现在已经改过来了: public class RoomserviceImpl implements Roomservice{ private RoomDao roomDao; public void setRoomDao(RoomDao roomDao){ this.roomDao=roomDao; } public List findUnbookroom()throws Exception{ return roomDao.findUnbookroom(); } } 但是还是一样没有返回查询结果,还是一样的错误~ |
|
返回顶楼 | |
发表时间:2006-12-09
你把抛nullPointer的那行代码指出来啊
|
|
返回顶楼 | |
发表时间:2006-12-10
dao 要注入你的Service,在配置文件中没有看到你的Service的配置.
|
|
返回顶楼 | |
发表时间:2007-05-25
你应该是从AplicationContext 中用getBean取得这个dao才行啊
|
|
返回顶楼 | |