Hibernate Spring的整合求高手指导快要崩溃了

mzx16822 2012-06-25 04:15:25
public Book selectBook(String ISBN){
System.out.println(ISBN);
List list=this.getHibernateTemplate().find("from Book where ISBN=?",ISBN); //报错就是这一行
Book book=null;
if(list.size()>0)
{
book=(Book)list.get(0);
}
return book;
}

/////*******************////
public interface BookDao {

public void addBook(Book book);
public void deleteBook(String ISBN);
public void updateBook(Book book);
public Book selectBook(String ISBN);

}


///////////////**************////////
public interface StudentService {
public Student selectStudent(String readerId);
}


网页报错/////////////***///////
java.lang.NullPointerException
org.Dao.impl.BookDaoImpl.selectBook(BookDaoImpl.java:14)
org.action.BookAction.selectBook(BookAction.java:107)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:597)
...全文
324 23 打赏 收藏 转发到动态 举报
写回复
用AI写文章
23 条回复
切换为时间正序
请发表友善的回复…
发表回复
风火轮 2012-06-29
  • 打赏
  • 举报
回复

<bean id="LoginAction" class="org.action.LoginAction">
<property name="loginservice" ref="loginservice"></property>
</bean>

<bean id="LendAction" class="org.action.LendAction">
<property name="lendservice" ref="lendservice"></property>
<property name="bookservice" ref="bookservice"></property>
<property name="studentservice" ref="studentservice"></property>
</bean>

<bean id="BookAction" class="org.action.BookAction">
<property name="bookservice" ref="bookservice"></property>
<property name="lendservice" ref="lendservice"></property>
</bean>
这是你的三个action 你真正调用入口 这几哥action怎么获取的 不要告诉我你是new出来的
风火轮 2012-06-29
  • 打赏
  • 举报
回复
我估计根本就不是这个问题 你的controller层是怎么 调用的
  • 打赏
  • 举报
回复
1.在配置文件中配置
<!-- hibernateTemplate设置 -->
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
2.让你的Dao extends HibernateDaoSupport
mzx16822 2012-06-25
  • 打赏
  • 举报
回复
[Quote=引用 19 楼 的回复:]

引用 14 楼 的回复:

1.在配置文件中配置
<!-- hibernateTemplate设置 -->
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFact……
[/Quote]
感觉应该是不用再添加了,至少哟一个能已经可以用了

BookDaoImpl代码


package org.dao.impl;

import java.util.List;

import org.dao.BookDAO.BookDao;
import org.model.Book;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

public class BookDaoImpl extends HibernateDaoSupport implements BookDao {
//查询图书信息
public Book selectBook(String isbn) {
// TODO Auto-generated method stub
List list = this.getHibernateTemplate().find("from Book where isbn=?",isbn);
Book book = null;
if(list.size()>0){
book=(Book)list.get(0);
}
return book;
}

//添加图书信息
public void addBook(Book book) {
// TODO Auto-generated method stub
this.getHibernateTemplate().save(book);
}

//删除图书信息
public void deleteBook(String isbn) {
// TODO Auto-generated method stub
this.getHibernateTemplate().delete(this.selectBook(isbn));

}

//修改图书信息
public void updateBook(Book book) {
// TODO Auto-generated method stub
this.getHibernateTemplate().update(book);

}

}




风火轮 2012-06-25
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 的回复:]

1.在配置文件中配置
<!-- hibernateTemplate设置 -->
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
……
[/Quote]

14楼这个要配的? 不要误导别人 你这个多此一举 只要注入属性sessionFactory就行了 楼主肯定其他应气得
风火轮 2012-06-25
  • 打赏
  • 举报
回复
你把 BookDaoImpl 类贴出来
mzx16822 2012-06-25
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 的回复:]

引用 8 楼 的回复:
调试都不会...

这个有什么奇怪的 你生下来就会啊
[/Quote]

没事骂骂更健康 !!
mzx16822 2012-06-25
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 的回复:]

1.在配置文件中配置
<!-- hibernateTemplate设置 -->
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
……
[/Quote]

不行啊 我还有一个登陆功能的 那个没问题啊 能用那个hibernateTemplate
关键我是洛哥 2012-06-25
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 的回复:]
调试都不会...
[/Quote]
这个有什么奇怪的 你生下来就会啊
statichu2 2012-06-25
  • 打赏
  • 举报
回复
1.在配置文件中配置
<!-- hibernateTemplate设置 -->
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
2.让你的Dao extends HibernateDaoSupport
statichu2 2012-06-25
  • 打赏
  • 举报
回复
你都没有在配置文件中配置hibernateTemplate,你怎么获得它呢
mzx16822 2012-06-25
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

跟踪一下看看你的this.getHibernateTemplate()是不是空的?
[/Quote]
我打印了一下 的确是空的!配置哪里出问题啊 帮我看看?
mzx16822 2012-06-25
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 的回复:]

把你的sessionFactory的配置和dataSource的配置贴出来,应该就是你配置的问题
[/Quote]

