初学SSH框架,遇到一个小问题

dai_jiawei 2014-05-06 12:12:52
小弟初学SSH框架,遇到一个小小的问题。比如我要对一条新闻进行增删改查。。增和查都可以修改到数据库,而删和改测试的时候都没有报错。但是数据库中却没被修改到。。
代码如下。。求大神指点
//通过ID查找新闻
public News findNewsById(int newsId) {
return (News) sessionFactory.openSession()
.get(News.class, newsId);
}

//更新新闻
public boolean updateNews(News news) {
try {
sessionFactory.openSession().update(news);
return true;
} catch (RuntimeException e) {
e.printStackTrace();
}
return false;
}

//删除新闻
public boolean deleteNews(News news) {
try {
sessionFactory.openSession().delete(news);
return true;
} catch (RuntimeException e) {
e.printStackTrace();
}
return false;
}


//保存新闻
public boolean saveNews(News news) {
try {
sessionFactory.openSession().save(news);
return true;
} catch (RuntimeException e) {
e.printStackTrace();
}
return false;
}
ApplicationContext.xml代码如下:
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" 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
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
">


<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver">
</property>
<property name="url" value="jdbc:mysql://localhost:3306/onlineshopping">
</property>
<property name="username" value="root"></property>
<property name="password" value="daijiawei"></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.MySQLDialect
</prop>
<prop key="current_session_context_class">thread</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>com/mall/entity/User.hbm.xml</value>
<value>com/mall/entity/Goodscategory.hbm.xml</value>
<value>com/mall/entity/Goods.hbm.xml</value>
<value>com/mall/entity/Message.hbm.xml</value>
<value>com/mall/entity/News.hbm.xml</value>
<value>com/mall/entity/Chatrecord.hbm.xml</value>
<value>com/mall/entity/Order.hbm.xml</value>
<value>com/mall/entity/Orderdetail.hbm.xml</value>
</list>
</property>
</bean>
<!-- 声明事务管理器 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 定义事务通知 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="get*" read-only="true" propagation="NOT_SUPPORTED" />
<tx:method name="*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
<!-- 定义切面,并将事务通知和切面组合(定义哪些方法应用事务规则) -->
<aop:config>
<aop:pointcut expression="execution(* com.mall.biz.*.*(..))"
id="transactionPointcut" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="transactionPointcut" />
</aop:config>

<bean id="userDao" class="com.mall.dao.impl.UserDAOImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="orderDao" class="com.mall.dao.impl.OrderDAOImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="messageDao" class="com.mall.dao.impl.MessageDAOImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="newsDao" class="com.mall.dao.impl.NewsDAOImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>

<!-- 定义UserBizImpl,并给其属性userDao注入实例 -->
<bean id="orderBiz" class="com.mall.biz.impl.OrderBizImpl">
<property name="orderDao" ref="orderDao"></property>
</bean>
<bean id="messageBiz" class="com.mall.biz.impl.MessageBizImpl">
<property name="messageDao" ref="messageDao"></property>
</bean>
<bean id="newsBiz" class="com.mall.biz.impl.NewsBizImpl">
<property name="newsDao" ref="newsDao"></property>
</bean>
<bean id="userBiz" class="com.mall.biz.impl.UserBizImpl">
<property name="userDao" ref="userDao"></property>
</bean>
<bean name="userAction" class="com.mall.action.UserAction" scope="prototype">
<property name="userBiz" ref="userBiz"></property>
</bean>
<bean name="imageAction" class="com.mall.action.ImageAction"></bean>


<bean name="newsAction" class="com.mall.action.NewsAction">
<property name="newsBiz" ref="newsBiz"></property>
</bean>
</beans>
...全文
607 20 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
pengzhang130 2014-05-15
  • 打赏
  • 举报
回复
引用 3 楼 bojustgo 的回复:
hibernate中 对数据的删除、修改、添加都需要开启事务的.你如果开启了事务,就看看最后是否提交事务。
对头!
sdjzyuxinburen 2014-05-11
  • 打赏
  • 举报
回复

session = HibernateSessionFactory.getSession();
session.beginTransaction();
session.save(t);
session.getTransaction().commit();
return true;
是不是应该这样,开启事务,进行相关操作,然后提交事务,才能执行成功
IT_163 2014-05-10
  • 打赏
  • 举报
回复
使用ssh框架,dao类最好是继承HibernateDaoSupport类,该类是spring对hibernate进行的简单封装,注意在编写dao类方法时, 要在每个需要对数据库进行操作的方法前开启事务,代码如下:

