当前位置: 代码迷 >> J2EE >> Spring与Hibernate整合getHibernateTemplate().delete(object)删除不了数据,该如何解决
  详细解决方案

Spring与Hibernate整合getHibernateTemplate().delete(object)删除不了数据,该如何解决

热度:95   发布时间:2016-04-22 02:14:40.0
Spring与Hibernate整合getHibernateTemplate().delete(object)删除不了数据
xml文件
XML code
    <bean id="sessionFactory"        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">        <property name="configLocation"             value="classpath:hibernate.cfg.xml">        </property>    </bean>    <bean id="postdaohib" class="daoimpl.PostsDaoHibImple" >    <property name="sessionFactory" ref="sessionFactory"></property>    </bean>

Java code
public class PostsDaoHibImple extends HibernateDaoSupport implements PostsDao {     public void del(int id) {        HibernateTemplate ht=super.getHibernateTemplate();        Posts p=ht.get(Posts.class, id);         try {                 ht.delete(p);             System.out.println("删除");             ht.flush();             } catch(Exception e){                 System.out.println("删除出错");             }        }    public static void main(String[] args)    {         ApplicationContext c = new ClassPathXmlApplicationContext("applicationContext.xml");        PostsDaoHibImple p=(PostsDaoHibImple) c.getBean("postdaohib");        p.del(35);          }}

我先后台打印,再删除,在打印。后台显示的数据确实删除了,但是数据库里还是没有删除,个人怀疑是数据回滚了,不知道怎么处理,查资料也弄了一晚上,请教各位了。



------解决方案--------------------
catch(Exception e){
System.out.println("删除出错");
}
把错误信息打印出来看看

可能是hibernate设置关联了
------解决方案--------------------
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean> 
网上例子很多 看下这个 http://www.javaeye.com/topic/615413
------解决方案--------------------
你把实体配置的xml发出来,如果使用注释,贴出带注释的实体
  相关解决方案