<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="com.microsoft.sqlserver.jdbc.SQLServerDriver">
</property>
<property name="url"
value="jdbc:sqlserver://localhost:1433;databaseName=CH12Data">
</property>
<property name="username" value="sa"></property>
<property name="password" value="1680012"></property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.SQLServerDialect
</prop>
<prop key="hibernate.show_sql">
true
</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>org/model/Login.hbm.xml</value>
<value>org/model/Book.hbm.xml</value>
<value>org/model/Student.hbm.xml</value>
<value>org/model/Lend.hbm.xml</value></list>
</property></bean>
<bean id="StudentDAO" class="org.Dao.impl.StudentDaoImpl">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="LendDAO" class="org.Dao.impl.LendDaoImpl">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="BookDAO" class="org.Dao.impl.BookDaoImpl">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="LoginDAO" class="org.Dao.impl.LoginDaoImpl">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>

<bean id="bookservice" class="org.service.Impl.BookServiceImpl">
<property name="bookDao" ref="BookDAO"></property>
</bean>
<bean id="loginservice" class="org.service.Impl.LoginServiceImpl">
<property name="loginDao" ref="LoginDAO"></property></bean>

<bean id="studentservice" class="org.service.Impl.StudentServiceImpl">
<property name="studentDao" ref="StudentDAO"></property></bean>

<bean id="lendservice" class="org.service.Impl.LendServiceImpl">
<property name="lendDao" ref="LendDAO"></property></bean>

<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory"></ref>
</property></bean>

<bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
<property name="transactionManager" ref="transactionManager"></property>
<property name="transactionAttributes">
<props>
<prop key="delete*">PROPAGATION_REQUIRED</prop>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="beanNames">
<list>
<value>bookservice</value>
<value>studentservice</value>
<value>loginservice</value>
<value>lendservice</value>
</list>
</property>
<property name="interceptorNames">
<list>
<value>transactionInterceptor</value>
</list>
</property>
</bean>

<bean id="LoginAction" class="org.action.LoginAction">
<property name="loginservice" ref="loginservice"></property>
</bean>

<bean id="LendAction" class="org.action.LendAction">
<property name="lendservice" ref="lendservice"></property>
<property name="bookservice" ref="bookservice"></property>
<property name="studentservice" ref="studentservice"></property>
</bean>

<bean id="BookAction" class="org.action.BookAction">
<property name="bookservice" ref="bookservice"></property>
<property name="lendservice" ref="lendservice"></property>
</bean>
</beans>




<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="org.model.Book" table="book" schema="dbo" catalog="CH12Data">
<id name="isbn" type="java.lang.String">
<column name="ISBN" length="20" />
<generator class="assigned" />
</id>
<property name="bookName" type="java.lang.String">
<column name="bookName" length="40" not-null="true" />
</property>
<property name="author" type="java.lang.String">
<column name="author" length="20" not-null="true" />
</property>
<property name="publisher" type="java.lang.String">
<column name="publisher" length="20" not-null="true" />
</property>
<property name="price" type="java.lang.Double">
<column name="price" precision="53" scale="0" not-null="true" />
</property>
<property name="cnum" type="java.lang.Integer">
<column name="cnum" not-null="true" />
</property>
<property name="snum" type="java.lang.Integer">
<column name="snum" not-null="true" />
</property>
<property name="summary" type="java.lang.String">
<column name="summary" length="200" />
</property>
<property name="photo" type="java.lang.String">
<column name="photo" length="50" />
</property>
</class>
</hibernate-mapping>



mzx16822 2012-06-25
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 的回复:]

调试都不会...
[/Quote]
是蛮失败的,主要是不大了解运行原理造成的!!
zxcliuwei 2012-06-25
  • 打赏
  • 举报
回复
把你的sessionFactory的配置和dataSource的配置贴出来,应该就是你配置的问题
SmileToCandy 2012-06-25
  • 打赏
  • 举报
回复
调试都不会...
statichu2 2012-06-25
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 的回复:]

引用 1 楼 的回复:

跟踪一下看看你的this.getHibernateTemplate()是不是空的?


怎么跟踪 我不大会用啊
[/Quote]

dubug跟踪一下,断点打在 List list=this.getHibernateTemplate().find("from Book where ISBN=?",ISBN);这一行,选中this.getHibernateTemplate() ctrl+shift+i看一下获得到的值是不是空的,要是空的可能就是你配置的问题了
zxcliuwei 2012-06-25
  • 打赏
  • 举报
回复
那你怎么配置的sessionFactory贴出来
mzx16822 2012-06-25
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 的回复:]

你把SessionFactory的配置贴出来啊
[/Quote]

<bean id="StudentDAO" class="org.Dao.impl.StudentDaoImpl">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="LendDAO" class="org.Dao.impl.LendDaoImpl">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="BookDAO" class="org.Dao.impl.BookDaoImpl">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="LoginDAO" class="org.Dao.impl.LoginDaoImpl">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
mzx16822 2012-06-25
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

跟踪一下看看你的this.getHibernateTemplate()是不是空的?
[/Quote]

怎么跟踪 我不大会用啊
加载更多回复(3)

81,091

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