@Transactional(propagation=Propagation.REQUIRED,readOnly=true)//开启只读事务
public News findNewsById(int newsId) {
return (News)getHibernateTemplate().get(News.class,newI id);
}

//更新新闻
@Transactional(propagation=Propagation.REQUIRED)//开启事务
public boolean updateNews(News news) {
    getHibernateTemplate().update(news);
return false;
}

//删除新闻
@Transactional(propagation=Propagation.REQUIRED)//开启事务
public boolean deleteNews(News news) {
 getHibernateTemplate().delete(news);
return false;
}
希望能帮到你!!
咖啡不放糖 2014-05-09
  • 打赏
  • 举报
回复
我也才开始学习SSH,顺便看看问题
yuguanquan1990 2014-05-08
  • 打赏
  • 举报
回复
引用 12 楼 dai_jiawei 的回复:
[quote=引用 7 楼 yuguanquan1990 的回复:] 应该要close把,opensession是需要把session close的,另外,建议用getCurrentSession,这玩意要配置事物,麻烦点,但是,对于数据库操作,大多都是以事务形式的
要如何配置呢?你看下我贴出来的那个ApplicationContext.xml文件有没有错。[/quote] 配置文件中你已经添加了事物了(transactionManager),在程序中,就应该在service层添加@Transactional 在Dao层直接SessionFactory.GetCurrentSession().doSomething(),不需要你手动提交,注解会在Service层函数开始执行时添加食物,执行结束之后,事物提交,同时,你还应该扑捉HibernateException,启动事物回滚
lichao_java 2014-05-08
  • 打赏
  • 举报
回复
在<tx:method name="*" propagation="REQUIRED" />这里加个save*和delete*试试
cena520 2014-05-08
  • 打赏
  • 举报
回复
事务没有提交
tony4geek 2014-05-07
  • 打赏
  • 举报
回复
解决了没,?
yuguanquan1990 2014-05-07
  • 打赏
  • 举报
回复
应该要close把,opensession是需要把session close的,另外,建议用getCurrentSession,这玩意要配置事物,麻烦点,但是,对于数据库操作,大多都是以事务形式的
不是太高的手 2014-05-07
  • 打赏
  • 举报
回复
引用 11 楼 dai_jiawei 的回复:
引用 9 楼 zhulin2012 的回复:
sessionFactory.openSession() 这种方法最好 不要用
那要用什么呢?求指点。
HibernateTemplate 或者回调函数
dai_jiawei 2014-05-07
  • 打赏
  • 举报
回复
引用 7 楼 yuguanquan1990 的回复:
应该要close把,opensession是需要把session close的,另外,建议用getCurrentSession,这玩意要配置事物,麻烦点,但是,对于数据库操作,大多都是以事务形式的
要如何配置呢?你看下我贴出来的那个ApplicationContext.xml文件有没有错。
dai_jiawei 2014-05-07
  • 打赏
  • 举报
回复
引用 9 楼 zhulin2012 的回复:
sessionFactory.openSession() 这种方法最好 不要用
那要用什么呢?求指点。
dai_jiawei 2014-05-07
  • 打赏
  • 举报
回复
引用 8 楼 rui888 的回复:
解决了没,?
还没有。用上面的方法也没有解决。。是不是我配置文件那里有错还是什么的。。
不是太高的手 2014-05-07
  • 打赏
  • 举报
回复
sessionFactory.openSession() 这种方法最好 不要用
栖息z 2014-05-06
  • 打赏
  • 举报
回复
开启事务,修改之后commit()
点滴寸土 2014-05-06
  • 打赏
  • 举报
回复
需要commit提交事务
「已注销」 2014-05-06
  • 打赏
  • 举报
回复
hibernate中 对数据的删除、修改、添加都需要开启事务的.你如果开启了事务,就看看最后是否提交事务。
春风化作秋雨 2014-05-06
  • 打赏
  • 举报
回复
提交事务,commit
小灯光环 2014-05-06
  • 打赏
  • 举报
回复
你没有封装,直接通过Session删除和修改的话这样做貌似不行~ 删除是根据id删的,你改成: News bean=session.load(News.class,news.getXxxId); session.delete(bean); 修改的话也一样: News bean=session.load(News.class,news.getXxxId); bean.setXxxx(xxx); bean.setXxxx(xxx); session.update(bean);
天狼10010 2014-05-06
  • 打赏
  • 举报
回复
对数据库做了,增删改需要开启事物,而查补需要开启事物。

81,122

社区成员

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

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