xml文件 <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>
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);
}
}我先后台打印,再删除,在打印。后台显示的数据确实删除了,但是数据库里还是没有删除,个人怀疑是数据回滚了,不知道怎么处理,查资料也弄了一晚上,请教各位了。
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>
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);
}
}我先后台打印,再删除,在打印。后台显示的数据确实删除了,但是数据库里还是没有删除,个人怀疑是数据回滚了,不知道怎么处理,查资料也弄了一晚上,请教各位了。
System.out.println("删除出错");
}
把错误信息打印出来看看可能是hibernate设置关联了
delete(entity);
删除后打印: 34 35看数据库数据还在,反复运行这个程序还是一样..总之没删除.
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
网上例子很多 看下这个 http://www.javaeye.com/topic/615413
getHibernateTemplate().delete(entity)一句这个应该不需要配置吧. 继续等待. 谢谢
delete(entity);
删除后打印: 34 35
33,怎么没了???
跟这个没关系,我后面的操作,33被我用Hibernate Session.delete();实验删除掉了.跟这个没关
ht.delete(p);
System.out.println("删除");
ht.flush();
} catch(Exception e){
System.out.println("删除出错");
}
你的try,catch语句,中的,System.out.println("删除");
执行没???
执行了, 本来delete(33);
执行后打印34,35;在查还是33,34,35。好像只删除了缓存...昨天弄了一晚上..
<property name="connection.autocommit">true</property>
问题解决了..Spring.你害我昨天弄了一晚上..
总之我又学到东西了..3q